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 1 commit
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.

5 changes: 5 additions & 0 deletions sftp/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func (fm *fileManagerImpl) Upload(localFilePath, remoteDir string) error {
_ = localFile.Close()
}()

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

remoteFileName := filepath.Join(remoteDir, filepath.Base(localFilePath))
remoteFile, err := fm.client.Create(remoteFileName)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions sftp/sftp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ 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}

Expand Down
Loading