Implement note editing

This commit is contained in:
2024-03-07 20:15:01 -05:00
parent 3a90bef48a
commit 22758460ab
7 changed files with 141 additions and 84 deletions
+39 -20
View File
@@ -76,32 +76,31 @@ func main() {
field = 1
case "url", "-l":
field = 2
case "note", "-n": // TODO cli.EditNote(targetLocation), exit after run
case "note", "-n":
if argsCount == 4 {
cli.EditEntryNote(targetLocation, true)
} else {
switch args[4] {
case "show", "-s":
cli.EditEntryNote(targetLocation, false)
default:
cli.EditEntryNote(targetLocation, true)
}
}
case "rename", "-r":
cli.RenameCli(targetLocation)
default:
cli.HelpEdit()
}
cli.EditEntry(targetLocation, true, field)
case "add":
switch args[3] {
case "password", "-p":
if argsCount == 4 {
cli.AddEntry(targetLocation, true, 0)
} else {
switch args[4] {
case "show", "-s":
cli.AddEntry(targetLocation, false, 0)
default:
cli.AddEntry(targetLocation, true, 0)
}
if argsCount == 4 {
cli.EditEntry(targetLocation, true, field)
} else {
switch args[4] {
case "show", "-s":
cli.EditEntry(targetLocation, false, field)
default:
cli.EditEntry(targetLocation, true, field)
}
case "note", "-n":
cli.AddEntry(targetLocation, true, 2)
case "folder", "-f":
offline.AddFolder(targetLocation)
default:
cli.HelpAdd()
}
case "gen":
if argsCount == 4 {
@@ -122,6 +121,26 @@ func main() {
}
}
cli.HelpGen()
case "add":
switch args[3] {
case "password", "-p":
if argsCount == 4 {
cli.AddEntry(targetLocation, true, 0)
} else {
switch args[4] {
case "show", "-s":
cli.AddEntry(targetLocation, false, 0)
default:
cli.AddEntry(targetLocation, true, 0)
}
}
case "note", "-n":
cli.AddEntry(targetLocation, true, 2)
case "folder", "-f":
offline.AddFolder(targetLocation)
default:
cli.HelpAdd()
}
default:
cli.HelpMain()
}
+3 -10
View File
@@ -30,23 +30,16 @@ func AddEntry(targetLocation string, hidePassword bool, entryType uint8) {
url := input("URL:")
if inputBinary("Add a note to this entry?") {
note := newNote()
note, _ := editNote([]string{})
unencryptedEntry = append([]string{password, username, url}, note...)
} else {
unencryptedEntry = []string{password, username, url}
}
} else {
note := newNote()
note, _ := editNote([]string{})
unencryptedEntry = append([]string{"", "", ""}, note...)
}
// write and preview the new entry
if offline.EntryIsNotEmpty(unencryptedEntry) {
offline.WriteEntry(targetLocation, unencryptedEntry)
fmt.Println(ansiBold + "\nEntry Preview:" + offline.AnsiReset)
EntryReader(unencryptedEntry, hidePassword)
} else {
fmt.Println(offline.AnsiError + "No data supplied for entry" + offline.AnsiReset)
os.Exit(1)
}
writeEntryShortcut(targetLocation, unencryptedEntry, hidePassword)
}
+74 -12
View File
@@ -1,9 +1,12 @@
package cli
import (
"bufio"
"fmt"
"github.com/rwinkhart/MUTN/src/offline"
"os"
"os/exec"
"reflect"
)
// RenameCli renames an entry at oldLocation to a new location (user input)
@@ -42,14 +45,32 @@ func EditEntry(targetLocation string, hidePassword bool, field int) {
}
// write and preview the modified entry
if offline.EntryIsNotEmpty(unencryptedEntry) {
offline.WriteEntry(targetLocation, unencryptedEntry)
fmt.Println(ansiBold + "\nEntry Preview:" + offline.AnsiReset)
EntryReader(unencryptedEntry, hidePassword)
} else {
fmt.Println(offline.AnsiError + "No data supplied for entry" + offline.AnsiReset)
writeEntryShortcut(targetLocation, unencryptedEntry, hidePassword)
}
func EditEntryNote(targetLocation string, hidePassword bool) {
// ensure targetLocation exists
offline.TargetIsFile(targetLocation, true, 2)
// read old entry data
unencryptedEntry := offline.DecryptGPG(targetLocation)
// store non-note data separately
nonNoteData := unencryptedEntry[:3]
// store note data separately
noteData := unencryptedEntry[3:]
// edit the note
editedNote, noteEdited := editNote(noteData)
if !noteEdited { // exit early if the note was not edited
fmt.Println(offline.AnsiError + "Entry is unchanged" + offline.AnsiReset)
os.Exit(1)
}
unencryptedEntry = append(nonNoteData, editedNote...)
// write and preview the modified entry
writeEntryShortcut(targetLocation, unencryptedEntry, hidePassword)
}
// GenUpdate generates a new password for an entry at targetLocation (user input)
@@ -64,12 +85,53 @@ func GenUpdate(targetLocation string, hidePassword bool) {
unencryptedEntry[0] = offline.StringGen(inputInt("Password length:"), inputBinary("Generate a complex (special characters) password?"), 0.2)
// write and preview the modified entry
if offline.EntryIsNotEmpty(unencryptedEntry) {
offline.WriteEntry(targetLocation, unencryptedEntry)
fmt.Println(ansiBold + "\nEntry Preview:" + offline.AnsiReset)
EntryReader(unencryptedEntry, hidePassword)
} else {
fmt.Println(offline.AnsiError + "No data supplied for entry" + offline.AnsiReset)
writeEntryShortcut(targetLocation, unencryptedEntry, hidePassword)
}
// editNote uses the user-specified text editor to edit an existing note (or create a new one if baseNote is empty)
// returns the edited note and a boolean indicating whether the note was edited
func editNote(baseNote []string) ([]string, bool) {
tempFile := offline.CreateTempFile()
defer os.Remove(tempFile.Name())
editor := offline.ReadConfig([]string{"textEditor"})[0]
// write baseNote to tempFile (if it is not empty)
if len(baseNote) > 0 {
for _, line := range baseNote {
_, _ = tempFile.WriteString(line + "\n")
}
}
// edit the tempFile (note) with the user's text editor
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)
}
// TODO does it need to be reopened?
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()
// read the edited note from the tempFile
var note []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
note = append(note, scanner.Text())
}
// return the edited note if it is different from baseNote
if !reflect.DeepEqual(offline.RemoveTrailingEmptyStrings(note), baseNote) {
return note, true
} else {
return note, false
}
}
+6 -4
View File
@@ -50,15 +50,17 @@ func EntryReader(decryptedEntry []string, hidePassword bool) {
notesFlag = true
}
default:
// print notes header if first notes line was blank
if !notesFlag {
// print notes header if current line is not empty and notes have not been printed
if !notesFlag && decryptedEntry[i] != "" {
fmt.Println(ansiDirectoryHeader + "Notes:" + offline.AnsiReset)
// indicate that notes have been printed
notesFlag = true
}
// print extended notes line
fmt.Println(decryptedEntry[i])
// print extended notes line if header was printed
if notesFlag {
fmt.Println(decryptedEntry[i])
}
}
}
// print trailing newline if notes were printed
+9 -28
View File
@@ -6,7 +6,6 @@ import (
"github.com/rwinkhart/MUTN/src/offline"
"golang.org/x/crypto/ssh/terminal"
"os"
"os/exec"
"strings"
)
@@ -51,33 +50,15 @@ func inputBinary(prompt string) bool {
return false
}
// 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)
// 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) {
trimmedEntry := offline.RemoveTrailingEmptyStrings(unencryptedEntry)
offline.WriteEntry(targetLocation, trimmedEntry)
fmt.Println(ansiBold + "\nEntry Preview:" + offline.AnsiReset)
EntryReader(trimmedEntry, hidePassword)
} else {
fmt.Println(offline.AnsiError + "No data supplied for entry" + 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)
}
-10
View File
@@ -21,13 +21,3 @@ func AddFolder(targetLocation string) {
// TODO If in online mode, create the directory on the server
os.Exit(0)
}
// WriteEntry writes entryData to an encrypted file at targetLocation
func WriteEntry(targetLocation string, entryData []string) {
encryptedBytes := EncryptGPG(entryData)
err := os.WriteFile(targetLocation, encryptedBytes, 0600)
if err != nil {
fmt.Println(AnsiError + "Failed to write to file: " + err.Error() + AnsiReset)
os.Exit(1)
}
}
+10
View File
@@ -39,6 +39,16 @@ func TargetIsFile(targetLocation string, errorOnFail bool, failCondition uint8)
}
}
// WriteEntry writes entryData to an encrypted file at targetLocation
func WriteEntry(targetLocation string, entryData []string) {
encryptedBytes := EncryptGPG(entryData)
err := os.WriteFile(targetLocation, encryptedBytes, 0600)
if err != nil {
fmt.Println(AnsiError + "Failed to write to file: " + err.Error() + AnsiReset)
os.Exit(1)
}
}
// writeToStdin writes a string to a command's stdin
func writeToStdin(cmd *exec.Cmd, input string) {
stdin, _ := cmd.StdinPipe()