1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-15 11:56:37 +02:00

support writing back to user config

This commit is contained in:
Jesse Duffield 2018-08-18 13:22:05 +10:00
parent 4dc6d40b5a
commit 10fdb5a609
3 changed files with 65 additions and 28 deletions

View File

@ -60,7 +60,7 @@ func NewApp(config config.AppConfigurer) (*App, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, app.Tr, config.GetUserConfig(), config.GetVersion()) app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, app.Tr, config)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -28,6 +28,7 @@ type AppConfigurer interface {
GetBuildDate() string GetBuildDate() string
GetName() string GetName() string
GetUserConfig() *viper.Viper GetUserConfig() *viper.Viper
InsertToUserConfig(string, string) error
} }
// NewAppConfig makes a new app config // NewAppConfig makes a new app config
@ -78,31 +79,70 @@ func (c *AppConfig) GetUserConfig() *viper.Viper {
return c.UserConfig return c.UserConfig
} }
// LoadUserConfig gets the user's config func newViper() (*viper.Viper, error) {
func LoadUserConfig() (*viper.Viper, error) {
v := viper.New() v := viper.New()
v.SetConfigType("yaml") v.SetConfigType("yaml")
defaults := getDefaultConfig() v.SetConfigName("config")
err := v.ReadConfig(bytes.NewBuffer(defaults)) return v, nil
}
// LoadUserConfig gets the user's config
func LoadUserConfig() (*viper.Viper, error) {
v, err := newViper()
if err != nil { if err != nil {
panic(err)
}
if err = LoadDefaultConfig(v); err != nil {
return nil, err return nil, err
} }
v.SetConfigName("config") if err = LoadUserConfigFromFile(v); err != nil {
return nil, err
}
return v, nil
}
// LoadDefaultConfig loads in the defaults defined in this file
func LoadDefaultConfig(v *viper.Viper) error {
defaults := getDefaultConfig()
return v.ReadConfig(bytes.NewBuffer(defaults))
}
// LoadUserConfigFromFile Loads the user config from their config file, creating
// the file as an empty config if it does not exist
func LoadUserConfigFromFile(v *viper.Viper) error {
// chucking my name there is not for vanity purposes, the xdg spec (and that // chucking my name there is not for vanity purposes, the xdg spec (and that
// function) requires a vendor name. May as well line up with github // function) requires a vendor name. May as well line up with github
configDirs := configdir.New("jesseduffield", "lazygit") configDirs := configdir.New("jesseduffield", "lazygit")
folder := configDirs.QueryFolderContainsFile("config.yml") folder := configDirs.QueryFolderContainsFile("config.yml")
if folder != nil { if folder == nil {
configData, err := folder.ReadFile("config.yml") // create the file as an empty config and load it
folders := configDirs.QueryFolders(configdir.Global)
folders[0].WriteFile("config.yml", []byte{})
folder = configDirs.QueryFolderContainsFile("config.yml")
}
v.AddConfigPath(folder.Path)
if err := v.MergeInConfig(); err != nil {
return err
}
return nil
}
// InsertToUserConfig adds a key/value pair to the user's config and saves it
func (c *AppConfig) InsertToUserConfig(key, value string) error {
// making a new viper object so that we're not writing any defaults back
// to the user's config file
v, err := newViper()
if err != nil { if err != nil {
return nil, err return err
} }
if err = v.MergeConfig(bytes.NewReader(configData)); err != nil { if err = LoadUserConfigFromFile(v); err != nil {
return nil, err return err
} }
v.Set(key, value)
if err := v.WriteConfig(); err != nil {
return err
} }
return v, nil return nil
} }
func getDefaultConfig() []byte { func getDefaultConfig() []byte {

View File

@ -19,8 +19,8 @@ import (
"github.com/golang-collections/collections/stack" "github.com/golang-collections/collections/stack"
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/spf13/viper"
) )
// OverlappingEdges determines if panel edges overlap // OverlappingEdges determines if panel edges overlap
@ -59,10 +59,9 @@ type Gui struct {
Log *logrus.Logger Log *logrus.Logger
GitCommand *commands.GitCommand GitCommand *commands.GitCommand
OSCommand *commands.OSCommand OSCommand *commands.OSCommand
Version string
SubProcess *exec.Cmd SubProcess *exec.Cmd
State guiState State guiState
Config *viper.Viper Config config.AppConfigurer
Tr *i18n.Localizer Tr *i18n.Localizer
Errors SentinelErrors Errors SentinelErrors
} }
@ -79,11 +78,10 @@ type guiState struct {
Conflicts []commands.Conflict Conflicts []commands.Conflict
EditHistory *stack.Stack EditHistory *stack.Stack
Platform commands.Platform Platform commands.Platform
Version string
} }
// NewGui builds a new gui handler // NewGui builds a new gui handler
func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, tr *i18n.Localizer, userConfig *viper.Viper, version string) (*Gui, error) { func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, tr *i18n.Localizer, config config.AppConfigurer) (*Gui, error) {
initialState := guiState{ initialState := guiState{
Files: make([]commands.File, 0), Files: make([]commands.File, 0),
PreviousView: "files", PreviousView: "files",
@ -94,16 +92,14 @@ func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *comm
Conflicts: make([]commands.Conflict, 0), Conflicts: make([]commands.Conflict, 0),
EditHistory: stack.New(), EditHistory: stack.New(),
Platform: *oSCommand.Platform, Platform: *oSCommand.Platform,
Version: version,
} }
gui := &Gui{ gui := &Gui{
Log: log, Log: log,
GitCommand: gitCommand, GitCommand: gitCommand,
OSCommand: oSCommand, OSCommand: oSCommand,
Version: version,
State: initialState, State: initialState,
Config: userConfig, Config: config,
Tr: tr, Tr: tr,
} }
@ -116,7 +112,7 @@ func (gui *Gui) scrollUpMain(g *gocui.Gui, v *gocui.View) error {
mainView, _ := g.View("main") mainView, _ := g.View("main")
ox, oy := mainView.Origin() ox, oy := mainView.Origin()
if oy >= 1 { if oy >= 1 {
return mainView.SetOrigin(ox, oy-gui.Config.GetInt("gui.scrollHeight")) return mainView.SetOrigin(ox, oy-gui.Config.GetUserConfig().GetInt("gui.scrollHeight"))
} }
return nil return nil
} }
@ -125,7 +121,7 @@ func (gui *Gui) scrollDownMain(g *gocui.Gui, v *gocui.View) error {
mainView, _ := g.View("main") mainView, _ := g.View("main")
ox, oy := mainView.Origin() ox, oy := mainView.Origin()
if oy < len(mainView.BufferLines()) { if oy < len(mainView.BufferLines()) {
return mainView.SetOrigin(ox, oy+gui.Config.GetInt("gui.scrollHeight")) return mainView.SetOrigin(ox, oy+gui.Config.GetUserConfig().GetInt("gui.scrollHeight"))
} }
return nil return nil
} }
@ -153,6 +149,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
commitsStashBoundary := height - 5 // height - 5 commitsStashBoundary := height - 5 // height - 5
minimumHeight := 16 minimumHeight := 16
minimumWidth := 10 minimumWidth := 10
version := gui.Config.GetVersion()
panelSpacing := 1 panelSpacing := 1
if OverlappingEdges { if OverlappingEdges {
@ -231,7 +228,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
v.FgColor = gocui.ColorWhite v.FgColor = gocui.ColorWhite
} }
if v, err := g.SetView("options", -1, optionsTop, width-len(gui.Version)-2, optionsTop+2, 0); err != nil { if v, err := g.SetView("options", -1, optionsTop, width-len(version)-2, optionsTop+2, 0); err != nil {
if err != gocui.ErrUnknownView { if err != gocui.ErrUnknownView {
return err return err
} }
@ -252,14 +249,14 @@ func (gui *Gui) layout(g *gocui.Gui) error {
} }
} }
if v, err := g.SetView("version", width-len(gui.Version)-1, optionsTop, width, optionsTop+2, 0); err != nil { if v, err := g.SetView("version", width-len(version)-1, optionsTop, width, optionsTop+2, 0); err != nil {
if err != gocui.ErrUnknownView { if err != gocui.ErrUnknownView {
return err return err
} }
v.BgColor = gocui.ColorDefault v.BgColor = gocui.ColorDefault
v.FgColor = gocui.ColorGreen v.FgColor = gocui.ColorGreen
v.Frame = false v.Frame = false
gui.renderString(g, "version", gui.Version) gui.renderString(g, "version", version)
// these are only called once // these are only called once
gui.handleFileSelect(g, filesView) gui.handleFileSelect(g, filesView)