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) { diff --git a/src/offline/init.go b/src/offline/init.go index 0d500ee..46f1ec1 100644 --- a/src/offline/init.go +++ b/src/offline/init.go @@ -3,6 +3,8 @@ package offline import ( "fmt" "os" + "os/exec" + "strings" ) func TempInit(configFileMap map[string]string) { @@ -37,3 +39,18 @@ func TempInit(configFileMap map[string]string) { os.Exit(0) } + +// GpgUIDListGen generates a list of all GPG key IDs on the system and returns them as a slice of strings +func GpgUIDListGen() []string { + cmd := exec.Command("gpg", "-k", "--with-colons") + gpgOutputBytes, _ := cmd.Output() + gpgOutputLines := strings.Split(string(gpgOutputBytes), "\n") + var uidSlice []string + for _, line := range gpgOutputLines { + if strings.HasPrefix(line, "uid") { + uid := strings.Split(line, ":")[9] + uidSlice = append(uidSlice, uid) + } + } + return uidSlice +}