From b4c80068bf86d0237b6d6e6faeeff97db4c953ec Mon Sep 17 00:00:00 2001 From: punkfairie <23287005+punkfairie@users.noreply.github.com> Date: Sat, 7 Sep 2024 17:29:34 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20refactor(main):=20Extract=20sort?= =?UTF-8?q?MapKeys=20func?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 24 +++++++++++++++++------- yaml.go | 28 +++++++++++++++------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index 67739cd..bc05a49 100644 --- a/main.go +++ b/main.go @@ -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 +} diff --git a/yaml.go b/yaml.go index ac8a477..83e433e 100644 --- a/yaml.go +++ b/yaml.go @@ -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) }