support writing back config values to user config

This commit is contained in:
Jesse Duffield 2019-06-02 11:52:26 +10:00
parent 664058d5f0
commit 46d27c477e
2 changed files with 25 additions and 36 deletions

View file

@ -77,40 +77,40 @@ func loadUserConfig(configDir string, base *UserConfig) (*UserConfig, error) {
} }
type UserConfig struct { type UserConfig struct {
Gui GuiConfig Gui GuiConfig `yaml:"gui,omitempty"`
Reporting string Reporting string `yaml:"reporting,omitempty"`
ConfirmOnQuit bool ConfirmOnQuit bool `yaml:"confirmOnQuit,omitempty"`
CommandTemplates CommandTemplatesConfig CommandTemplates CommandTemplatesConfig `yaml:"commandTemplates,omitempty"`
OS OSConfig OS OSConfig `yaml:"oS,omitempty"`
Update UpdateConfig Update UpdateConfig `yaml:"update,omitempty"`
} }
type ThemeConfig struct { type ThemeConfig struct {
ActiveBorderColor []string ActiveBorderColor []string `yaml:"activeBorderColor,omitempty"`
InactiveBorderColor []string InactiveBorderColor []string `yaml:"inactiveBorderColor,omitempty"`
OptionsTextColor []string OptionsTextColor []string `yaml:"optionsTextColor,omitempty"`
} }
type GuiConfig struct { type GuiConfig struct {
ScrollHeight int ScrollHeight int `yaml:"scrollHeight,omitempty"`
ScrollPastBottom bool ScrollPastBottom bool `yaml:"scrollPastBottom,omitempty"`
MouseEvents bool MouseEvents bool `yaml:"mouseEvents,omitempty"`
Theme ThemeConfig Theme ThemeConfig `yaml:"theme,omitempty"`
} }
type CommandTemplatesConfig struct { type CommandTemplatesConfig struct {
RestartService string RestartService string `yaml:"restartService,omitempty"`
DockerCompose string DockerCompose string `yaml:"dockerCompose,omitempty"`
StopService string StopService string `yaml:"stopService,omitempty"`
} }
type OSConfig struct { type OSConfig struct {
OpenCommand string OpenCommand string `yaml:"openCommand,omitempty"`
OpenLinkCommand string OpenLinkCommand string `yaml:"openLinkCommand,omitempty"`
} }
type UpdateConfig struct { type UpdateConfig struct {
Method string Method string `yaml:"method,omitempty"`
} }
// GetDefaultConfig returns the application default configuration // GetDefaultConfig returns the application default configuration
@ -140,18 +140,10 @@ func GetDefaultConfig() UserConfig {
} }
} }
// AppState stores data between runs of the app like when the last update check // WriteToUserConfig allows you to set a value on the user config to be saved
// was performed and which other repos have been checked out // note that if you set a zero-value, it may be ignored e.g. a false or 0 or empty string
type AppState struct { // this is because we are using the omitempty yaml directive so that we don't write a heap
LastUpdateCheck int64 // of zero values to the user's config.yml
}
func getDefaultAppState() []byte {
return []byte(`
lastUpdateCheck: 0
`)
}
func (c *AppConfig) WriteToUserConfig(updateConfig func(*UserConfig) error) error { func (c *AppConfig) WriteToUserConfig(updateConfig func(*UserConfig) error) error {
userConfig, err := loadUserConfig(c.ConfigDir, &UserConfig{}) userConfig, err := loadUserConfig(c.ConfigDir, &UserConfig{})
if err != nil { if err != nil {

View file

@ -2,7 +2,6 @@ package gui
import ( import (
"fmt" "fmt"
"path/filepath"
"strings" "strings"
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
@ -47,13 +46,11 @@ func (gui *Gui) handleStatusSelect(g *gocui.Gui, v *gocui.View) error {
} }
func (gui *Gui) handleOpenConfig(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) handleOpenConfig(g *gocui.Gui, v *gocui.View) error {
filename := filepath.Join(gui.Config.ConfigDir, "config.yml") return gui.openFile(gui.Config.ConfigFilename())
return gui.openFile(filename)
} }
func (gui *Gui) handleEditConfig(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) handleEditConfig(g *gocui.Gui, v *gocui.View) error {
filename := filepath.Join(gui.Config.ConfigDir, "config.yml") return gui.editFile(gui.Config.ConfigFilename())
return gui.editFile(filename)
} }
func lazydockerTitle() string { func lazydockerTitle() string {