-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.go
67 lines (51 loc) · 1.56 KB
/
code.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
package guides
import (
"fmt"
"path"
"strings"
"github.com/AlecAivazis/survey/v2"
surveyasker "github.com/joselitofilho/aws-terraform-generator/internal/survey"
)
type CodeAnswers struct {
StackName string
Config string
Output string
}
func GuideCode(surveyAsker surveyasker.Asker, workdir string, fileMap map[string][]string) (*CodeAnswers, error) {
if len(fileMap["config"]) == 0 {
return nil, ErrDirDoesNotContainAnyConfigFile
}
configOptions := make([]string, 0, len(fileMap["config"]))
configOptions = append(configOptions, fileMap["config"]...)
answers := CodeAnswers{}
_, defaultStackName := path.Split(workdir)
if err := surveyAsker.AskOne(&survey.Input{
Message: "Enter the stack name:",
Default: defaultStackName,
}, &answers.StackName); err != nil {
return nil, fmt.Errorf("%w", err)
}
var defaultConfigOption int
for i := range configOptions {
if strings.Contains(configOptions[i], "diagram.yaml") ||
strings.Contains(configOptions[i], "diagram.yml") {
defaultConfigOption = i
}
}
if err := surveyAsker.AskOne(&survey.Select{
Message: "Choose a config:",
Default: defaultConfigOption,
Options: configOptions,
}, &answers.Config); err != nil {
return nil, fmt.Errorf("%w", err)
}
answers.Config = replaceDoubleSlash(path.Join(workdir, answers.Config))
if err := surveyAsker.AskOne(&survey.Input{
Message: "Enter the output folder:",
Default: "./output",
}, &answers.Output); err != nil {
return nil, fmt.Errorf("%w", err)
}
answers.Output = replaceDoubleSlash(answers.Output)
return &answers, nil
}