forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive.go
43 lines (34 loc) · 1.16 KB
/
interactive.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
39
40
41
42
43
package plugin
import (
"fmt"
"os"
"sync"
"github.com/mattn/go-isatty"
)
var isInteractive bool = (isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd())) &&
(isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()))
// InitInteractive is used by Wash commands to set option-specific overrides. Only sets
// interactivity to true if it already was and 'init' is also true.
func InitInteractive(init bool) {
isInteractive = init && isInteractive
}
// IsInteractive returns true if Wash is running as an interactive session. If false, please don't
// prompt for input on stdin.
func IsInteractive() bool {
return isInteractive
}
// Only allow one Prompt call at a time. This prevents multiple plugins loading concurrently
// from messing things up by calling Prompt concurrently.
var promptMux sync.Mutex
// Prompt prints the supplied message, then waits for input on stdin.
func Prompt(msg string) (string, error) {
if !IsInteractive() {
return "", fmt.Errorf("not an interactive session")
}
promptMux.Lock()
defer promptMux.Unlock()
var v string
fmt.Fprintf(os.Stderr, "%s: ", msg)
_, err := fmt.Scanln(&v)
return v, err
}