From 7fc1c46018de5038b1b64e07f1b76954381d8c70 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Thu, 7 Mar 2024 22:48:57 -0500 Subject: [PATCH] Allow picking GPG key ID from a generated list --- src/cli/init.go | 11 +++++------ src/cli/utilitiesMisc.go | 12 ++++++++++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/cli/init.go b/src/cli/init.go index 93fa6c2..e345c83 100644 --- a/src/cli/init.go +++ b/src/cli/init.go @@ -2,16 +2,15 @@ package cli import ( "github.com/rwinkhart/MUTN/src/offline" - "os" - "os/exec" ) func TempInitCli() { // gpgID - cmd := exec.Command("gpg", "-k") - cmd.Stdout = os.Stdout - cmd.Run() - gpgID := input("GPG key ID:") + //generateGPG := inputBinary("Auto-generate GPG key?") + + uidSlice := offline.GpgUIDListGen() + gpgIDInt := inputMenuGen("Select GPG key:", uidSlice) + gpgID := uidSlice[gpgIDInt-1] // textEditor textEditor := input("Text editor (leave blank for $EDITOR, falls back to \"vi\"):") diff --git a/src/cli/utilitiesMisc.go b/src/cli/utilitiesMisc.go index 3eb8c56..8764c12 100644 --- a/src/cli/utilitiesMisc.go +++ b/src/cli/utilitiesMisc.go @@ -34,14 +34,14 @@ func inputHidden(prompt string) string { return password } -// inputInt prompts the user for input and returns the input as an integer +// inputInt prompts the user for input and returns the input as an integer (0 is not a valid input) func inputInt(prompt string) int { // loop until a valid integer is entered for { fmt.Print("\n" + prompt + " ") var userInput int _, err := fmt.Scanln(&userInput) - if err == nil { + if err == nil && userInput > 0 { return userInput } } @@ -58,6 +58,14 @@ func inputBinary(prompt string) bool { return false } +// inputMenu prompts the user with a menu and returns the user's choice as an integer +func inputMenuGen(prompt string, options []string) int { + for i, option := range options { + fmt.Printf("%d. %s\n", i+1, option) + } + return inputInt(prompt) +} + // writeEntryShortcut writes an entry to targetLocation (trimming trailing blank lines) and previews it, or errors if no data is supplied func writeEntryShortcut(targetLocation string, unencryptedEntry []string, hidePassword bool) { if offline.EntryIsNotEmpty(unencryptedEntry) {