-
Notifications
You must be signed in to change notification settings - Fork 46
/
preset.go
114 lines (90 loc) · 2.62 KB
/
preset.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
package commands
import (
"fmt"
"kool-dev/kool/core/presets"
"kool-dev/kool/core/shell"
"sort"
"github.com/spf13/cobra"
)
// KoolPreset holds handlers and functions to implement the preset command logic
type KoolPreset struct {
DefaultKoolService
presetsParser presets.Parser
promptSelect shell.PromptSelect
}
func AddKoolPreset(root *cobra.Command) {
var (
preset = NewKoolPreset()
presetCmd = NewPresetCommand(preset)
)
root.AddCommand(presetCmd)
}
// NewKoolPreset creates a new handler for preset logic
func NewKoolPreset() *KoolPreset {
return &KoolPreset{
*newDefaultKoolService(),
presets.NewParser(),
shell.NewPromptSelect(),
}
}
// Execute runs the preset logic with incoming arguments.
func (p *KoolPreset) Execute(args []string) (err error) {
var preset string
if preset, err = p.getPreset(args); err != nil {
return
}
if !p.presetsParser.Exists(preset) {
err = fmt.Errorf("unknown preset %s", preset)
return
}
p.Shell().Println("Preset", preset, "is initializing!")
p.presetsParser.PrepareExecutor(p.Shell())
if err = p.presetsParser.Install(preset); err != nil {
return
}
p.Shell().Success("Preset ", preset, " initialized!")
return
}
// NewPresetCommand initializes new kool preset command
func NewPresetCommand(preset *KoolPreset) (presetCmd *cobra.Command) {
presetCmd = &cobra.Command{
Use: "preset [PRESET]",
Short: "Install configuration files customized for Kool in the current directory",
Long: `Initialize a project using the specified [PRESET] by installing configuration
files customized for Kool in the current working directory. If no [PRESET] is provided,
an interactive wizard will present the available options.`,
Args: cobra.MaximumNArgs(1),
RunE: DefaultCommandRunFunction(preset),
DisableFlagsInUseLine: true,
}
return
}
func (p *KoolPreset) getPreset(args []string) (pickedPreset string, err error) {
if len(args) == 1 {
pickedPreset = args[0]
return
}
if !p.Shell().IsTerminal() {
err = fmt.Errorf("please specify a preset as argument (non-TTY env)")
return
}
var tag string
if tag, err = p.promptSelect.Ask("Pick the preset category you are looking for", p.presetsParser.GetTags()); err != nil {
return
}
var availablePresets = p.presetsParser.GetPresets(tag)
var presets []string
for _, name := range availablePresets {
presets = append(presets, name)
}
sort.Strings(presets)
if pickedPreset, err = p.promptSelect.Ask("What preset do you want to use", presets); err != nil {
return
}
for preset, name := range availablePresets {
if name == pickedPreset {
pickedPreset = preset
}
}
return
}