sftp: implement open with config

This commit is contained in:
Alexander Neumann 2015-12-28 18:22:19 +01:00
parent 7b1e8fdd06
commit f7c9091970
1 changed files with 23 additions and 0 deletions

View File

@ -92,6 +92,23 @@ func Open(dir string, program string, args ...string) (*SFTP, error) {
return sftp, nil
}
func buildSSHCommand(cfg Config) []string {
args := []string{cfg.Host}
if cfg.User != "" {
args = append(args, "-l")
args = append(args, cfg.User)
}
args = append(args, "-s")
args = append(args, "sftp")
return args
}
// OpenWithConfig opens an sftp backend as described by the config by running
// "ssh" with the appropiate arguments.
func OpenWithConfig(cfg Config) (*SFTP, error) {
return Open(cfg.Dir, "ssh", buildSSHCommand(cfg)...)
}
// Create creates all the necessary files and directories for a new sftp
// backend at dir. Afterwards a new config blob should be created.
func Create(dir string, program string, args ...string) (*SFTP, error) {
@ -138,6 +155,12 @@ func Create(dir string, program string, args ...string) (*SFTP, error) {
return Open(dir, program, args...)
}
// CreateWithConfig creates an sftp backend as described by the config by running
// "ssh" with the appropiate arguments.
func CreateWithConfig(cfg Config) (*SFTP, error) {
return Create(cfg.Dir, "ssh", buildSSHCommand(cfg)...)
}
// Location returns this backend's location (the directory name).
func (r *SFTP) Location() string {
return r.p