Persist server configuration to config file

This commit is contained in:
2023-02-25 01:24:22 -07:00
parent a41c12d20c
commit 3fef157f6a
7 changed files with 110 additions and 53 deletions
+2
View File
@@ -3,6 +3,7 @@ module git.ohea.xyz/cursorius/tui
go 1.20 go 1.20
require ( require (
git.ohea.xyz/golang/config v0.0.0-20230225082310-91f0f601076e
github.com/Khan/genqlient v0.5.0 github.com/Khan/genqlient v0.5.0
github.com/charmbracelet/bubbles v0.15.0 github.com/charmbracelet/bubbles v0.15.0
github.com/charmbracelet/bubbletea v0.23.2 github.com/charmbracelet/bubbletea v0.23.2
@@ -21,6 +22,7 @@ require (
github.com/muesli/cancelreader v0.2.2 // indirect github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.14.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/rivo/uniseg v0.2.0 // indirect
github.com/sahilm/fuzzy v0.1.0 // indirect github.com/sahilm/fuzzy v0.1.0 // indirect
github.com/vektah/gqlparser/v2 v2.4.5 // indirect github.com/vektah/gqlparser/v2 v2.4.5 // indirect
+18 -2
View File
@@ -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/99designs/gqlgen v0.17.2/go.mod h1:K5fzLKwtph+FFgh9j7nFbRUdBKvTcGnsta51fsMTn3o=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Khan/genqlient v0.5.0 h1:TMZJ+tl/BpbmGyIBiXzKzUftDhw4ZWxQZ+1ydn0gyII= 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.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=
github.com/muesli/termenv v0.14.0 h1:8x9NFfOe8lmIWK4pgy3IfVEy47f+ppe3tUqdPZG2Uy0= github.com/muesli/termenv v0.14.0 h1:8x9NFfOe8lmIWK4pgy3IfVEy47f+ppe3tUqdPZG2Uy0=
github.com/muesli/termenv v0.14.0/go.mod h1:kG/pF1E7fh949Xhe156crRUrHNyK221IuGO7Ez60Uc8= 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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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= 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/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.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.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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 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.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.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/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.0/go.mod h1:flJWIR04IMQPGz+BXLrORkrARBxv/rtyIAFvd/MceW0=
github.com/vektah/gqlparser/v2 v2.4.5 h1:C02NsyEsL4TXJB7ndonqTfuQOL4XPIu0aAWugdmTgmc= 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.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.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 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.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=
+33 -24
View File
@@ -4,8 +4,10 @@ import (
"fmt" "fmt"
"os" "os"
"git.ohea.xyz/cursorius/tui/screens"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"git.ohea.xyz/cursorius/tui/screens"
"git.ohea.xyz/cursorius/tui/settings"
) )
type model struct { type model struct {
@@ -55,31 +57,38 @@ func (m model) View() string {
} }
func main() { func main() {
conf, err := settings.GetConfig()
if err != nil {
fmt.Printf("Error: %v", err)
return
}
initialModel := model{ initialModel := model{
screen: screens.CreateLogin( screen: screens.CreateLogin(
// TODO: load from config file conf,
[]screens.CursoriusServer{ //[]screens.CursoriusServer{
{ // {
Name: "local-test", // Name: "local-test",
Url: "http://127.0.0.1:45421/graphql", // Url: "http://127.0.0.1:45421/graphql",
Token: "", // Token: "",
}, // },
{ // {
Name: "ohea", // Name: "ohea",
Url: "https://ci.cursorius.server", // Url: "https://ci.cursorius.server",
Token: "test", // Token: "test",
}, // },
{ // {
Name: "nohea", // Name: "nohea",
Url: "https://ci.cursoriuspreview.server", // Url: "https://ci.cursoriuspreview.server",
Token: "test", // Token: "test",
}, // },
{ // {
Name: "work", // Name: "work",
Url: "https://ci.acme.corp", // Url: "https://ci.acme.corp",
Token: "test", // Token: "test",
}, // },
}, //},
), ),
} }
+3 -8
View File
@@ -11,16 +11,11 @@ import (
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"git.ohea.xyz/cursorius/tui/settings"
"git.ohea.xyz/cursorius/tui/widget" "git.ohea.xyz/cursorius/tui/widget"
) )
type CursoriusServer struct { func ServerLogin(s settings.CursoriusServer) tea.Msg {
Name string
Url string
Token string
}
func (s CursoriusServer) Login() tea.Msg {
dashboard, err := createDashboard(s) dashboard, err := createDashboard(s)
if err != nil { if err != nil {
return err return err
@@ -156,7 +151,7 @@ func (d dashboardItemDelegate) Render(w io.Writer, m list.Model, index int, list
fmt.Fprint(w, fn(str)) 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) client := graphql.NewClient(s.Url, http.DefaultClient)
+12 -9
View File
@@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"io" "io"
"git.ohea.xyz/cursorius/tui/settings"
"git.ohea.xyz/golang/config"
"github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput" "github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
@@ -11,7 +13,7 @@ import (
type EditServer struct { type EditServer struct {
entries list.Model entries list.Model
servers []CursoriusServer config config.Config[settings.Config]
pos int pos int
} }
@@ -77,15 +79,16 @@ func (m EditServer) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
token = e.field.Value() token = e.field.Value()
} }
newServer := CursoriusServer{ newServer := settings.CursoriusServer{
Name: name, Name: name,
Url: url, Url: url,
Token: token, Token: token,
} }
m.servers[m.pos] = newServer m.config.Config.Servers[m.pos] = newServer
m.config.Write()
return m, func() tea.Msg { return m, func() tea.Msg {
return ScreenSwitchMsg{ return ScreenSwitchMsg{
NewScreen: CreateLogin(m.servers), NewScreen: CreateLogin(m.config),
} }
} }
} }
@@ -113,21 +116,21 @@ func (m EditServer) View() string {
return m.entries.View() 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 := textinput.New()
nameField.Focus() nameField.Focus()
nameField.Width = 20 nameField.Width = 20
nameField.Prompt = "Name: " nameField.Prompt = "Name: "
nameField.Placeholder = "Nickname" nameField.Placeholder = "Nickname"
nameField.SetValue(s[pos].Name) nameField.SetValue(conf.Config.Servers[pos].Name)
urlField := textinput.New() urlField := textinput.New()
urlField.Prompt = "Url: " urlField.Prompt = "Url: "
urlField.Placeholder = "https://ci.cursorius.ohea/" urlField.Placeholder = "https://ci.cursorius.ohea/"
urlField.SetValue(s[pos].Url) urlField.SetValue(conf.Config.Servers[pos].Url)
tokenField := textinput.New() tokenField := textinput.New()
tokenField.Prompt = "Token: " tokenField.Prompt = "Token: "
tokenField.Placeholder = "1234567890" tokenField.Placeholder = "1234567890"
tokenField.SetValue(s[pos].Token) tokenField.SetValue(conf.Config.Servers[pos].Token)
tokenField.EchoMode = textinput.EchoPassword tokenField.EchoMode = textinput.EchoPassword
items := []list.Item{ items := []list.Item{
entryItem{field: nameField}, entryItem{field: nameField},
@@ -145,7 +148,7 @@ func createEditServer(s []CursoriusServer, pos int) EditServer {
l.KeyMap.Quit.SetEnabled(false) l.KeyMap.Quit.SetEnabled(false)
e := EditServer{ e := EditServer{
entries: l, entries: l,
servers: s, config: conf,
pos: pos, pos: pos,
} }
return e return e
+14 -10
View File
@@ -4,6 +4,9 @@ import (
"fmt" "fmt"
"io" "io"
"git.ohea.xyz/cursorius/tui/settings"
"git.ohea.xyz/golang/config"
"github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
@@ -11,7 +14,7 @@ import (
type Login struct { type Login struct {
selected int selected int
servers []CursoriusServer config config.Config[settings.Config]
serverList list.Model serverList list.Model
} }
@@ -32,27 +35,28 @@ func (m Login) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "e": case "e":
if m.serverList.Index() < len(m.servers) { if m.serverList.Index() < len(m.config.Config.Servers) {
return m, func() tea.Msg { return m, func() tea.Msg {
return ScreenSwitchMsg{ return ScreenSwitchMsg{
NewScreen: createEditServer( NewScreen: createEditServer(
m.servers, m.config,
m.serverList.Index(), m.serverList.Index(),
), ),
} }
} }
} }
case "enter": 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, func() tea.Msg {
return m.servers[m.serverList.Index()].Login() return ServerLogin(m.config.Config.Servers[m.serverList.Index()])
} }
} else { } else {
return m, func() tea.Msg { return m, func() tea.Msg {
m.config.Config.Servers = append(m.config.Config.Servers, settings.CursoriusServer{})
return ScreenSwitchMsg{ return ScreenSwitchMsg{
NewScreen: createEditServer( NewScreen: createEditServer(
append(m.servers, CursoriusServer{}), m.config,
len(m.servers), 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)) fmt.Fprint(w, fn(str))
} }
func CreateLogin(servers []CursoriusServer) Login { func CreateLogin(conf config.Config[settings.Config]) Login {
items := []list.Item{} items := []list.Item{}
for _, server := range servers { for _, server := range conf.Config.Servers {
items = append( items = append(
items, items,
serverItem{ serverItem{
@@ -137,7 +141,7 @@ func CreateLogin(servers []CursoriusServer) Login {
return Login{ return Login{
selected: 0, selected: 0,
servers: servers, config: conf,
serverList: l, serverList: l,
} }
} }
+28
View File
@@ -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
}