-
Notifications
You must be signed in to change notification settings - Fork 24
/
blueprints.go
64 lines (56 loc) · 1.65 KB
/
blueprints.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
package utils
import (
"errors"
"os"
"path/filepath"
tea "github.com/charmbracelet/bubbletea"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/tui"
"github.com/ghodss/yaml"
)
// GetBlueprintsTUIModel prepares the TUI model for the blueprints command.
func GetBlueprintsTUIModel(blueprintsDir string, selectAll bool) ([]string, *tui.CheckBoxModel, error) {
blueprints, err := GetBlueprints(blueprintsDir)
if err != nil {
return nil, nil, err
}
if len(blueprints) == 0 {
return nil, nil, errors.New("no blueprints found")
}
model := tui.InitialCheckboxModel(blueprints, "Which blueprints would you like to apply?")
if !selectAll {
p := tea.NewProgram(model)
if _, err := p.Run(); err != nil {
return nil, nil, err
}
} else {
for i := range blueprints {
model.Selected[i] = struct{}{}
}
}
return blueprints, model, nil
}
// GetBlueprints returns a list of files that look like blueprints.
func GetBlueprints(blueprintsDir string) ([]string, error) {
blueprints := []string{}
return blueprints, filepath.Walk(blueprintsDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Ext(info.Name()) == ".yaml" || filepath.Ext(info.Name()) == ".yml" {
// read the blueprint and look for the blueprint and uri fields
bytes, err := os.ReadFile(path)
if err != nil {
return nil
}
var blueprint map[string]interface{}
if err := yaml.Unmarshal(bytes, &blueprint); err != nil {
return nil
}
if blueprint["blueprint"] != nil && blueprint["uri"] != nil {
// add this filename to the list of blueprints
blueprints = append(blueprints, path)
}
}
return nil
})
}