forked from gruntwork-io/terragrunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
47 lines (38 loc) · 1.24 KB
/
prompt.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
44
45
46
47
package shell
import (
"strings"
"fmt"
"os"
"bufio"
"github.com/gruntwork-io/terragrunt/errors"
"github.com/gruntwork-io/terragrunt/options"
)
// Prompt the user for text in the CLI. Returns the text entered by the user.
func PromptUserForInput(prompt string, terragruntOptions *options.TerragruntOptions) (string, error) {
if terragruntOptions.Logger.Prefix() != "" {
prompt = fmt.Sprintf("%s %s", terragruntOptions.Logger.Prefix(), prompt)
}
fmt.Print(prompt)
if terragruntOptions.NonInteractive {
fmt.Println()
terragruntOptions.Logger.Printf("The non-interactive flag is set to true, so assuming 'yes' for all prompts")
return "yes", nil
}
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString('\n')
if err != nil {
return "", errors.WithStackTrace(err)
}
return strings.TrimSpace(text), nil
}
// Prompt the user for a yes/no response and return true if they entered yes.
func PromptUserForYesNo(prompt string, terragruntOptions *options.TerragruntOptions) (bool, error) {
resp, err := PromptUserForInput(fmt.Sprintf("%s (y/n) ", prompt), terragruntOptions)
if err != nil {
return false, errors.WithStackTrace(err)
}
switch strings.ToLower(resp) {
case "y", "yes": return true, nil
default: return false, nil
}
}