1

I need a way to read input from console twice (1 -from cat outupt, 2 - user inputs password), like this: cat ./test_data/test.txt | app account import

My current code skips password input:

reader := bufio.NewReader(os.Stdin)
raw, err := ioutil.ReadAll(reader)
if err != nil {
    return cli.NewExitError(err.Error(), 1)
}

wlt, err := wallet.Deserialize(string(raw))
if err != nil {
    return cli.NewExitError(err.Error(), 1)
}

fmt.Print("Enter password: ")
pass := ""
fmt.Fscanln(reader, &pass)

Also tried to read password with Scanln - doesn't works.

Note: cat (and piping at all) can't be used with user input, as shell redirects inputs totally. So the most simple solutions are:

  • to pass filename as argument

  • redirect manually app account import < ./test.txt

1 Answer 1

1

Read file and password separately (and don't show password), try this:

package main

import (
    "fmt"
    "io/ioutil"
    "log"

    "github.com/howeyc/gopass"
)

func main() {
    b, err := ioutil.ReadFile("./test_data/test.txt") // just pass the file name
    if err != nil {
        fmt.Print(err)
    }
    fmt.Println(string(b))

    fmt.Print("Password: ")
    pass, err := gopass.GetPasswd()
    if err != nil {
        log.Fatalln(err)
    }
    fmt.Println(string(pass))
}

and go get github.com/howeyc/gopass first.

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

4 Comments

Yeap, I already used terminal.ReadPassword(0) to hide password. But I need option to use my app in a shell pipeline
OK so you need to read from std in , then how do you separate file data from password? it is one input stream, isn't it? when reading from one stream you need to detect fist data end and second data start, meaning sync header. may be you write a second program like cat which reads from one file and ask password from std in then send output to std out using formatted frame and sync. I hope this helps.
If you're reading the password from the terminal, it looks like everything is working. Do you still have a problem?
I forget that while piping, there is no way to interrupt stdin to input password from keyboard. So maybe the option for is just to use filename

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.