Fix ANSI escapes (on Windows) not being interpreted after using GPG pinentry

This commit is contained in:
2024-07-17 18:54:29 -04:00
parent d5e07bdeb6
commit 7a48544932
4 changed files with 31 additions and 0 deletions
+6
View File
@@ -12,3 +12,9 @@ const (
PathSeparator = "/"
IsWindows = false
)
// enableVirtualTerminalProcessing is a dummy function on UNIX-like systems (only needed on Windows)
// TODO remove after migration off of GPG, as pinentry is responsible for disabling ANSI escape sequence interpretation
func enableVirtualTerminalProcessing() {
return
}
+17
View File
@@ -2,6 +2,11 @@
package backend
import (
"os"
"syscall"
)
// EntryRoot path to libmutton entry directory
var EntryRoot = Home + "\\AppData\\Local\\libmutton\\entries"
var ConfigDir = Home + "\\AppData\\Local\\libmutton\\config"
@@ -12,3 +17,15 @@ const (
PathSeparator = "\\"
IsWindows = true
)
// enableVirtualTerminalProcessing ensures ANSI escape sequences are interpreted properly on Windows
// TODO remove after migration off of GPG, as pinentry is responsible for disabling ANSI escape sequence interpretation
func enableVirtualTerminalProcessing() {
stdout := syscall.Handle(os.Stdout.Fd())
var originalMode uint32
syscall.GetConsoleMode(stdout, &originalMode)
originalMode |= 0x0004
syscall.MustLoadDLL("kernel32").MustFindProc("SetConsoleMode").Call(uintptr(stdout), uintptr(originalMode))
}
+5
View File
@@ -13,11 +13,16 @@ import (
func DecryptGPG(targetLocation string) []string {
cmd := exec.Command("gpg", "--pinentry-mode", "loopback", "-q", "-d", targetLocation)
output, err := cmd.Output()
// ensure ANSI escape sequences are interpreted properly on Windows
enableVirtualTerminalProcessing()
if err != nil {
fmt.Println(AnsiError + "Failed to decrypt \"" + targetLocation + "\" - ensure it is a valid GPG-encrypted file and that you entered your passphrase correctly" + AnsiReset)
os.Exit(1)
}
outputSlice := strings.Split(string(output), "\n")
return outputSlice
}