mirror of
https://github.com/rwinkhart/MUTN.git
synced 2026-08-28 12:56:30 -04:00
85 lines
2.7 KiB
Go
85 lines
2.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/rwinkhart/MUTN/src/backend"
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
)
|
|
|
|
// input prompts the user for input and returns the input as a string
|
|
func input(prompt string) string {
|
|
fmt.Print("\n" + prompt + " ")
|
|
reader := bufio.NewReader(os.Stdin)
|
|
userInput, _ := reader.ReadString('\n')
|
|
return strings.TrimRight(userInput, "\n\r ") // remove trailing newlines, carriage returns, and spaces
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// inputInt prompts the user for input and returns the input as an integer
|
|
// a maxValue of 0 will cause the function to return 0, an error - a negative maxValue will disable the maxValue check
|
|
func inputInt(prompt string, maxValue int) int {
|
|
if maxValue == 0 {
|
|
return 0
|
|
}
|
|
|
|
// loop until a valid integer is entered
|
|
for {
|
|
fmt.Print("\n" + prompt + " ")
|
|
var userInput int
|
|
_, err := fmt.Scanln(&userInput)
|
|
if err == nil && userInput > 0 && (userInput <= maxValue || maxValue < 0) {
|
|
return userInput
|
|
}
|
|
}
|
|
}
|
|
|
|
// inputBinary prompts the user with a yes/no question and returns the response as a boolean
|
|
func inputBinary(prompt string) bool {
|
|
reader := bufio.NewReader(os.Stdin)
|
|
fmt.Print("\n" + prompt + " (y/N) ")
|
|
char, _, _ := reader.ReadRune()
|
|
if char == 'y' || char == 'Y' {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// inputMenuGen prompts the user with a menu and returns the user's choice as an integer
|
|
func inputMenuGen(prompt string, options []string) int {
|
|
for i, option := range options {
|
|
fmt.Printf("%d. %s\n", i+1, option)
|
|
}
|
|
return inputInt(prompt, len(options))
|
|
}
|
|
|
|
// writeEntryCLI writes an entry to targetLocation and previews it (errors if no data is supplied)
|
|
func writeEntryCLI(targetLocation string, unencryptedEntry []string, hideSecrets, verifyEntryDoesNotExist bool) {
|
|
if backend.EntryIsNotEmpty(unencryptedEntry) {
|
|
// write the entry to the target location
|
|
backend.WriteEntry(targetLocation, unencryptedEntry, verifyEntryDoesNotExist)
|
|
// preview the entry
|
|
fmt.Println(AnsiBold + "\nEntry Preview:" + backend.AnsiReset)
|
|
EntryReader(unencryptedEntry, hideSecrets, true)
|
|
} else {
|
|
fmt.Println(backend.AnsiError + "No data supplied for entry" + backend.AnsiReset)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
// expandPathWithHome, given a path (as a string) containing "~", returns the path with "~" expanded to the user's home directory
|
|
func expandPathWithHome(path string) string {
|
|
return strings.Replace(path, "~", backend.Home, 1)
|
|
}
|