From f7c909197081f637a90da3899eb9dfac1b5bbcd1 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 28 Dec 2015 18:22:19 +0100 Subject: [PATCH] sftp: implement open with config --- backend/sftp/sftp.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/backend/sftp/sftp.go b/backend/sftp/sftp.go index 4166bdb7f..f52c85c53 100644 --- a/backend/sftp/sftp.go +++ b/backend/sftp/sftp.go @@ -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