5 Commits
Author SHA1 Message Date
RandyTheSilly f70ff83e1e Add rune input functions 2026-05-29 21:02:04 -04:00
RandyTheSilly 2fe21452c7 Bump dependencies & drop web package 2026-05-29 19:38:12 -04:00
RandyTheSilly 687518a0a6 Bump dependencies 2026-04-01 00:09:34 -04:00
RandyTheSilly 8313a183b0 Add web package 2026-04-01 00:08:39 -04:00
RandyTheSilly 7a63a41116 Make WriteToStdin input zeroization optional 2026-02-14 16:50:35 -05:00
5 changed files with 37 additions and 9 deletions
+4 -2
View File
@@ -11,7 +11,7 @@ import (
) )
// WriteToStdin is a utility function that writes a byte slice to a command's stdin. // WriteToStdin is a utility function that writes a byte slice to a command's stdin.
func WriteToStdinAndZeroizeInput(cmd *exec.Cmd, input []byte) error { func WriteToStdin(cmd *exec.Cmd, input []byte, zeroizeInput bool) error {
stdin, err := cmd.StdinPipe() stdin, err := cmd.StdinPipe()
if err != nil { if err != nil {
return errors.New("unable to access stdin for system command: " + err.Error()) return errors.New("unable to access stdin for system command: " + err.Error())
@@ -21,7 +21,9 @@ func WriteToStdinAndZeroizeInput(cmd *exec.Cmd, input []byte) error {
_ = stdin.Close() // error ignored; if stdin could be accessed, it can probably be closed _ = stdin.Close() // error ignored; if stdin could be accessed, it can probably be closed
}(stdin) }(stdin)
_, _ = stdin.Write(input) _, _ = stdin.Write(input)
security.ZeroizeBytes(input) if zeroizeInput {
security.ZeroizeBytes(input)
}
}() }()
return nil return nil
} }
@@ -16,6 +16,15 @@ func Input(prompt string) string {
return strings.TrimRight(userInput, "\n\r ") // remove trailing newlines, carriage returns, and spaces return strings.TrimRight(userInput, "\n\r ") // remove trailing newlines, carriage returns, and spaces
} }
// Input prompts the user for input and
// returns the input as a rune.
func InputRune(prompt string) rune {
fmt.Print(prompt + " ")
reader := bufio.NewReader(os.Stdin)
userInput, _, _ := reader.ReadRune()
return userInput
}
// InputBinary prompts the user with a yes/no // InputBinary prompts the user with a yes/no
// question and returns the response as a boolean. // question and returns the response as a boolean.
func InputBinary(prompt string) bool { func InputBinary(prompt string) bool {
@@ -3,6 +3,7 @@ package front
import ( import (
"fmt" "fmt"
"os" "os"
"slices"
"golang.org/x/term" "golang.org/x/term"
) )
@@ -16,6 +17,22 @@ func InputMenuGen(prompt string, options []string) int {
return InputInt("\n"+prompt, 1, len(options)) return InputInt("\n"+prompt, 1, len(options))
} }
// InputMenuGenWithRuneInputs prompts the user with a menu
// and returns the user's choice as a rune.
// Ensure runeInputs contains only unique runes and matches the
// length of options.
func InputMenuGenWithRuneInputs(prompt string, options []string, runeInputs []rune) rune {
for i, option := range options {
fmt.Printf("%c. %s\n", runeInputs[i], option)
}
for {
userInput := InputRune("\n" + prompt)
if slices.Contains(runeInputs, userInput) {
return userInput
}
}
}
// InputSecret prompts the user for input and returns the // InputSecret prompts the user for input and returns the
// input as a byte array, hiding the input from the terminal. // input as a byte array, hiding the input from the terminal.
func InputSecret(prompt string) []byte { func InputSecret(prompt string) []byte {
+3 -3
View File
@@ -1,7 +1,7 @@
module github.com/rwinkhart/go-boilerplate module github.com/rwinkhart/go-boilerplate
go 1.25.7 go 1.26.3
require golang.org/x/term v0.40.0 require golang.org/x/term v0.43.0
require golang.org/x/sys v0.41.0 // indirect require golang.org/x/sys v0.45.0 // indirect
+4 -4
View File
@@ -1,4 +1,4 @@
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k= golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/term v0.40.0 h1:36e4zGLqU4yhjlmxEaagx2KuYbJq3EwY8K943ZsHcvg= golang.org/x/term v0.43.0 h1:S4RLU2sB31O/NCl+zFN9Aru9A/Cq2aqKpTZJ6B+DwT4=
golang.org/x/term v0.40.0/go.mod h1:w2P8uVp06p2iyKKuvXIm7N/y0UCRt3UfJTfZ7oOpglM= golang.org/x/term v0.43.0/go.mod h1:lrhlHNdQJHO+1qVYiHfFKVuVioJIheAc3fBSMFYEIsk=