-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add an SFTP client interface definition - Implement an SFTP client upload method using native Go SSH and SFTP packages - Add SFTP configuration with some default values - Generate a client mock with mockgen - Add SFTP client tests against a test SFTP server
- Loading branch information
Showing
21 changed files
with
741 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package sftp | ||
|
||
import ( | ||
"context" | ||
"io" | ||
) | ||
|
||
// A Client manages the transmission of data over SFTP. | ||
// | ||
// Implementations of the Client interface handle the connection details, | ||
// authentication, and other intricacies associated with different SFTP | ||
// servers and protocols. | ||
type Client interface { | ||
// Upload transfers data from the provided source reader to a specified | ||
// destination on the SFTP server. | ||
Upload(ctx context.Context, src io.Reader, dest string) (bytes int64, err error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package sftp | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type Config struct { | ||
// Host address, e.g. 127.0.0.1 (default), sftp.example.org. | ||
Host string | ||
|
||
// User name. | ||
User string | ||
|
||
// Host port (default: 22). | ||
Port string | ||
|
||
// Path to known_hosts file as per https://linux.die.net/man/8/sshd | ||
// "SSH_KNOWN_HOSTS FILE FORMAT" (default: "$HOME/.ssh/known_hosts"). The | ||
// known_hosts file must include the public key of the SFTP server for | ||
// authentication to succeed. | ||
KnownHostsFile string | ||
|
||
// Private key used for authentication. | ||
PrivateKey PrivateKey | ||
|
||
// Default directory on SFTP server for file transfers. | ||
RemoteDir string | ||
} | ||
|
||
type PrivateKey struct { | ||
// Path to private key file used for authentication (default: | ||
// "$HOME/.ssh/id_rsa") | ||
Path string | ||
|
||
// Passphrase (if any) used to decrypt private key. | ||
Passphrase string | ||
} | ||
|
||
// SetDefaults sets default values for some configs. | ||
func (c *Config) SetDefaults() { | ||
if c.Host == "" { | ||
c.Host = "localhost" | ||
} | ||
|
||
if c.Port == "" { | ||
c.Port = "22" | ||
} | ||
|
||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
return // Don't set default paths if homedir is unknown. | ||
} | ||
|
||
if c.KnownHostsFile == "" { | ||
c.KnownHostsFile = filepath.Join(home, ".ssh", "known_hosts") | ||
} | ||
|
||
if c.PrivateKey.Path == "" { | ||
c.PrivateKey.Path = filepath.Join(home, ".ssh", "id_rsa") | ||
} | ||
} |
Oops, something went wrong.