From 3fef157f6ab301635ed2b91e4c41b6d6234f707d Mon Sep 17 00:00:00 2001 From: restitux Date: Sat, 25 Feb 2023 01:24:22 -0700 Subject: [PATCH] Persist server configuration to config file --- go.mod | 2 ++ go.sum | 20 +++++++++++++-- main.go | 57 +++++++++++++++++++++++++------------------ screens/dashboard.go | 11 +++------ screens/editserver.go | 21 +++++++++------- screens/login.go | 24 ++++++++++-------- settings/settings.go | 28 +++++++++++++++++++++ 7 files changed, 110 insertions(+), 53 deletions(-) create mode 100644 settings/settings.go diff --git a/go.mod b/go.mod index 4584f76..d9758f0 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.ohea.xyz/cursorius/tui go 1.20 require ( + git.ohea.xyz/golang/config v0.0.0-20230225082310-91f0f601076e github.com/Khan/genqlient v0.5.0 github.com/charmbracelet/bubbles v0.15.0 github.com/charmbracelet/bubbletea v0.23.2 @@ -21,6 +22,7 @@ require ( github.com/muesli/cancelreader v0.2.2 // indirect github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/termenv v0.14.0 // indirect + github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/sahilm/fuzzy v0.1.0 // indirect github.com/vektah/gqlparser/v2 v2.4.5 // indirect diff --git a/go.sum b/go.sum index 3c5c5da..843d7dc 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,9 @@ +git.ohea.xyz/golang/config v0.0.0-20221002005232-8a901413a8b0 h1:a8ygEuzmqFDxXmf+e1IseDKBcAtkaIwfL3k4PIVVVr8= +git.ohea.xyz/golang/config v0.0.0-20221002005232-8a901413a8b0/go.mod h1:86PbXJ2WdqQ+3hYqrnv3ukgKNRK9nQfThnlY03FAO0g= +git.ohea.xyz/golang/config v0.0.0-20230225082201-095f317865a2 h1:B0nZl+J2d38+mjC4+eeMDl9S1OXPntqDwm8Kf8WIXL0= +git.ohea.xyz/golang/config v0.0.0-20230225082201-095f317865a2/go.mod h1:86PbXJ2WdqQ+3hYqrnv3ukgKNRK9nQfThnlY03FAO0g= +git.ohea.xyz/golang/config v0.0.0-20230225082310-91f0f601076e h1:Hwv4cSg2+VG7vk7uOS/WqGIGySmW1xpjs0blo6dfHYc= +git.ohea.xyz/golang/config v0.0.0-20230225082310-91f0f601076e/go.mod h1:86PbXJ2WdqQ+3hYqrnv3ukgKNRK9nQfThnlY03FAO0g= github.com/99designs/gqlgen v0.17.2/go.mod h1:K5fzLKwtph+FFgh9j7nFbRUdBKvTcGnsta51fsMTn3o= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Khan/genqlient v0.5.0 h1:TMZJ+tl/BpbmGyIBiXzKzUftDhw4ZWxQZ+1ydn0gyII= @@ -70,6 +76,10 @@ github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc= github.com/muesli/termenv v0.14.0 h1:8x9NFfOe8lmIWK4pgy3IfVEy47f+ppe3tUqdPZG2Uy0= github.com/muesli/termenv v0.14.0/go.mod h1:kG/pF1E7fh949Xhe156crRUrHNyK221IuGO7Ez60Uc8= +github.com/pelletier/go-toml/v2 v2.0.5 h1:ipoSadvV8oGUjnUbMub59IDPPwfxF694nG/jwbMiyQg= +github.com/pelletier/go-toml/v2 v2.0.5/go.mod h1:OMHamSCAODeSsVrwwvcJOaoN0LIUIaFVNZzmWyNfXas= +github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU= +github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= @@ -84,11 +94,16 @@ github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNX github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= github.com/vektah/gqlparser/v2 v2.4.0/go.mod h1:flJWIR04IMQPGz+BXLrORkrARBxv/rtyIAFvd/MceW0= github.com/vektah/gqlparser/v2 v2.4.5 h1:C02NsyEsL4TXJB7ndonqTfuQOL4XPIu0aAWugdmTgmc= @@ -151,5 +166,6 @@ gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index ee76b17..49a4f3d 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,10 @@ import ( "fmt" "os" - "git.ohea.xyz/cursorius/tui/screens" tea "github.com/charmbracelet/bubbletea" + + "git.ohea.xyz/cursorius/tui/screens" + "git.ohea.xyz/cursorius/tui/settings" ) type model struct { @@ -55,31 +57,38 @@ func (m model) View() string { } func main() { + + conf, err := settings.GetConfig() + if err != nil { + fmt.Printf("Error: %v", err) + return + } + initialModel := model{ screen: screens.CreateLogin( - // TODO: load from config file - []screens.CursoriusServer{ - { - Name: "local-test", - Url: "http://127.0.0.1:45421/graphql", - Token: "", - }, - { - Name: "ohea", - Url: "https://ci.cursorius.server", - Token: "test", - }, - { - Name: "nohea", - Url: "https://ci.cursoriuspreview.server", - Token: "test", - }, - { - Name: "work", - Url: "https://ci.acme.corp", - Token: "test", - }, - }, + conf, + //[]screens.CursoriusServer{ + // { + // Name: "local-test", + // Url: "http://127.0.0.1:45421/graphql", + // Token: "", + // }, + // { + // Name: "ohea", + // Url: "https://ci.cursorius.server", + // Token: "test", + // }, + // { + // Name: "nohea", + // Url: "https://ci.cursoriuspreview.server", + // Token: "test", + // }, + // { + // Name: "work", + // Url: "https://ci.acme.corp", + // Token: "test", + // }, + //}, ), } diff --git a/screens/dashboard.go b/screens/dashboard.go index 6e53798..62e8397 100644 --- a/screens/dashboard.go +++ b/screens/dashboard.go @@ -11,16 +11,11 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "git.ohea.xyz/cursorius/tui/settings" "git.ohea.xyz/cursorius/tui/widget" ) -type CursoriusServer struct { - Name string - Url string - Token string -} - -func (s CursoriusServer) Login() tea.Msg { +func ServerLogin(s settings.CursoriusServer) tea.Msg { dashboard, err := createDashboard(s) if err != nil { return err @@ -156,7 +151,7 @@ func (d dashboardItemDelegate) Render(w io.Writer, m list.Model, index int, list fmt.Fprint(w, fn(str)) } -func createDashboard(s CursoriusServer) (Dashboard, error) { +func createDashboard(s settings.CursoriusServer) (Dashboard, error) { client := graphql.NewClient(s.Url, http.DefaultClient) diff --git a/screens/editserver.go b/screens/editserver.go index 5594530..49e3d4d 100644 --- a/screens/editserver.go +++ b/screens/editserver.go @@ -4,6 +4,8 @@ import ( "fmt" "io" + "git.ohea.xyz/cursorius/tui/settings" + "git.ohea.xyz/golang/config" "github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" @@ -11,7 +13,7 @@ import ( type EditServer struct { entries list.Model - servers []CursoriusServer + config config.Config[settings.Config] pos int } @@ -77,15 +79,16 @@ func (m EditServer) Update(msg tea.Msg) (tea.Model, tea.Cmd) { token = e.field.Value() } - newServer := CursoriusServer{ + newServer := settings.CursoriusServer{ Name: name, Url: url, Token: token, } - m.servers[m.pos] = newServer + m.config.Config.Servers[m.pos] = newServer + m.config.Write() return m, func() tea.Msg { return ScreenSwitchMsg{ - NewScreen: CreateLogin(m.servers), + NewScreen: CreateLogin(m.config), } } } @@ -113,21 +116,21 @@ func (m EditServer) View() string { return m.entries.View() } -func createEditServer(s []CursoriusServer, pos int) EditServer { +func createEditServer(conf config.Config[settings.Config], pos int) EditServer { nameField := textinput.New() nameField.Focus() nameField.Width = 20 nameField.Prompt = "Name: " nameField.Placeholder = "Nickname" - nameField.SetValue(s[pos].Name) + nameField.SetValue(conf.Config.Servers[pos].Name) urlField := textinput.New() urlField.Prompt = "Url: " urlField.Placeholder = "https://ci.cursorius.ohea/" - urlField.SetValue(s[pos].Url) + urlField.SetValue(conf.Config.Servers[pos].Url) tokenField := textinput.New() tokenField.Prompt = "Token: " tokenField.Placeholder = "1234567890" - tokenField.SetValue(s[pos].Token) + tokenField.SetValue(conf.Config.Servers[pos].Token) tokenField.EchoMode = textinput.EchoPassword items := []list.Item{ entryItem{field: nameField}, @@ -145,7 +148,7 @@ func createEditServer(s []CursoriusServer, pos int) EditServer { l.KeyMap.Quit.SetEnabled(false) e := EditServer{ entries: l, - servers: s, + config: conf, pos: pos, } return e diff --git a/screens/login.go b/screens/login.go index b757ab1..11ad76c 100644 --- a/screens/login.go +++ b/screens/login.go @@ -4,6 +4,9 @@ import ( "fmt" "io" + "git.ohea.xyz/cursorius/tui/settings" + + "git.ohea.xyz/golang/config" "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -11,7 +14,7 @@ import ( type Login struct { selected int - servers []CursoriusServer + config config.Config[settings.Config] serverList list.Model } @@ -32,27 +35,28 @@ func (m Login) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyMsg: switch msg.String() { case "e": - if m.serverList.Index() < len(m.servers) { + if m.serverList.Index() < len(m.config.Config.Servers) { return m, func() tea.Msg { return ScreenSwitchMsg{ NewScreen: createEditServer( - m.servers, + m.config, m.serverList.Index(), ), } } } case "enter": - if m.serverList.Index() < len(m.servers) { + if m.serverList.Index() < len(m.config.Config.Servers) { return m, func() tea.Msg { - return m.servers[m.serverList.Index()].Login() + return ServerLogin(m.config.Config.Servers[m.serverList.Index()]) } } else { return m, func() tea.Msg { + m.config.Config.Servers = append(m.config.Config.Servers, settings.CursoriusServer{}) return ScreenSwitchMsg{ NewScreen: createEditServer( - append(m.servers, CursoriusServer{}), - len(m.servers), + m.config, + len(m.config.Config.Servers)-1, ), } } @@ -114,9 +118,9 @@ func (d loginItemDelegate) Render(w io.Writer, m list.Model, index int, listItem fmt.Fprint(w, fn(str)) } -func CreateLogin(servers []CursoriusServer) Login { +func CreateLogin(conf config.Config[settings.Config]) Login { items := []list.Item{} - for _, server := range servers { + for _, server := range conf.Config.Servers { items = append( items, serverItem{ @@ -137,7 +141,7 @@ func CreateLogin(servers []CursoriusServer) Login { return Login{ selected: 0, - servers: servers, + config: conf, serverList: l, } } diff --git a/settings/settings.go b/settings/settings.go new file mode 100644 index 0000000..fa76b00 --- /dev/null +++ b/settings/settings.go @@ -0,0 +1,28 @@ +package settings + +import "git.ohea.xyz/golang/config" + +type CursoriusServer struct { + Name string + Url string + Token string +} + +type Config struct { + Servers []CursoriusServer +} + +func GetConfig() (config.Config[Config], error) { + conf := config.Config[Config]{ + Name: "cursorius", + Filename: "tui", + Config: Config{}, + } + + _, err := conf.Get() + if err != nil { + return config.Config[Config]{}, err + } + + return conf, nil +}