diff --git a/flatten.go b/flatten.go new file mode 100644 index 0000000..ef5e379 --- /dev/null +++ b/flatten.go @@ -0,0 +1,29 @@ +package main + +import ( + "reflect" +) + +func flatten(v ...interface{}) []string { + args := flattenDeep(nil, reflect.ValueOf(v)) + var strings []string + + for _, i := range args { + strings = append(strings, i.(string)) + } + return strings +} + +func flattenDeep(args []interface{}, v reflect.Value) []interface{} { + if v.Kind() == reflect.Interface { + v = v.Elem() + } + if v.Kind() == reflect.Array || v.Kind() == reflect.Slice { + for i := 0; i < v.Len(); i++ { + args = flattenDeep(args, v.Index(i)) + } + } else { + args = append(args, v.Interface()) + } + return args +} diff --git a/main.go b/main.go index bc05a49..3ca95ff 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ import ( var width, height, _ = term.GetSize(int(os.Stdout.Fd())) type menu struct { - order SoftwarePackages + order []string current int keys keyMap help help.Model @@ -26,7 +26,10 @@ type menu struct { quitting bool } -const softwareInstructionsFile = "/Users/marley/hackin/install.fairie/software-custom.yml" +const ( + softwareInstructionsFile = "/Users/marley/hackin/install.fairie/home/.chezmoidata.yaml" + softwareGroup = "_Full-Desktop" +) func initialModel() menu { s := spinner.New() @@ -65,14 +68,14 @@ func (k keyMap) FullHelp() [][]key.Binding { } } -type yamlMsg YamlStructure +type softwareListMsg []string type errMsg struct{ err error } func (e errMsg) Error() string { return e.err.Error() } func (m menu) Init() tea.Cmd { - return tea.Batch(readYaml(softwareInstructionsFile), m.spinner.Tick) + return tea.Batch(getSoftwareList(softwareInstructionsFile), m.spinner.Tick) } func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) { @@ -81,8 +84,8 @@ func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { - case yamlMsg: - m.order = msg.SoftwarePackages + case softwareListMsg: + m.order = msg return m, nil case tea.KeyMsg: @@ -125,10 +128,8 @@ func (m menu) View() string { software := list.New().Enumerator(softwareListEnumerator) - keys := sortMapKeys(m.order) - - for _, k := range keys { - software.Item(m.order[k].Name) + for _, item := range m.order { + software.Item(item) } sidebarContent := software.String() diff --git a/yaml.go b/yaml.go index 83e433e..c4bf8af 100644 --- a/yaml.go +++ b/yaml.go @@ -71,20 +71,28 @@ type osNames struct { Windows *string `yaml:"windows"` } -func readYaml(file string) tea.Cmd { +type ChezmoiData struct { + SoftwareGroups SoftwareGroups `yaml:"softwareGroups"` +} + +type SoftwareGroups map[string]any + +func getSoftwareList(file string) tea.Cmd { return func() tea.Msg { fileData, fileErr := os.ReadFile(file) if fileErr != nil { return errMsg{fileErr} } - var parsedYaml YamlStructure + var parsedYaml ChezmoiData yamlErr := yaml.Unmarshal(fileData, &parsedYaml) if yamlErr != nil { return errMsg{yamlErr} } - return yamlMsg(parsedYaml) + list := flatten(parsedYaml.SoftwareGroups[softwareGroup]) + + return softwareListMsg(list) } }