feat(yaml): Reading yaml files!

This commit is contained in:
punkfairie 2024-09-07 14:38:48 -07:00
parent 0a23305cbd
commit 7d91974c56
3 changed files with 83 additions and 14 deletions

11
go.mod
View file

@ -2,10 +2,14 @@ module bartender
go 1.23.1
require (
github.com/charmbracelet/bubbletea v1.1.0
github.com/charmbracelet/lipgloss v0.13.0
golang.org/x/term v0.24.0
)
require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/bubbletea v1.1.0 // indirect
github.com/charmbracelet/lipgloss v0.13.0 // indirect
github.com/charmbracelet/x/ansi v0.2.3 // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
@ -18,6 +22,7 @@ require (
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.3.8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

9
go.sum
View file

@ -31,7 +31,12 @@ golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

77
main.go
View file

@ -5,26 +5,75 @@ import (
"os"
tea "github.com/charmbracelet/bubbletea"
gloss "github.com/charmbracelet/lipgloss"
"golang.org/x/term"
"gopkg.in/yaml.v3"
)
var width, height, _ = term.GetSize(int(os.Stdout.Fd()))
type menu struct {
order []string
order SoftwarePackages
current int
done int
}
func initialModel() menu {
return menu{
order: []string{"brew install yq"},
current: 0,
}
}
type SoftwareDef struct {
Bin *string `yaml:"_bin"`
Desc string `yaml:"_desc"`
Docs *string `yaml:"_docs"`
Github *string `yaml:"_github"`
Home *string `yaml:"_home"`
Name string `yaml:"_name"`
Apk *string `yaml:"apk"`
Appimage *string `yaml:"appimage"`
}
type SoftwarePackages map[string]SoftwareDef
type YamlStructure struct {
SoftwarePackages SoftwarePackages `yaml:"softwarePackages"`
}
type yamlMsg YamlStructure
type errMsg struct{ err error }
func (e errMsg) Error() string { return e.err.Error() }
func readYaml() tea.Msg {
fileData, fileErr := os.ReadFile("/Users/marley/hackin/install.fairie/software-custom.yml")
if fileErr != nil {
return errMsg{fileErr}
}
var parsedYaml YamlStructure
yamlErr := yaml.Unmarshal(fileData, &parsedYaml)
if yamlErr != nil {
return errMsg{yamlErr}
}
return yamlMsg(parsedYaml)
}
func (m menu) Init() tea.Cmd {
return nil
return readYaml
}
func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case yamlMsg:
m.order = msg.SoftwarePackages
return m, tea.Quit
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
@ -36,15 +85,25 @@ func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
func (m menu) View() string {
s := "Installing...\n\n"
mainStyle := gloss.NewStyle().
Width(int(float64(width) * 0.65)).
BorderStyle(gloss.NormalBorder()).
BorderForeground(gloss.Color("63"))
for _, item := range m.order {
s += fmt.Sprintf("%s\n", item)
}
sidebarStyle := gloss.NewStyle().
Width(int(float64(width) * 0.3)).
BorderStyle(gloss.NormalBorder()).
BorderForeground(gloss.Color("63"))
s += "\nPress q to quit.\n"
mainContent := fmt.Sprintln(m.order)
sidebarContent := ""
return s
main := mainStyle.Render(mainContent)
sidebar := sidebarStyle.Render(sidebarContent)
content := gloss.JoinHorizontal(gloss.Top, main, sidebar)
return gloss.PlaceHorizontal(width, gloss.Center, content)
}
func main() {