Home > database >  How to go exec and pass user input required by the command on run
How to go exec and pass user input required by the command on run

Time:02-08

golang expert, I'm relatively new to go exec stuff and trying to replicate the below command in golang exec

root@e4187cadd9ed:/opt/sample# ../solana transfer -k ../id.json  --from  prompt://?key=0/2 DE53G9MjqzX8WtexjdySsqogsTgGPD8kzAZFVsQce5fK  0.3  --url devnet
[from] seed phrase: 
[from] If this seed phrase has an associated passphrase, enter it now. Otherwise, press ENTER to continue:  

The program will ask for a seed phrase that needs to be passed so that the program will continue and this can't be passed in the argument.

cmd := exec.Command(app, arg0, arg1, arg2, arg3,arg4,arg5,arg6)

    subStdin, err := cmd.StdinPipe()
    // check err
    defer subStdin.Close()
    io.WriteString(subStdin, "found post practice action leave spread impact chair skate coach kingdom coral")

after that progmram except one more input that I don't know hot to write it in go lang and continute the program

How to pass this particular value dynamically using go lang ?

package main

//solana transfer --from  prompt://?key=0/2 DE53G9MjqzX8WtexjdySsqogsTgGPD8kzAZFVsQce5fK  0.3  --url devnet

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

func main() {
    app := "../solana"

    arg0 := "transfer"
    arg1 := "--from"
    arg2 := "prompt://?key=0/2"
    arg3 := "DE53G9MjqzX8WtexjdySsqogsTgGPD8kzAZFVsQce5fK"
    arg4 := "0.3"
    arg5 := "--url"
    arg6 := "devnet"

    cmd := exec.Command(app, arg0, arg1, arg2, arg3,arg4,arg5,arg6)

    subStdin, err := cmd.StdinPipe()
    // check err
    defer subStdin.Close()
    io.WriteString(subStdin, "abx sd sda sd dsa sd sad sda dsa coach asd dds")
    var out bytes.Buffer
    var stderr bytes.Buffer
    cmd.Stdout = &out
    cmd.Stderr = &stderr
    err = cmd.Run()
    if err != nil {
        fmt.Println(fmt.Sprint(err)   ": "   stderr.String())
        return
    }
    fmt.Println("Result: "   out.String())
    stdout, err := cmd.Output()
    fmt.Println(string(stdout))
}

The go program which is written takes all the user input but exit in the user prompt any pointer will be great help here.

CodePudding user response:

Maybe flags could be useful -

package flag implements command-line flag parsing

Usage example

CodePudding user response:

You can us os.Args for that.

os.Args[1] is the first argument following the app (os.Args[0]) and so on

https://gobyexample.com/command-line-arguments

  •  Tags:  
  • Related