Implement notes editor (required rewrite of offline.ReadConfig)

This commit is contained in:
2024-03-05 17:11:53 -05:00
parent f58c789c60
commit 9606bf6af6
3 changed files with 64 additions and 17 deletions
+15 -8
View File
@@ -6,21 +6,28 @@ import (
"os" "os"
) )
// AddPasswordEntry prompts the user for all information relevant to a password entry and writes the entry to an encrypted file at targetLocation // AddEntry creates a new entry at targetLocation by taking user input via CLI prompts
func AddPasswordEntry(targetLocation string, hidePassword bool) { func AddEntry(targetLocation string, hidePassword bool, isNote bool) {
_, isAccessible := offline.TargetIsFile(targetLocation, false, 0) _, isAccessible := offline.TargetIsFile(targetLocation, false, 0)
if isAccessible { if isAccessible {
fmt.Println(offline.AnsiError + "Target location already exists" + offline.AnsiReset) fmt.Println(offline.AnsiError + "Target location already exists" + offline.AnsiReset)
os.Exit(1) os.Exit(1)
} }
username := input("Username: ") var unencryptedEntry []string
password := inputHidden("Password: ")
url := input("URL: ") if !isNote {
// TODO implement notes editor username := input("Username: ")
notes := "" password := inputHidden("Password: ")
url := input("URL: ")
// TODO prompt for optional note
unencryptedEntry = []string{password, username, url}
} else {
note := newNote()
unencryptedEntry = append([]string{"", "", ""}, note...)
fmt.Println()
}
unencryptedEntry := []string{password, username, url, notes}
offline.WriteEntry(targetLocation, unencryptedEntry) offline.WriteEntry(targetLocation, unencryptedEntry)
fmt.Println(ansiBold + "\nEntry Preview:" + offline.AnsiReset) fmt.Println(ansiBold + "\nEntry Preview:" + offline.AnsiReset)
EntryReader(unencryptedEntry, hidePassword) EntryReader(unencryptedEntry, hidePassword)
+15 -9
View File
@@ -13,7 +13,7 @@ func EntryReader(decryptedEntry []string, hidePassword bool) {
fmt.Println() fmt.Println()
// track if extended notes have been printed (to avoid printing an extra newline) // track if extended notes have been printed (to avoid printing an extra newline)
notesFlag := false var notesFlag bool
for i := range decryptedEntry { for i := range decryptedEntry {
@@ -46,26 +46,32 @@ func EntryReader(decryptedEntry []string, hidePassword bool) {
// if the fourth field (notes begin) is not empty, print it // if the fourth field (notes begin) is not empty, print it
if decryptedEntry[3] != "" { if decryptedEntry[3] != "" {
fmt.Println(ansiDirectoryHeader + "Notes:" + offline.AnsiReset + "\n" + decryptedEntry[3]) fmt.Println(ansiDirectoryHeader + "Notes:" + offline.AnsiReset + "\n" + decryptedEntry[3])
} // indicate that the first notes line was printed (not empty)
default:
// print extended notes line
fmt.Println(decryptedEntry[i])
// indicate that extended notes have been printed
if !notesFlag {
notesFlag = true notesFlag = true
} }
default:
// print notes header if first notes line was blank
if !notesFlag {
fmt.Println(ansiDirectoryHeader + "Notes:" + offline.AnsiReset)
// indicate that notes have been printed
notesFlag = true
}
// print extended notes line
fmt.Println(decryptedEntry[i])
} }
} }
// print trailing newline if extended notes were printed // print trailing newline if notes were printed
if notesFlag { if notesFlag {
fmt.Println() fmt.Println()
} }
os.Exit(0) os.Exit(0)
} }
// EntryReaderShortcut is a shortcut for EntryReader that decrypts a GPG-encrypted file and prints the contents
func EntryReaderShortcut(targetLocation string, hidePassword bool) { func EntryReaderShortcut(targetLocation string, hidePassword bool) {
if isFile, _ := offline.TargetIsFile(targetLocation, true, 2); isFile { if isFile, _ := offline.TargetIsFile(targetLocation, true, 2); isFile {
EntryReader(offline.DecryptGPG(targetLocation), hidePassword) EntryReader(offline.DecryptGPG(targetLocation), hidePassword)
} }
// do not exit, as this is the job of EntryReader
} }
+34
View File
@@ -1,9 +1,12 @@
package cli package cli
import ( import (
"bufio"
"fmt" "fmt"
"github.com/rwinkhart/MUTN/src/offline"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
"os" "os"
"os/exec"
) )
// input prompts the user for input and returns the input as a string // input prompts the user for input and returns the input as a string
@@ -22,3 +25,34 @@ func inputHidden(prompt string) string {
fmt.Println() fmt.Println()
return password return password
} }
// newNote uses the user-specified text editor to create a new note and returns the note as a slice of strings
func newNote() []string {
tempFile := offline.CreateTempFile()
defer os.Remove(tempFile.Name())
editor := offline.ReadConfig([]string{"textEditor"})[0]
cmd := exec.Command(editor, tempFile.Name())
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println(offline.AnsiError + "Failed to write note with " + editor + offline.AnsiReset)
os.Exit(1)
}
file, err := os.Open(tempFile.Name())
if err != nil {
fmt.Println(offline.AnsiError + "Failed to open temporary file (\"" + tempFile.Name() + "\") " + err.Error() + offline.AnsiReset)
os.Exit(1)
}
defer file.Close()
var note []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
note = append(note, scanner.Text())
}
return offline.RemoveTrailingEmptyStrings(note)
}