From b247052a64a4954de83e50970c1551efe9f8a08b Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 14 Feb 2019 16:10:09 +0100 Subject: build: avoid dput and upload with sftp directly (#19067) (cherry picked from commit a8ddf7ad8393cff80848b193c698ce5e6440e061) --- internal/build/util.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'internal/build/util.go') diff --git a/internal/build/util.go b/internal/build/util.go index 195bdb404..a41ecfbed 100644 --- a/internal/build/util.go +++ b/internal/build/util.go @@ -177,3 +177,34 @@ func ExpandPackagesNoVendor(patterns []string) []string { } return patterns } + +// UploadSFTP uploads files to a remote host using the sftp command line tool. +// The destination host may be specified either as [user@]host[: or as a URI in +// the form sftp://[user@]host[:port]. +func UploadSFTP(identityFile, host, dir string, files []string) error { + sftp := exec.Command("sftp") + sftp.Stdout = nil + sftp.Stderr = os.Stderr + if identityFile != "" { + sftp.Args = append(sftp.Args, "-i", identityFile) + } + sftp.Args = append(sftp.Args, host) + fmt.Println(">>>", strings.Join(sftp.Args, " ")) + if *DryRunFlag { + return nil + } + + stdin, err := sftp.StdinPipe() + if err != nil { + return fmt.Errorf("can't create stdin pipe for sftp: %v", err) + } + if err := sftp.Start(); err != nil { + return err + } + in := io.MultiWriter(stdin, os.Stdout) + for _, f := range files { + fmt.Fprintln(in, "put", f, path.Join(dir, filepath.Base(f))) + } + stdin.Close() + return sftp.Wait() +} -- cgit v1.2.3