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:
akulij 2025-02-26 23:18:39 +00:00
parent 3b1079985a
commit 35648dad3d

View File

@ -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,25 +93,26 @@ 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
_, err = fmt.Fprintf(f, "# created: %s\n", time.Now().Format(time.RFC3339)) if verbose {
if err != nil { _, err = fmt.Fprintf(f, "# created: %s\n", time.Now().Format(time.RFC3339))
return err if err != nil {
return err
}
_, err = fmt.Fprintf(f, "# public key: %s\n", key.Recipient())
if err != nil {
return err
}
_, err = fmt.Fprintf(f, "%s\n", key)
} else {
_, err = fmt.Fprintf(f, "%s", key)
} }
_, err = fmt.Fprintf(f, "# public key: %s\n", key.Recipient()) return err
if err != nil {
return err
}
_, err = fmt.Fprintf(f, "%s\n", key)
if err != nil {
return err
}
return nil
} }
// almost a copy of private function in age/x25519.go // almost a copy of private function in age/x25519.go