From ce0c609e727189f69c397fc3ca517e5c6b02556e Mon Sep 17 00:00:00 2001 From: Arjun Baindur Date: Tue, 11 Jan 2022 11:16:02 -0800 Subject: [PATCH] Create remote directory during UploadFile --- pkg/ssh/sshclient.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/ssh/sshclient.go b/pkg/ssh/sshclient.go index 5d819b2d..feaabe32 100644 --- a/pkg/ssh/sshclient.go +++ b/pkg/ssh/sshclient.go @@ -11,6 +11,7 @@ import ( "io" "io/ioutil" "os" + "path/filepath" "github.com/pkg/sftp" "go.uber.org/zap" @@ -145,6 +146,13 @@ func (c *client) UploadFile(localFile string, remoteFilePath string, mode os.Fil // create a progrssReader that will call the callback function after each read progressReader := newProgressCBReader(fInfo.Size(), localFileReader, cb) + dir, _ := filepath.Split(remoteFilePath) + if dir != "" { + if err := c.sftpClient.MkdirAll(dir); err != nil { + return fmt.Errorf("Could not create remote dir %s: %s", dir, err) + } + } + remoteFile, err := c.sftpClient.Create(remoteFilePath) if err != nil { return fmt.Errorf("unable to create file: %s", err)