From 0afcdbb29b93efe027518cdac95aae66f88154d7 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Fri, 1 Mar 2024 18:26:05 -0500 Subject: [PATCH] Implement initial entry readout formatting --- main.go | 3 +-- src/cli/entryReader.go | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/cli/entryReader.go diff --git a/main.go b/main.go index 9054750..d29a36c 100644 --- a/main.go +++ b/main.go @@ -23,8 +23,7 @@ func main() { if strings.HasPrefix(args[0], "/") { targetLocation := offline.EntryRoot + offline.PathSeparator + args[0][1:] if isFile, _ := offline.TargetIsFile(targetLocation, true); isFile { - // TODO properly display entry contents (with headers) - offline.DecryptGPG(targetLocation) + cli.EntryReader(offline.DecryptGPG(targetLocation)) } else { fmt.Println(offline.AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + offline.AnsiReset) } diff --git a/src/cli/entryReader.go b/src/cli/entryReader.go new file mode 100644 index 0000000..d0face5 --- /dev/null +++ b/src/cli/entryReader.go @@ -0,0 +1,57 @@ +package cli + +import ( + "fmt" + "github.com/rwinkhart/MUTN/src/offline" +) + +// EntryReader prints the decrypted contents of a libmutton entry in a human-readable format +func EntryReader(decryptedEntry []string) { + fmt.Println() + + // track if extended notes have been printed (to avoid printing an extra newline) + notesFlag := false + + for i := range decryptedEntry { + + // if the entry only contains a password, skip the username field to avoid an index out of range error + if len(decryptedEntry) == 1 { + i = 1 + } + + switch i { + case 0: + // if the second field (username) is not empty, print it + if decryptedEntry[1] != "" { + fmt.Print(ansiDirectoryHeader + "Username:" + offline.AnsiReset + "\n" + decryptedEntry[1] + "\n\n") + } + case 1: + // if the first field (password) is not empty, print it + if decryptedEntry[0] != "" { + fmt.Print(ansiDirectoryHeader + "Password:" + offline.AnsiReset + "\n" + decryptedEntry[0] + "\n\n") + } + case 2: + // if the third field (url) is not empty, print it + if decryptedEntry[2] != "" { + fmt.Print(ansiDirectoryHeader + "URL:" + offline.AnsiReset + "\n" + decryptedEntry[2] + "\n\n") + } + case 3: + // if the fourth field (notes begin) is not empty, print it + if decryptedEntry[3] != "" { + fmt.Println(ansiDirectoryHeader + "Notes:" + offline.AnsiReset + "\n" + decryptedEntry[3]) + } + default: + // print extended notes line + fmt.Println(decryptedEntry[i]) + + // indicate that extended notes have been printed + if !notesFlag { + notesFlag = true + } + } + } + // print trailing newline if extended notes were printed + if notesFlag { + fmt.Println() + } +}