Hi there!
I should point out that I am starting to learn Go and my knowledge of go is very limited. I am struggling with the following problem for more than a week now without success...please help!
Basically I am trying to write an app in go that will execute a number of bash commands. The problem is that some of those commands, like setting up mountpoints on a remote machine using sshfs, require user input (adding to the list of known hosts, password to the host, etc). I am redirecting the cmd stdin and stdout to os.Stdin and os.Stdout but after typing the first input string the whole process freezes(or looks like it hangs) ,
Enough taking let the code speak for itself!:
package main
import (
"fmt"
"io"
"os"
"os/exec"
)
func main() {
cmd := exec.Command("sudo", "docker", "run", "-i", "-t", "-privileged", "-dns=[172.25.0.10]", "-w=/home/foo/", "orobix/itk_sshfs", "/bin/bash", "-c", "sshfs -o idmap=user pi...@172.17.42.1:/home/piotr/helloworld /mnt/", "touch /mnt/CraneFile") //works when executed using unix terminal
stdin, err := cmd.StdinPipe()
exitOnErr("IN PIPE!", err)
stderr, err := cmd.StderrPipe()
exitOnErr("ERR PIPE!", err)
stdout, err := cmd.StdoutPipe()
exitOnErr("OUT PIPE!", err)
err = cmd.Start()
exitOnErr("START ERROR!", err)
defer cmd.Wait()
go io.Copy(os.Stdout, stdout)
go io.Copy(stdin, os.Stdin)
go io.Copy(os.Stderr, stderr)
}
func exitOnErr(message string, err error) {
if err != nil {
fmt.Println(message, err)
os.Exit(1)
}
The sshfs command should mount a host directory inside a remote machine (docker container) and if that was successful it would create an example file on host. But the file is not created because the process looks like it is hanging after typing in password. Should I close some streams?
In order to run it you need to install docker (really really coll technology -
http://www.docker.io/) but apart that it should compile nicely on your machine.
Any input is appreciated!