-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactivity.go
38 lines (30 loc) · 1.01 KB
/
interactivity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package pkg
import (
"fmt"
)
var isInteractive bool
func init() {
// By default, the library is not interactive. Interactive mode is used for CLI applications when you need to ask for user input
isInteractive = false
}
// SetInteractiveMode sets the interactive mode.
// If it is set to true, the library will ask for user input.
// By default, the library is not interactive.
// Interactive mode is used for CLI applications when you need to ask for user input.
func SetInteractiveMode(interactive bool) {
isInteractive = interactive
}
// ScanOrDefault scans user input and returns it. If the input is empty, it returns the default value.
// Input is not scanned in non-interactive mode and the default value is returned
func ScanOrDefault(prompt, defaultValue string) (input string) {
if !isInteractive {
return defaultValue
}
// We don't use current logger here, because we want to print the prompt without newlines
fmt.Print(prompt)
_, err := fmt.Scanln(&input)
if err != nil {
return defaultValue
}
return
}