From f58c789c60d831952226cdb8e9717461bd581627 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Tue, 5 Mar 2024 14:09:50 -0500 Subject: [PATCH] Implement initial entry adding (partial, missing notes support) --- src/cli/add.go | 27 +++++++++++++++++++++++++++ src/cli/utilitiesMisc.go | 24 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/cli/add.go create mode 100644 src/cli/utilitiesMisc.go diff --git a/src/cli/add.go b/src/cli/add.go new file mode 100644 index 0000000..8de1c5f --- /dev/null +++ b/src/cli/add.go @@ -0,0 +1,27 @@ +package cli + +import ( + "fmt" + "github.com/rwinkhart/MUTN/src/offline" + "os" +) + +// AddPasswordEntry prompts the user for all information relevant to a password entry and writes the entry to an encrypted file at targetLocation +func AddPasswordEntry(targetLocation string, hidePassword bool) { + _, isAccessible := offline.TargetIsFile(targetLocation, false, 0) + if isAccessible { + fmt.Println(offline.AnsiError + "Target location already exists" + offline.AnsiReset) + os.Exit(1) + } + + username := input("Username: ") + password := inputHidden("Password: ") + url := input("URL: ") + // TODO implement notes editor + notes := "" + + unencryptedEntry := []string{password, username, url, notes} + offline.WriteEntry(targetLocation, unencryptedEntry) + fmt.Println(ansiBold + "\nEntry Preview:" + offline.AnsiReset) + EntryReader(unencryptedEntry, hidePassword) +} diff --git a/src/cli/utilitiesMisc.go b/src/cli/utilitiesMisc.go new file mode 100644 index 0000000..41f53f0 --- /dev/null +++ b/src/cli/utilitiesMisc.go @@ -0,0 +1,24 @@ +package cli + +import ( + "fmt" + "golang.org/x/crypto/ssh/terminal" + "os" +) + +// input prompts the user for input and returns the input as a string +func input(prompt string) string { + fmt.Print("\n" + prompt) + var input string + fmt.Scanln(&input) + return input +} + +// inputHidden prompts the user for input and returns the input as a string, hiding the input from the terminal +func inputHidden(prompt string) string { + fmt.Print("\n" + prompt) + byteInput, _ := terminal.ReadPassword(int(os.Stdin.Fd())) + password := string(byteInput) + fmt.Println() + return password +}