Skip to content

Commit

Permalink
support write multiple files in one session
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSingee committed Dec 10, 2020
1 parent 9f730be commit 93632a1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
19 changes: 10 additions & 9 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ func (c *RemoteClient) Start(filename string, isDirectory bool) error {

command := commandBuilder.String()

return c.session.Start(command)
err := c.session.Start(command)

if err != nil {
return err
}

return c.checkResponse()
}

func (c *RemoteClient) checkResponse() error {
Expand All @@ -67,9 +73,9 @@ func (c *RemoteClient) checkResponse() error {
errorMessageBuilder := strings.Builder{}

if buffer[0] == 1 {
errorMessageBuilder.WriteString("Warning (1): ")
errorMessageBuilder.WriteString("Error (1): ")
} else if buffer[0] == 2 {
errorMessageBuilder.WriteString("Error (2): ")
errorMessageBuilder.WriteString("Fatal (2): ")
} else {
errorMessageBuilder.WriteString("UnknownError (")
errorMessageBuilder.WriteString(strconv.Itoa(int(buffer[0])))
Expand All @@ -91,12 +97,7 @@ func (c *RemoteClient) checkResponse() error {
}

func (c *RemoteClient) WriteFile(perm string, size int64, filename string, data io.Reader) error {
err := c.checkResponse()
if err != nil {
return err
}

_, err = fmt.Fprintln(c.stdin, "C"+perm, size, filename)
_, err := fmt.Fprintln(c.stdin, "C"+perm, size, filename)
if err != nil {
return err
}
Expand Down
25 changes: 25 additions & 0 deletions protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,28 @@ func TestWriteFileToUnExistFolder(t *testing.T) {

tt.AssertFalse(t, checkFileExist(t, "/test/not/exist/folder"))
}

func TestWriteMultipleFilesToFolder(t *testing.T) {
reset(t)

session := getSshSession(t)
defer session.Close()

client, err := NewClient(session)
tt.AssertIsNil(t, err)

err = client.Start("/test", false)
tt.AssertIsNil(t, err)

err = client.WriteFile("0644", 6, "test01", strings.NewReader("hahaha"))
tt.AssertIsNil(t, err)

err = client.WriteFile("0644", 8, "test02", strings.NewReader("xixixixi"))
tt.AssertIsNil(t, err)

tt.AssertTrue(t, checkFileExist(t, "/test/test01"))
tt.AssertTrue(t, checkFileExist(t, "/test/test02"))

tt.AssertEqual(t, "hahaha", string(readFile(t, "/test/test01")))
tt.AssertEqual(t, "xixixixi", string(readFile(t, "/test/test02")))
}

0 comments on commit 93632a1

Please sign in to comment.