Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add remoteFilePath support and create remote directory in sftp #437

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

@mihir20 mihir20 Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be better if instead of using gomock.Any() actual value that is expected for the fn is used? i.e someRemotePath
Similarly for other mocks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make any difference? Used Any() to increase the readability

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using actual expected value, is better for testing point of view.


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
Loading