use flags --output and --raw-output
Now logic for output looks like: password from stdin (stays as was) keys: private to stdout OR private to file (via -o flag) and public to stdout stderr for errors and password prompt
This commit is contained in:
parent
3b1079985a
commit
14439a6479
@ -60,11 +60,23 @@ func main() {
|
|||||||
// if user is not seeing private keyfile, which also contains public key,
|
// if user is not seeing private keyfile, which also contains public key,
|
||||||
// also duplicate public key it to stderr,
|
// also duplicate public key it to stderr,
|
||||||
// but if user sees public key via stdout, no need for duplication
|
// but if user sees public key via stdout, no need for duplication
|
||||||
if !term.IsTerminal(int(os.Stdout.Fd())) {
|
if outputFile != "" {
|
||||||
fmt.Fprintf(os.Stderr, "Public key: %s\n", k.Recipient())
|
if !rawOutput {
|
||||||
|
fmt.Printf("Public key: %s\n", k.Recipient())
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%s", k.Recipient())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = writeSecretKey(os.Stdout, k)
|
output := os.Stdout
|
||||||
|
if outputFile != "" {
|
||||||
|
output, err = os.Create(outputFile)
|
||||||
|
if err != nil {
|
||||||
|
errorf("failed to create output file, error: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = writeSecretKey(output, k, !rawOutput)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to write secret key to file, error: %s\n", err)
|
fmt.Printf("Failed to write secret key to file, error: %s\n", err)
|
||||||
}
|
}
|
||||||
@ -72,7 +84,7 @@ func main() {
|
|||||||
|
|
||||||
func getPasswordBytes() ([]byte, error) {
|
func getPasswordBytes() ([]byte, error) {
|
||||||
if term.IsTerminal(int(os.Stdin.Fd())) {
|
if term.IsTerminal(int(os.Stdin.Fd())) {
|
||||||
fmt.Print("Enter password: ")
|
fmt.Fprintf(os.Stderr, "Enter password: ")
|
||||||
passbytes, err := term.ReadPassword(int(os.Stdin.Fd()))
|
passbytes, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
return passbytes, err
|
return passbytes, err
|
||||||
@ -81,9 +93,10 @@ func getPasswordBytes() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeSecretKey(f *os.File, key *age.X25519Identity) error {
|
func writeSecretKey(f *os.File, key *age.X25519Identity, verbose bool) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
if verbose {
|
||||||
_, err = fmt.Fprintf(f, "# created: %s\n", time.Now().Format(time.RFC3339))
|
_, err = fmt.Fprintf(f, "# created: %s\n", time.Now().Format(time.RFC3339))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -95,11 +108,11 @@ func writeSecretKey(f *os.File, key *age.X25519Identity) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_, err = fmt.Fprintf(f, "%s\n", key)
|
_, err = fmt.Fprintf(f, "%s\n", key)
|
||||||
if err != nil {
|
} else {
|
||||||
return err
|
_, err = fmt.Fprintf(f, "%s", key)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// almost a copy of private function in age/x25519.go
|
// almost a copy of private function in age/x25519.go
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user