mirror of
https://github.com/rwinkhart/MUTN.git
synced 2026-09-02 23:27:22 -04:00
Port to latest libmutton development version (use go-boilerplate)
This commit is contained in:
@@ -1,74 +1,19 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/go-boilerplate/front"
|
||||
"github.com/rwinkhart/libmutton/core"
|
||||
"github.com/rwinkhart/libmutton/sync"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
// 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 byte array, hiding the input from the terminal.
|
||||
func inputHidden(prompt string) []byte {
|
||||
fmt.Print("\n" + prompt + " ")
|
||||
byteInput, _ := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
fmt.Println()
|
||||
return byteInput
|
||||
}
|
||||
|
||||
// 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))
|
||||
}
|
||||
|
||||
// inputPasswordGen prompts the user for password generation parameters and returns a generated password as a string.
|
||||
func inputPasswordGen() string {
|
||||
passLength := inputInt("Password length:", -1)
|
||||
passLength := front.InputPositiveInt("Password length:", 1, -1)
|
||||
fmt.Println()
|
||||
passCharset := uint8(inputMenuGen("Password complexity:", []string{"Simple", "Complex", "Ultra Complex (not compatible with many services)"}))
|
||||
passCharset := uint8(front.InputMenuGen("Password complexity:", []string{"Simple", "Complex", "Ultra Complex (not compatible with many services)"}))
|
||||
var complexity float64
|
||||
switch passCharset {
|
||||
case 1:
|
||||
@@ -87,15 +32,9 @@ func writeEntryCLI(targetLocation string, unencryptedEntry []string, hideSecrets
|
||||
// write the entry to the target location
|
||||
core.WriteEntry(targetLocation, []byte(strings.Join(unencryptedEntry, "\n")))
|
||||
// preview the entry
|
||||
fmt.Println(AnsiBold + "\nEntry Preview:" + core.AnsiReset)
|
||||
fmt.Println(AnsiBold + "\nEntry Preview:" + back.AnsiReset)
|
||||
EntryReader(unencryptedEntry, hideSecrets, true)
|
||||
} else {
|
||||
core.PrintError("No data supplied for entry", core.ErrorTargetNotFound, true)
|
||||
back.PrintError("No data supplied for entry", back.ErrorTargetNotFound, true)
|
||||
}
|
||||
}
|
||||
|
||||
// RunJobWrapper is a wrapper for sync.RunJob that sets the passphrase input function to inputHidden.
|
||||
func RunJobWrapper(manualSync bool) {
|
||||
core.PassphraseInputFunction = inputHidden
|
||||
sync.RunJob(manualSync, false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user