mirror of
https://github.com/rwinkhart/MUTN.git
synced 2026-08-28 04:46:41 -04:00
Manually select auto-generated GPG key due to inconsistent GPG output, use tempfile for gpg-gen
This commit is contained in:
+6
-5
@@ -9,13 +9,14 @@ func TempInitCli() {
|
||||
// gpgID
|
||||
var gpgID string
|
||||
if inputBinary("Auto-generate GPG key?") {
|
||||
gpgID = offline.GpgKeyGen()
|
||||
} else {
|
||||
uidSlice := offline.GpgUIDListGen()
|
||||
gpgIDInt := inputMenuGen("Select GPG key:", uidSlice)
|
||||
gpgID = uidSlice[gpgIDInt-1]
|
||||
offline.GpgKeyGen()
|
||||
}
|
||||
|
||||
// select 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 \"" + offline.FallbackEditor + "\"):")
|
||||
|
||||
|
||||
+10
-21
@@ -14,13 +14,8 @@ func TempInit(configFileMap map[string]string) {
|
||||
// remove existing config file
|
||||
removeFile(ConfigPath)
|
||||
|
||||
// ensure textEditor is set
|
||||
if configFileMap["textEditor"] == "" {
|
||||
textEditor, editorEnvPresent := os.LookupEnv("EDITOR")
|
||||
if !editorEnvPresent {
|
||||
textEditor = FallbackEditor
|
||||
}
|
||||
configFileMap["textEditor"] = textEditor
|
||||
configFileMap["textEditor"] = textEditorFallback()
|
||||
}
|
||||
|
||||
// create and write config file
|
||||
@@ -49,24 +44,18 @@ func GpgUIDListGen() []string {
|
||||
return uidSlice
|
||||
}
|
||||
|
||||
// GpgKeyGen generates a new GPG key and returns the key ID as a string
|
||||
func GpgKeyGen() string {
|
||||
// GpgKeyGen generates a new GPG key - it does not return it due to GPG's unreliable output
|
||||
func GpgKeyGen() {
|
||||
gpgGenTempFile := CreateTempFile()
|
||||
defer os.Remove(gpgGenTempFile.Name())
|
||||
|
||||
// create and write gpg-gen file
|
||||
createFile(ConfigDir+"/gpg-gen", []string{"Key-Type: eddsa", "Key-Curve: ed25519", "Key-Usage: sign", "Subkey-Type: ecdh", "Subkey-Curve: cv25519", "Subkey-Usage: encrypt", "Name-Real: libmutton", "Name-Comment: gpg-libmutton", "Name-Email: github.com/rwinkhart/libmutton", "Expire-Date: 0"})
|
||||
cmd := exec.Command("gpg", "-q", "--batch", "--generate-key", ConfigDir+"/gpg-gen")
|
||||
gpgGenTempFile.WriteString(strings.Join([]string{"Key-Type: eddsa", "Key-Curve: ed25519", "Key-Usage: sign", "Subkey-Type: ecdh", "Subkey-Curve: cv25519", "Subkey-Usage: encrypt", "Name-Real: libmutton", "Name-Comment: gpg-libmutton", "Name-Email: github.com/rwinkhart/libmutton", "Expire-Date: 0"}, "\n"))
|
||||
|
||||
// generate GPG key based on gpg-gen file
|
||||
cmd := exec.Command("gpg", "-q", "--batch", "--generate-key", gpgGenTempFile.Name())
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Run()
|
||||
|
||||
// generate GPG key based on gog-gen file
|
||||
cmd = exec.Command("gpg", "-k", "--with-colons")
|
||||
gpgOutputBytes, _ := cmd.Output()
|
||||
gpgOutputLines := strings.Split(string(gpgOutputBytes), "\n")
|
||||
uid := strings.Split(gpgOutputLines[len(gpgOutputLines)-4], ":")[9]
|
||||
|
||||
// remove gpg-gen file
|
||||
removeFile(ConfigDir + "/gpg-gen")
|
||||
|
||||
return uid
|
||||
}
|
||||
|
||||
@@ -25,3 +25,13 @@ func dirInit() {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// textEditorFallback returns the value of the $EDITOR environment variable, or FallbackEditor if it is not set
|
||||
func textEditorFallback() string {
|
||||
// ensure textEditor is set
|
||||
textEditor := os.Getenv("EDITOR")
|
||||
if textEditor == "" {
|
||||
textEditor = FallbackEditor
|
||||
}
|
||||
return textEditor
|
||||
}
|
||||
|
||||
@@ -18,3 +18,8 @@ func dirInit() {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// textEditorFallback returns FallbackEditor
|
||||
func textEditorFallback() string {
|
||||
return FallbackEditor
|
||||
}
|
||||
|
||||
@@ -68,6 +68,18 @@ func CreateTempFile() *os.File {
|
||||
return tempFile
|
||||
}
|
||||
|
||||
// removeFile removes a file at targetLocation and does not error if the file does not exist
|
||||
func removeFile(targetLocation string) {
|
||||
// remove existing config file
|
||||
err := os.Remove(targetLocation)
|
||||
if err != nil {
|
||||
// ignore error if file does not exist
|
||||
if !os.IsNotExist(err) {
|
||||
fmt.Println(AnsiError + "Failed to remove \"" + targetLocation + "\":" + err.Error() + AnsiReset)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveTrailingEmptyStrings removes empty strings from the end of a slice
|
||||
func RemoveTrailingEmptyStrings(slice []string) []string {
|
||||
for i := len(slice) - 1; i >= 0; i-- {
|
||||
@@ -135,27 +147,3 @@ func EntryIsNotEmpty(entryData []string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// removeFile removes a file at targetLocation and does not error if the file does not exist
|
||||
func removeFile(targetLocation string) {
|
||||
// remove existing config file
|
||||
err := os.Remove(targetLocation)
|
||||
if err != nil {
|
||||
// ignore error if file does not exist
|
||||
if !os.IsNotExist(err) {
|
||||
fmt.Println(AnsiError + "Failed to remove \"" + targetLocation + "\":" + err.Error() + AnsiReset)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// createFile creates and writes a file at targetLocation with fileData - it removes the file if it already exists
|
||||
func createFile(targetLocation string, fileData []string) {
|
||||
removeFile(targetLocation)
|
||||
file, err := os.OpenFile(targetLocation, os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError + "Failed to create \"" + targetLocation + "\":" + err.Error() + AnsiReset)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer file.Close()
|
||||
file.WriteString(strings.Join(fileData, "\n"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user