forked from Samasource/jen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
confirm.go
41 lines (37 loc) · 1.04 KB
/
confirm.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
package steps
import (
"github.com/AlecAivazis/survey/v2"
"github.com/silphid/jen/cmd/jen/internal/evaluation"
"github.com/silphid/jen/cmd/jen/internal/exec"
logging "github.com/silphid/jen/cmd/jen/internal/logging"
)
// Confirm represents a conditional step that executes its child executable only if
// user answers Yes when prompted for given message
type Confirm struct {
Message string
Then exec.Executables
}
func (c Confirm) String() string {
return "confirm"
}
// Execute executes a child action only if user answers Yes when prompted for given message
func (c Confirm) Execute(context exec.Context) error {
message, err := evaluation.EvalTemplate(context, c.Message)
if err != nil {
return err
}
prompt := &survey.Confirm{
Message: message,
Default: false,
}
value := false
if err := survey.AskOne(prompt, &value); err != nil {
return err
}
if !value {
logging.Log("Skipping sub-steps because user cancelled")
return nil
}
logging.Log("Executing sub-steps because user confirmed")
return c.Then.Execute(context)
}