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 {
m.logger.Infof("Finished installing %s!", pkg)
m.logger.Infof("Output: %s\n\n", *m.output)
time.Sleep(2 * time.Second)
time.Sleep(1 * time.Second)
return cmdDoneMsg{}
}

71
log.txt
View file

@ -35,74 +35,3 @@ INFO Output:
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

33
main.go
View file

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

0
tea-log.txt Normal file
View file

48
yaml.go
View file

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