🐛 fix(software-list): Flatten array

This commit is contained in:
punkfairie 2024-09-07 18:01:05 -07:00
parent b4c80068bf
commit c915314647
3 changed files with 51 additions and 13 deletions

29
flatten.go Normal file
View file

@ -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
}

21
main.go
View file

@ -17,7 +17,7 @@ import (
var width, height, _ = term.GetSize(int(os.Stdout.Fd())) var width, height, _ = term.GetSize(int(os.Stdout.Fd()))
type menu struct { type menu struct {
order SoftwarePackages order []string
current int current int
keys keyMap keys keyMap
help help.Model help help.Model
@ -26,7 +26,10 @@ type menu struct {
quitting bool 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 { func initialModel() menu {
s := spinner.New() s := spinner.New()
@ -65,14 +68,14 @@ func (k keyMap) FullHelp() [][]key.Binding {
} }
} }
type yamlMsg YamlStructure type softwareListMsg []string
type errMsg struct{ err error } type errMsg struct{ err error }
func (e errMsg) Error() string { return e.err.Error() } func (e errMsg) Error() string { return e.err.Error() }
func (m menu) Init() tea.Cmd { 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) { 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) { switch msg := msg.(type) {
case yamlMsg: case softwareListMsg:
m.order = msg.SoftwarePackages m.order = msg
return m, nil return m, nil
case tea.KeyMsg: case tea.KeyMsg:
@ -125,10 +128,8 @@ func (m menu) View() string {
software := list.New().Enumerator(softwareListEnumerator) software := list.New().Enumerator(softwareListEnumerator)
keys := sortMapKeys(m.order) for _, item := range m.order {
software.Item(item)
for _, k := range keys {
software.Item(m.order[k].Name)
} }
sidebarContent := software.String() sidebarContent := software.String()

14
yaml.go
View file

@ -71,20 +71,28 @@ type osNames struct {
Windows *string `yaml:"windows"` 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 { return func() tea.Msg {
fileData, fileErr := os.ReadFile(file) fileData, fileErr := os.ReadFile(file)
if fileErr != nil { if fileErr != nil {
return errMsg{fileErr} return errMsg{fileErr}
} }
var parsedYaml YamlStructure var parsedYaml ChezmoiData
yamlErr := yaml.Unmarshal(fileData, &parsedYaml) yamlErr := yaml.Unmarshal(fileData, &parsedYaml)
if yamlErr != nil { if yamlErr != nil {
return errMsg{yamlErr} return errMsg{yamlErr}
} }
return yamlMsg(parsedYaml) list := flatten(parsedYaml.SoftwareGroups[softwareGroup])
return softwareListMsg(list)
} }
} }