7

I'm building a cli in go and have the following situation. I'm calling a bash command from go that prompts the user for a login, then prints a token after login. For example:

cmd := exec.Command("vault", "login", "-method=okta", "-format=json", "username=abc")
cmd.Stdin = os.Stdinout
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
_ = cmd.Run()

This streams the output nicely, but I have no way to parse the token from the command's output after the user logs in. I've tried to wrap the cmd.Run() into piping functions like this this and this but in both cases the output returned is empty. Any ideas?

Thanks!

1
  • There is no os.Stdinout so the code seems to be entered incorrectly. Commented Jun 30, 2022 at 2:42

2 Answers 2

9

There are probably lots of packages to do this, but it's not hard to whip up your own:

package main

import (
    "fmt"
    "os"
    "os/exec"
)

type saveOutput struct {
    savedOutput []byte
}

func (so *saveOutput) Write(p []byte) (n int, err error) {
    so.savedOutput = append(so.savedOutput, p...)
    return os.Stdout.Write(p)
}

func main() {
    var so saveOutput
    cmd := exec.Command("factor", "999999")
    cmd.Stdin = os.Stdin
    cmd.Stdout = &so
    cmd.Stderr = os.Stderr
    _ = cmd.Run()
    fmt.Printf("I got this output: %s\n", so.savedOutput)
}

Playground: https://go.dev/play/p/T-o3QvGOm5q

Sign up to request clarification or add additional context in comments.

Comments

5

Don't make your structure for nothing. Use bytes.Buffer

package main

import (
    "bytes"
    "log"
)

func main() {
    var buffer bytes.Buffer
    cmd := exec.Command("vault", "login", "-method=okta", "-format=json", "username=abc")
    cmd.Stdout = &buffer
    _ = cmd.Run()
    log.Printf("Vault login output: %s", buffer.String())
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.