From cf068885fdf1f25e9e53ee2b89e09ba29393c1d3 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 11 Apr 2021 21:51:38 +0700 Subject: ssh: check for empty private key file on Get and Put If the private key file is empty, skip it for being added as parameter of scp command. This is to prevent warning message on user side. --- lib/ssh/client.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/ssh/client.go b/lib/ssh/client.go index 184b2e45..419f3682 100644 --- a/lib/ssh/client.go +++ b/lib/ssh/client.go @@ -132,8 +132,15 @@ func (cl *Client) Get(remote, local string) (err error) { remote = fmt.Sprintf("%s@%s:%s", cl.cfg.User, cl.cfg.Hostname, remote) - cmd := exec.Command("scp", "-r", "-i", cl.cfg.privateKeyFile, - "-P", cl.cfg.stringPort, remote, local) + args := []string{"-r", "-P", cl.cfg.stringPort} + if len(cl.cfg.privateKeyFile) > 0 { + args = append(args, "-i") + args = append(args, cl.cfg.privateKeyFile) + } + args = append(args, remote) + args = append(args, local) + + cmd := exec.Command("scp", args...) cmd.Dir = cl.cfg.workingDir cmd.Stdout = os.Stdout @@ -165,8 +172,15 @@ func (cl *Client) Put(local, remote string) (err error) { remote = fmt.Sprintf("%s@%s:%s", cl.cfg.User, cl.cfg.Hostname, remote) - cmd := exec.Command("scp", "-r", "-i", cl.cfg.privateKeyFile, - "-P", cl.cfg.stringPort, local, remote) + args := []string{"-r", "-P", cl.cfg.stringPort} + if len(cl.cfg.privateKeyFile) > 0 { + args = append(args, "-i") + args = append(args, cl.cfg.privateKeyFile) + } + args = append(args, local) + args = append(args, remote) + + cmd := exec.Command("scp", args...) cmd.Dir = cl.cfg.workingDir cmd.Stdout = os.Stdout -- cgit v1.3