feat(yaml): Reading recipe file!

This commit is contained in:
punkfairie 2024-09-08 20:14:35 -07:00
parent d06436d993
commit 63e4723817
5 changed files with 60 additions and 94 deletions

View file

@ -36,7 +36,7 @@ func (m menu) installPackage() tea.Cmd {
if err == io.EOF { if err == io.EOF {
m.logger.Infof("Finished installing %s!", pkg) m.logger.Infof("Finished installing %s!", pkg)
m.logger.Infof("Output: %s\n\n", *m.output) m.logger.Infof("Output: %s\n\n", *m.output)
time.Sleep(2 * time.Second) time.Sleep(1 * time.Second)
return cmdDoneMsg{} return cmdDoneMsg{}
} }

71
log.txt
View file

@ -35,74 +35,3 @@ INFO Output:
INFO Installing bash-completion... INFO Installing bash-completion...
INFO Finished installing bash-completion!
INFO Output:
0 line
1 line
2 line
3 line
4 line
5 line
6 line
7 line
8 line
9 line
10 line
11 line
12 line
13 line
14 line
15 line
16 line
17 line
18 line
19 line
20 line
21 line
22 line
23 line
24 line
25 line
26 line
27 line
28 line
29 line
30 line
INFO Installing bandwhich...
INFO Finished installing bandwhich!
INFO Output:
0 line
1 line
2 line
3 line
4 line
5 line
6 line
7 line
8 line
9 line
10 line
11 line
12 line
13 line
14 line
15 line
16 line
17 line
18 line
19 line
20 line
21 line
22 line
23 line
24 line
25 line
26 line
27 line
28 line
29 line
30 line

29
main.go
View file

@ -18,6 +18,7 @@ import (
type menu struct { type menu struct {
order []string order []string
recipes SoftwarePackages
current int current int
keys keyMap keys keyMap
help help.Model help help.Model
@ -32,7 +33,8 @@ type menu struct {
} }
const ( const (
softwareInstructionsFile = "/Users/marley/hackin/install.fairie/home/.chezmoidata.yaml" ordersFile = "/Users/marley/hackin/install.fairie/home/.chezmoidata.yaml"
recipesFile = "/Users/marley/hackin/install.fairie/software.yml"
softwareGroup = "_Full-Desktop" softwareGroup = "_Full-Desktop"
) )
@ -86,8 +88,6 @@ func (k keyMap) FullHelp() [][]key.Binding {
} }
} }
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() }
@ -99,7 +99,7 @@ func (m *menu) appendOutput(s string) {
} }
func (m menu) Init() tea.Cmd { func (m menu) Init() tea.Cmd {
return tea.Batch(getSoftwareList(softwareInstructionsFile), m.spinner.Tick) return tea.Batch(getOrders(ordersFile), getRecipes(recipesFile), m.spinner.Tick)
} }
func (m menu) setDimensions() { func (m menu) setDimensions() {
@ -112,9 +112,17 @@ func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case softwareListMsg: case ordersMsg:
m.order = msg m.order = msg
if len(m.recipes) > 0 {
cmds = append(cmds, m.installPackage(), waitForCmdResponses(m.sub)) cmds = append(cmds, m.installPackage(), waitForCmdResponses(m.sub))
}
case recipesMsg:
m.recipes = SoftwarePackages(msg)
if len(m.recipes) > 0 {
cmds = append(cmds, m.installPackage(), waitForCmdResponses(m.sub))
}
case cmdMsg: case cmdMsg:
m.appendOutput(string(msg)) m.appendOutput(string(msg))
@ -168,10 +176,18 @@ func (m menu) View() string {
func main() { func main() {
l, err := os.Create("log.txt") l, err := os.Create("log.txt")
if err != nil { if err != nil {
panic(err) fmt.Println("fatal:", err)
os.Exit(1)
} }
defer l.Close() defer l.Close()
f, err := tea.LogToFile("tea-log.txt", "debug")
if err != nil {
fmt.Println("fatal:", err)
os.Exit(1)
}
defer f.Close()
p := tea.NewProgram( p := tea.NewProgram(
initialModel(l), initialModel(l),
tea.WithAltScreen(), tea.WithAltScreen(),
@ -179,7 +195,6 @@ func main() {
if _, err := p.Run(); err != nil { if _, err := p.Run(); err != nil {
fmt.Printf("There's been an error: %v", err) fmt.Printf("There's been an error: %v", err)
os.Exit(1) os.Exit(1)
} }
} }

0
tea-log.txt Normal file
View file

36
yaml.go
View file

@ -8,19 +8,19 @@ import (
) )
type YamlStructure struct { type YamlStructure struct {
InstallerPreferences InstallerPreferences `yaml:"installerPreferences"` InstallerPreference InstallerPreference `yaml:"installerPreference"`
SoftwarePackages SoftwarePackages `yaml:"softwarePackages"` SoftwarePackages SoftwarePackages `yaml:"softwarePackages"`
} }
type InstallerPreferences struct { type InstallerPreference struct {
Apt []string `yaml:"apt"` Apt []string `yaml:"apt"`
Darwin []string `yaml:"darwin"` Darwin []string `yaml:"darwin"`
Dnf []string `yaml:"dnf"` Fedora []string `yaml:"fedora"`
Freebsd []string `yaml:"freebsd"` Freebsd []string `yaml:"freebsd"`
Pacman []string `yaml:"pacman"` Arch []string `yaml:"arch"`
Ubuntu []string `yaml:"ubuntu"` Ubuntu []string `yaml:"ubuntu"`
Windows []string `yaml:"windows"` Windows []string `yaml:"windows"`
Zypper []string `yaml:"zypper"` OpenSUSE []string `yaml:"openSUSE"`
} }
type SoftwarePackages map[string]SoftwareDef type SoftwarePackages map[string]SoftwareDef
@ -77,7 +77,11 @@ type ChezmoiData struct {
type SoftwareGroups map[string]any type SoftwareGroups map[string]any
func getSoftwareList(file string) tea.Cmd { type ordersMsg []string
type recipesMsg SoftwarePackages
func getOrders(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 {
@ -93,6 +97,24 @@ func getSoftwareList(file string) tea.Cmd {
list := flatten(parsedYaml.SoftwareGroups[softwareGroup]) list := flatten(parsedYaml.SoftwareGroups[softwareGroup])
return softwareListMsg(list) return ordersMsg(list)
}
}
func getRecipes(file string) tea.Cmd {
return func() tea.Msg {
fileData, fileErr := os.ReadFile(file)
if fileErr != nil {
return errMsg{fileErr}
}
var parsedYaml SoftwarePackages
yamlErr := yaml.Unmarshal(fileData, &parsedYaml)
if yamlErr != nil {
return errMsg{yamlErr}
}
return recipesMsg(parsedYaml)
} }
} }