🎨 refactor(main): Extract sortMapKeys func

This commit is contained in:
punkfairie 2024-09-07 17:29:34 -07:00
parent 97929aa589
commit b4c80068bf
2 changed files with 32 additions and 20 deletions

24
main.go
View file

@ -26,13 +26,15 @@ type menu struct {
quitting bool
}
const softwareInstructionsFile = "/Users/marley/hackin/install.fairie/software-custom.yml"
func initialModel() menu {
s := spinner.New()
s.Spinner = spinner.MiniDot
s.Style = gloss.NewStyle().Foreground(gloss.Color("3"))
return menu{
current: 3,
current: 0,
keys: keys,
help: help.New(),
spinner: s,
@ -70,7 +72,7 @@ type errMsg struct{ err error }
func (e errMsg) Error() string { return e.err.Error() }
func (m menu) Init() tea.Cmd {
return tea.Batch(readYaml, m.spinner.Tick)
return tea.Batch(readYaml(softwareInstructionsFile), m.spinner.Tick)
}
func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@ -123,11 +125,7 @@ func (m menu) View() string {
software := list.New().Enumerator(softwareListEnumerator)
keys := make([]string, 0, len(m.order))
for k := range m.order {
keys = append(keys, k)
}
sort.Strings(keys)
keys := sortMapKeys(m.order)
for _, k := range keys {
software.Item(m.order[k].Name)
@ -161,3 +159,15 @@ func main() {
os.Exit(1)
}
}
func sortMapKeys(m SoftwarePackages) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}

28
yaml.go
View file

@ -71,18 +71,20 @@ type osNames struct {
Windows *string `yaml:"windows"`
}
func readYaml() tea.Msg {
fileData, fileErr := os.ReadFile("/Users/marley/hackin/install.fairie/software-custom.yml")
if fileErr != nil {
return errMsg{fileErr}
func readYaml(file string) tea.Cmd {
return func() tea.Msg {
fileData, fileErr := os.ReadFile(file)
if fileErr != nil {
return errMsg{fileErr}
}
var parsedYaml YamlStructure
yamlErr := yaml.Unmarshal(fileData, &parsedYaml)
if yamlErr != nil {
return errMsg{yamlErr}
}
return yamlMsg(parsedYaml)
}
var parsedYaml YamlStructure
yamlErr := yaml.Unmarshal(fileData, &parsedYaml)
if yamlErr != nil {
return errMsg{yamlErr}
}
return yamlMsg(parsedYaml)
}