Allow picking GPG key ID from a generated list

This commit is contained in:
2024-03-07 22:48:57 -05:00
parent c6ee5fff06
commit 7d17af9b61
3 changed files with 32 additions and 8 deletions
+5 -6
View File
@@ -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\"):")
+10 -2
View File
@@ -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) {
+17
View File
@@ -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
}