GO-通过SSH连接执行命令时发生未知错误

I'm trying to execute a command via session.Run() function over a ssh connection. So far I can successfully execute some commands but on others I keep getting the following error: "Process exited with: 1. Reason was: () exit status 1"

func (p *project) connect(config *ssh.ClientConfig) {

    log.Printf("Trying connection...
")

    conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", p.hostname.name, p.port.name), config)
    checkError("Failed to dial: ", err)
    log.Printf("Connection established.
")

    for step := range p.typ.program.setup {
        p.install(step, conn)
    }
}

func (p *project) install(step int, conn *ssh.Client) {
    session, err := conn.NewSession()
    checkError("Failed to build session: ", err)
    defer session.Close()

    var stdoutBuf bytes.Buffer
    session.Stdout = &stdoutBuf

    log.Printf("Executing command: %s", p.typ.program.setup[step])

    if err := session.Run(p.typ.program.setup[step]); err != nil {
        log.Println(session.Stdout)
        log.Fatal("Error on command execution", err.Error())
    }
}


// That would be an example of a command which returns me an error
// "cd ~/www/www/ && git commit -m 'on the beginning was the commit'"
// That comes inside a slice on p.typ.program.setup accessed by the step(index).

The command output (session.Stdout) is the one i expect:

 "# On branch master nothing to commit, working directory clean"

And just to note I already tried to execute the command directly on the console and it works just fine.

So, the code seems to be okay, the command ran on the remote but I still have an error no matter what.

Does anyone have a clue about why is that happening?

Thanks in advance.

Maybe my library will helps in your case: https://github.com/shagabutdinov/shell; it covers basic cases of running ssh commands in one session.

Try following:

handler := func(outputType int, message string) {
    if(outputType == shell.Stdout) {
        log.Println("stdout: ", message)
    } else if(outputType == shell.Stdout) {
        log.Println("stderr: ", message)
    }
}

key, err := ssh.ParsePrivateKey([]byte(YOUR_PRIVATE_KEY))
if(err != nil) {
    panic(err)
}

auth := []ssh.AuthMethod{ssh.PublicKeys(key)}
shell = shell.NewRemote(shell.RemoteConfig{
    Host: "root@example.com:22",
    Auth: auth,
})

if(err != nil) {
    panic(err)
}

status, err := shell.Run("cd ~/www/www/", handler)
if(err != nil) {
    panic(err)
}

status, err := shell.Run("git commit -m 'on the beginning was the commit'", handler)
if(err != nil) {
    panic(err)
}

if(status == 0) {
    console.log("command executed successfully")
} else {
    console.log("command execution failed")
}