/
input.go
134 lines (116 loc) · 3.62 KB
/
input.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package input
import (
"errors"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/ubclaunchpad/inertia/cfg"
"github.com/ubclaunchpad/inertia/cmd/core/utils/out"
)
var (
errInvalidInput = errors.New("invalid input")
errEmptyInput = errors.New("empty input")
errInvalidUser = errors.New("invalid user")
errInvalidAddress = errors.New("invalid IP address")
errInvalidBuildType = errors.New("invalid build type")
errInvalidBuildFilePath = errors.New("invalid buildfile path")
)
// CatchSigterm listens in the background for some kind of interrupt and calls
// the given cancelFunc as necessary
func CatchSigterm(cancelFunc func()) {
var signals = make(chan os.Signal)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-signals
cancelFunc()
}()
}
// Prompt prints the given query and reads the response
func Prompt(query ...interface{}) (string, error) {
out.Println(query...)
var response string
if _, err := fmt.Fscanln(os.Stdin, &response); err != nil {
if strings.Contains(err.Error(), "unexpected newline") {
return "", nil
}
return "", err
}
return response, nil
}
// Promptf prints the given query and reads the response
func Promptf(query string, args ...interface{}) (string, error) {
out.Printf(query+"\n", args...)
var response string
if _, err := fmt.Fscanln(os.Stdin, &response); err != nil {
return "", err
}
return response, nil
}
// AddProjectWalkthrough is the command line walkthrough that asks for details
// about the project the user intends to deploy
func AddProjectWalkthrough() (
buildType cfg.BuildType, buildFilePath string, err error,
) {
out.Println(out.C("Please enter the path to your build configuration file:", out.CY))
out.Println(" - docker-compose")
out.Println(" - dockerfile")
var response string
if _, err = fmt.Fscanln(os.Stdin, &response); err != nil {
return "", "", errInvalidBuildType
}
buildType, err = cfg.AsBuildType(response)
if err != nil {
return "", "", err
}
buildFilePath, err = Prompt(
out.C("Please enter the path to your build configuration file:", out.CY).String(),
)
if err != nil || buildFilePath == "" {
return "", "", errInvalidBuildFilePath
}
return
}
// EnterEC2CredentialsWalkthrough prints promts to stdout and reads input from
// given reader
func EnterEC2CredentialsWalkthrough() (id, key string, err error) {
out.Print(`To get your credentials:
1. Open the IAM console (https://console.aws.amazon.com/iam/home?#home).
2. In the navigation pane of the console, choose Users. You may have to create a user.
3. Choose your IAM user name (not the check box).
4. Choose the Security credentials tab and then choose Create access key.
5. To see the new access key, choose Show. Your credentials will look something like this:
Access key ID: AKIAIOSFODNN7EXAMPLE
Secret access key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
`)
var response string
out.Print("\nKey ID: ")
_, err = fmt.Fscanln(os.Stdin, &response)
if err != nil {
return
}
id = response
out.Print("\nAccess Key: ")
_, err = fmt.Fscanln(os.Stdin, &response)
if err != nil {
return
}
key = response
return
}
// ChooseFromListWalkthrough prints given options and reads in a choice from
// the given reader
func ChooseFromListWalkthrough(optionName string, options []string) (string, error) {
out.Printf("Available %ss:\n", optionName)
for _, o := range options {
out.Println(" > " + o)
}
out.Print(out.C("Please enter your desired %s: ", out.CY).With(optionName))
var response string
_, err := fmt.Fscanln(os.Stdin, &response)
if err != nil {
return "", errInvalidInput
}
return response, nil
}