Skip to content

Commit

Permalink
feat: add remoteFilePath support and create remote directory in sftp (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauravudia committed Apr 30, 2024
1 parent 618baa6 commit cd85cf5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
5 changes: 5 additions & 0 deletions sftp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type Client interface {
Create(path string) (io.WriteCloser, error)
Open(path string) (io.ReadCloser, error)
Remove(path string) error
MkdirAll(path string) error
}

// newSFTPClient creates an SFTP client with existing SSH client
Expand All @@ -111,3 +112,7 @@ func (c *clientImpl) Open(path string) (io.ReadCloser, error) {
func (c *clientImpl) Remove(path string) error {
return c.client.Remove(path)
}

func (c *clientImpl) MkdirAll(path string) error {
return c.client.MkdirAll(path)
}
14 changes: 14 additions & 0 deletions sftp/mock_sftp/mock_sftp_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions sftp/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewFileManager(sshClient *ssh.Client) (FileManager, error) {
}

// Upload uploads a file to the remote server
func (fm *fileManagerImpl) Upload(localFilePath, remoteDir string) error {
func (fm *fileManagerImpl) Upload(localFilePath, remoteFilePath string) error {
localFile, err := os.Open(localFilePath)
if err != nil {
return fmt.Errorf("cannot open local file: %w", err)
Expand All @@ -46,8 +46,13 @@ func (fm *fileManagerImpl) Upload(localFilePath, remoteDir string) error {
_ = localFile.Close()
}()

remoteFileName := filepath.Join(remoteDir, filepath.Base(localFilePath))
remoteFile, err := fm.client.Create(remoteFileName)
// Create the directory if it does not exist
remoteDir := filepath.Dir(remoteFilePath)
if err := fm.client.MkdirAll(remoteDir); err != nil {
return fmt.Errorf("cannot create remote directory: %w", err)
}

remoteFile, err := fm.client.Create(remoteFilePath)
if err != nil {
return fmt.Errorf("cannot create remote file: %w", err)
}
Expand Down
7 changes: 4 additions & 3 deletions sftp/sftp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ func TestUpload(t *testing.T) {

mockSFTPClient := mock_sftp.NewMockClient(ctrl)
mockSFTPClient.EXPECT().Create(gomock.Any()).Return(&nopWriteCloser{remoteBuf}, nil)
mockSFTPClient.EXPECT().MkdirAll(gomock.Any()).Return(nil)

fileManager := &fileManagerImpl{client: mockSFTPClient}

err = fileManager.Upload(localFilePath, "someRemoteDir")
err = fileManager.Upload(localFilePath, "someRemotePath")
require.NoError(t, err)
require.Equal(t, data, remoteBuf.Bytes())
}
Expand Down Expand Up @@ -226,7 +227,7 @@ func TestSFTP(t *testing.T) {
require.NoError(t, err)
defer func() { _ = session.Close() }()

remoteDir := filepath.Join("/tmp", "remote")
remoteDir := filepath.Join("/tmp", "remote", "data")
err = session.Run(fmt.Sprintf("mkdir -p %s", remoteDir))
require.NoError(t, err)

Expand All @@ -252,7 +253,7 @@ func TestSFTP(t *testing.T) {
err = os.WriteFile(localFilePath, data, 0o644)
require.NoError(t, err)

err = sftpManger.Upload(localFilePath, remoteDir)
err = sftpManger.Upload(localFilePath, remoteFilePath)
require.NoError(t, err)

err = sftpManger.Download(remoteFilePath, baseDir)
Expand Down

0 comments on commit cd85cf5

Please sign in to comment.