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,
// also duplicate public key it to stderr,
// but if user sees public key via stdout, no need for duplication
if !term.IsTerminal(int(os.Stdout.Fd())) {
fmt.Fprintf(os.Stderr, "Public key: %s\n", k.Recipient())
if outputFile != "" {
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 {
fmt.Printf("Failed to write secret key to file, error: %s\n", err)
}
@ -72,7 +84,7 @@ func main() {
func getPasswordBytes() ([]byte, error) {
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()))
fmt.Println()
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
if verbose {
_, err = fmt.Fprintf(f, "# created: %s\n", time.Now().Format(time.RFC3339))
if err != nil {
return err
@ -95,11 +108,11 @@ func writeSecretKey(f *os.File, key *age.X25519Identity) error {
}
_, err = fmt.Fprintf(f, "%s\n", key)
if err != nil {
return err
} else {
_, err = fmt.Fprintf(f, "%s", key)
}
return nil
return err
}
// almost a copy of private function in age/x25519.go