Using golang connect to docker container with functional tty - go

Start simple Docker container in Detached(Background) mode
docker run -d --name test ubuntu tail -f /dev/null
Here comes my simple golang code where I connect to running container.
In current connection I want to get functional tty.
package main
import (
"fmt"
"os/exec"
"bufio"
"io"
"os"
"github.com/kr/pty"
)
func main() {
cmd := exec.Command("docker", "exec", "-it", "test", "bin/bash")
tty, err := pty.Start(cmd)
if err != nil {
fmt.Println("Error start cmd", err)
}
defer tty.Close()
go func() {
scanner := bufio.NewScanner(tty)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}()
go func() {
io.Copy(tty, os.Stdin)
}()
err = cmd.Wait()
if err != nil {
fmt.Println("Error Waiting", err)
}
}
More less it works, but there is couple thinks which is not working as I would run docker command from my terminal.
After login I don't see prompt, like root#ba351b44ca80:/# only after hitting return it appears, but my currsor is in new line where are no prompt;
Also arrow up to get previous command is not working
Only prints out
root#ba351b44ca80:/#
^[[A^[[A^[[A
but behind scene previous command is selected and by hitting return it is executed.
After executing command for cursor is not displayed prompt, like
root#ba351b44ca80:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
<Here my cursor>

go-dockerclient is worth checking out. It is a simple nice abstraction of the Docker remote API. It is used by many opensource projects and is regularly maintained as well.

Related

How to properly "go build" using exec.Command with many arguments?

I'm trying to compile a go package using exec.Command. I was successful with the arguments being "go" and "build" as such below:
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
out, err := exec.Command("go", "build").Output()
if err != nil {
log.Fatal(err)
}
fmt.Println(out)
}
However, while trying to perform a "go build" with more arguments it seems that I'm doing something wrong? This is what my code looks like:
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
out, err := exec.Command("set", "GOOS=js&&", "set", "GOARCH=wasm&&", "go", "build", "-o", "C:/Users/Daniel/Desktop/go-workspace/src/sandbox/other/wasm/assets/json.wasm", "kard").Output()
if err != nil {
log.Fatal(err)
}
fmt.Println(out)
}
The output is exec: "set": executable file not found in %PATH%
The normal command I would perform for this in the command line would be set GOOS=js&& set GOARCH=wasm&& go build -o C:\Users\Daniel\Desktop\go-workspace\src\sandbox\other\wasm\assets\json.wasm kard.
I assume there is something I'm misunderstanding with using exec.Command? I really appreciate any support.
The application uses shell syntax to set the environment variables, but the exec package does not use a shell (unless the command that you are running is a shell).
Use the command environment to specify the environment variables for the exec'ed command.
The go build does not normally write to stdout, but it does write errors to stderr. Use CombinedOutput instead of Output to easily capture error text.
cmd := exec.Command("go", "build", "-o", "C:/Users/Daniel/Desktop/go-workspace/src/sandbox/other/wasm/assets/json.wasm", "kard")
cmd.Env = []string{"GOOS=js", "GOARCH=wasm"}
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("%v: %s\n", err, out)
}

Called program fails if run through os.exec

I am not sure if this is a problem of the called program or if the problem is caused by the way I call the program. Because of this I start at the source code.
I need to call ssh from a program (if you are interested in the reasons I will mention them below) but ssh silently exits.
When I call ssh -v user#remotehost from shell this succeeds:
the wanted debug output on stderr is shown
I am asked for the password
I can see the remote hosts shell
But when I do the same from within my program (myssh -v user#remotehost only this happens:
I am asked for the password
Neither the debug output on stderr is shown nor do I reach the remote hosts shell.
This is my sourcecode:
package main
import (
"fmt"
"log"
"os"
"os/exec"
)
func main() {
params := os.Args[1:]
fmt.Printf("passing this to ssh: %s\n", params)
cmd := exec.Command("ssh", params...)
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
Reason why I wrote this code: I use Ansible which calls ssh. I need to "manipulate" the parameters that Ansible passes to ssh. So far I asked on the OpenSSH and Ansible mailing lists, there is no means in OpenSSH and Ansible to change the parameters (for others it is, but not those I need). The best suggestion I got and that I want to implement is to provide an alternative ssh command to Ansible, use that to receive and modify the parameters and pass them on to the real ssh.
Are you capturing Stdout and Stderr from cmd? This is where the respective outputs of the command is sent to. The documentation for exec.Command has a really good example on this.
In your case, you'll also want to setup Stdin so that you can pass the password for example.
Here's a really basic example based on your code:
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
)
func main() {
params := os.Args[1:]
fmt.Println("Passing this to ssh: %s", params)
var stdin bytes.Buffer
var stdout bytes.Buffer
var stderr bytes.Bufer
cmd := exec.Command("ssh", params...)
cmd.Stdin = &stdin
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
fmt.Println("Stdout: %s", stdout.String())
fmt.Println("stderr: %s", stderr.String())
}
Since stdin, stdout, and stderr are all bytes.Buffers, you can read and write from them just like any other buffer.
You might also want to consider using the golang.org/x/crypto/ssh package which provides a native SSH interface for Go instead of using sub-processes.

Execute an external command and return its output

I'm trying to execute a linux command and convert the output to an int. This is my current code:
package main
import (
"os/exec"
"os"
"strconv"
_"fmt"
"log"
"bytes"
)
func main(){
cmd := exec.Command("ulimit", "-n")
cmdOutput := &bytes.Buffer{}
cmd.Stdout = cmdOutput
err := cmd.Run()
if err != nil {
os.Stderr.WriteString(err.Error())
}
count, err := strconv.Atoi( string(cmdOutput.Bytes()) )
if err != nil {
log.Fatal(err)
}
if count <= 1024 {
log.Fatal("This machine is not good for working!!!")
}
}
This is my current error:
2018/10/12 14:37:27 exec: "ulimit -n": executable file not found in
$PATH
I don't understand what this error means and how I can fix it.
There is no ulimit program in linux that you can run.
ulimit is a builtin of a shell. So you need to run a shell and have the shell run its internal ulimit command.
cmd := exec.Command("/bin/sh", "-c" "ulimit -n")
you will also have to remove the newline from the output of the ulimit command, e.g.
count, err := strconv.Atoi( strings.Trim(string(cmdOutput.Bytes()),"\n"))
A better alternative is to retreive these limits via the syscall API in go, see How to set ulimit -n from a golang program? - the accepted answer first gets and prints the current limit.

Spawning a docker command yields an empty result

I have the following Go code, where I try to spawn docker command and get its output:
package main
import "fmt"
import "os/exec"
func main() {
cmd := exec.Command("docker")
cmdOutput, err := cmd.Output()
if (err != nil) {
panic(err)
}
fmt.Println(string(cmdOutput))
}
But when I run this code as a result I get an empty string, which is stange since I get an output when I run docker command directly in a command line.
Moreover, this same code yields results just fine if I spawn other commands with it, for example ls.
What may be wrong here?
try using cmd.CombinedOutput()

How to start vim from go?

I've got a command line tool written in Golang and I need to start vim from it. However it's not working, and there's not any error or much else to work with. I've reduced the code to just this:
package main
import (
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("vim", "test.txt")
err := cmd.Run()
fmt.Println(err)
}
When I run this, I can see the vim process for a 2-3 seconds but the application doesn't actually open. Then the program simply exits (and the vim process closes) with an "exit status 1".
I've also tried this to capture stderr:
package main
import (
"bytes"
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("vim", "test.txt")
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
fmt.Println(err)
fmt.Println(stderr)
}
But in this case, the program gets stuck indefinitely.
Any idea what could be the issue?
Pass on stdin and stdout from the calling program which, provided it was run from a terminal (likely for a command line program) will start vim for you and return control when the user has finished editing the file.
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
cmd := exec.Command("vim", "test.txt")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
err := cmd.Run()
fmt.Println(err)
}
VIM needs a proper terminal and detects the absence of one.
If you use the StderrPipe and read it while vim is running you will see this:
2014/02/02 20:25:49 Vim: Warning: Output is not to a terminal
2014/02/02 20:25:49 Vim: Warning: Input is not from a terminal
Example for reading stderr while executing (on play):
func logger(pipe io.ReadCloser) {
reader := bufio.NewReader(pipe)
for {
output, err := reader.ReadString('\n')
if err != nil {
log.Println(err)
return
}
log.Print(string(output))
}
}
pipe, err := cmd.StderrPipe()
go logger(pipe)
cmd.Run()
For vim to run you probably need to emulate a terminal.
Maybe goat (doc) can help you out:
tty := term.NewTTY(os.Stdin)
cmd := exec.Command("vim", "test.txt")
cmd.Stdin = t
cmd.Stdout = t
// ...

Resources