forked from openshift/installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userprovided.go
59 lines (49 loc) · 1.22 KB
/
userprovided.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
package asset
import (
"io/ioutil"
"os"
survey "gopkg.in/AlecAivazis/survey.v1"
)
// UserProvided generates an asset that is supplied by a user.
type UserProvided struct {
AssetName string
Question *survey.Question
EnvVarName string
PathEnvVarName string
}
var _ Asset = (*UserProvided)(nil)
// Dependencies returns no dependencies.
func (a *UserProvided) Dependencies() []Asset {
return []Asset{}
}
// Generate queries for input from the user.
func (a *UserProvided) Generate(map[Asset]*State) (*State, error) {
var response string
if value, ok := os.LookupEnv(a.EnvVarName); ok {
response = value
} else if path, ok := os.LookupEnv(a.PathEnvVarName); ok {
value, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
response = string(value)
}
if response == "" {
if err := survey.Ask([]*survey.Question{a.Question}, &response); err != nil {
return nil, err
}
} else if a.Question.Validate != nil {
if err := a.Question.Validate(response); err != nil {
return nil, err
}
}
return &State{
Contents: []Content{{
Data: []byte(response),
}},
}, nil
}
// Name returns the human-friendly name of the asset.
func (a *UserProvided) Name() string {
return a.AssetName
}