Implement password generation on new entries

This commit is contained in:
2024-03-07 16:04:46 -05:00
parent 1982a5c0d6
commit 349fd807b5
2 changed files with 29 additions and 6 deletions
+12 -3
View File
@@ -7,7 +7,8 @@ import (
) )
// AddEntry creates a new entry at targetLocation by taking user input via CLI prompts // AddEntry creates a new entry at targetLocation by taking user input via CLI prompts
func AddEntry(targetLocation string, hidePassword bool, isNote bool) { // entryType: 0 = standard (password), 1 = auto-generated password, 2 = note
func AddEntry(targetLocation string, hidePassword bool, entryType uint8) {
_, 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)
@@ -16,9 +17,17 @@ func AddEntry(targetLocation string, hidePassword bool, isNote bool) {
var unencryptedEntry []string var unencryptedEntry []string
if !isNote { if entryType < 2 {
username := input("Username:") username := input("Username:")
password := inputHidden("Password:")
// determine whether to generate the password
var password string
if entryType == 0 {
password = inputHidden("Password:")
} else {
password = offline.StringGen(inputInt("Password length:"), inputBinary("Generate a complex (special characters) password?"), 0.2)
}
url := input("URL:") url := input("URL:")
if inputBinary("Add a note to this entry?") { if inputBinary("Add a note to this entry?") {
note := newNote() note := newNote()
+17 -3
View File
@@ -12,9 +12,9 @@ import (
// 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
func input(prompt string) string { func input(prompt string) string {
fmt.Print("\n" + prompt + " ") fmt.Print("\n" + prompt + " ")
var input string var userInput string
fmt.Scanln(&input) fmt.Scanln(&userInput)
return input return userInput
} }
// inputHidden prompts the user for input and returns the input as a string, hiding the input from the terminal // inputHidden prompts the user for input and returns the input as a string, hiding the input from the terminal
@@ -26,6 +26,20 @@ func inputHidden(prompt string) string {
return password return password
} }
// inputInt prompts the user for input and returns the input as an integer
func inputInt(prompt string) int {
// loop until a valid integer is entered
for {
fmt.Print("\n" + prompt + " ")
var userInput int
_, err := fmt.Scanln(&userInput)
if err == nil {
return userInput
}
}
}
// inputBinary prompts the user with a yes/no question and returns the response as a boolean // inputBinary prompts the user with a yes/no question and returns the response as a boolean
func inputBinary(prompt string) bool { func inputBinary(prompt string) bool {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)