What I want to do is exporting a file from my server via SFTP, in golang.
Here is the troubling code
cmd = exec.Command("sftp", "login@sftp.com", `INPUT
cd /some/path
put file.gz
quit
INPUT`)
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
fmt.Println(stderr.String())
os.Exit(1)
}
fmt.Println("done")
It gets to done
but doesn't import file.gz
.
I finally found the solution.
cmd = exec.Command("sftp", "login@sftp.com")
cmd.Stdin = strings.NewReader(`cd some/path
put file.gz
quit`)
cmd.Stderr = &stderr
err = cmd.Start()
if err != nil {
fmt.Println(stderr.String())
fmt.Println(err)
os.Exit(1)
}
err = cmd.Wait()
I set the commands I need to do once I'm in the STFP by setting cmd.Stdin
cmd.Start()
and cmd.Wait()
starts the command and stops when the whole command is done.