From 29ed9715580e991eb8c2c508cbf58910d544be6d Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Wed, 15 Aug 2018 21:34:25 +1000 Subject: add user configuration in json file --- main.go | 20 +++-------- pkg/app/app.go | 2 +- pkg/config/app_config.go | 87 +++++++++++++++++++++++++++++++++++++++++++++--- pkg/gui/gui.go | 9 +++-- 4 files changed, 93 insertions(+), 25 deletions(-) diff --git a/main.go b/main.go index c7fec1ae1..85e0f36bb 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,7 @@ import ( "flag" "fmt" "io/ioutil" - "log" "os" - "os/user" "path/filepath" "github.com/jesseduffield/lazygit/pkg/app" @@ -22,14 +20,6 @@ var ( versionFlag = flag.Bool("v", false, "Print the current version") ) -func homeDirectory() string { - usr, err := user.Current() - if err != nil { - log.Fatal(err) - } - return usr.HomeDir -} - func projectPath(path string) string { gopath := os.Getenv("GOPATH") return filepath.FromSlash(gopath + "/src/github.com/jesseduffield/lazygit/" + path) @@ -56,13 +46,11 @@ func main() { fmt.Printf("commit=%s, build date=%s, version=%s\n", commit, date, version) os.Exit(0) } - appConfig := &config.AppConfig{ - Name: "lazygit", - Version: version, - Commit: commit, - BuildDate: date, - Debug: *debuggingFlag, + appConfig, err := config.NewAppConfig("lazygit", version, commit, date, debuggingFlag) + if err != nil { + panic(err) } + app, err := app.NewApp(appConfig) app.Log.Info(err) app.GitCommand.SetupGit() diff --git a/pkg/app/app.go b/pkg/app/app.go index d558ed250..0eb9c8d25 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -52,7 +52,7 @@ func NewApp(config config.AppConfigurer) (*App, error) { if err != nil { return nil, err } - app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, config.GetVersion()) + app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, config.GetVersion(), config.GetUserConfig()) if err != nil { return nil, err } diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 98e56dea2..ec3414a55 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -1,12 +1,23 @@ package config +import ( + "bytes" + "log" + "os" + "os/user" + "path/filepath" + + "github.com/spf13/viper" +) + // AppConfig contains the base configuration fields required for lazygit. type AppConfig struct { - Debug bool `long:"debug" env:"DEBUG" default:"false"` - Version string `long:"version" env:"VERSION" default:"unversioned"` - Commit string `long:"commit" env:"COMMIT"` - BuildDate string `long:"build-date" env:"BUILD_DATE"` - Name string `long:"name" env:"NAME" default:"lazygit"` + Debug bool `long:"debug" env:"DEBUG" default:"false"` + Version string `long:"version" env:"VERSION" default:"unversioned"` + Commit string `long:"commit" env:"COMMIT"` + BuildDate string `long:"build-date" env:"BUILD_DATE"` + Name string `long:"name" env:"NAME" default:"lazygit"` + UserConfig *viper.Viper } // AppConfigurer interface allows individual app config structs to inherit Fields @@ -17,6 +28,25 @@ type AppConfigurer interface { GetCommit() string GetBuildDate() string GetName() string + GetUserConfig() *viper.Viper +} + +// NewAppConfig makes a new app config +func NewAppConfig(name, version, commit, date string, debuggingFlag *bool) (*AppConfig, error) { + userConfig, err := LoadUserConfig() + if err != nil { + panic(err) + } + + appConfig := &AppConfig{ + Name: "lazygit", + Version: version, + Commit: commit, + BuildDate: date, + Debug: *debuggingFlag, + UserConfig: userConfig, + } + return appConfig, nil } // GetDebug returns debug flag @@ -43,3 +73,50 @@ func (c *AppConfig) GetBuildDate() string { func (c *AppConfig) GetName() string { return c.Name } + +// GetUserConfig returns the user config +func (c *AppConfig) GetUserConfig() *viper.Viper { + return c.UserConfig +} + +// LoadUserConfig gets the user's config +func LoadUserConfig() (*viper.Viper, error) { + v := viper.New() + v.SetConfigType("json") + defaults := getDefaultConfig() + err := v.ReadConfig(bytes.NewBuffer(defaults)) + if err != nil { + return nil, err + } + v.SetConfigName("config") + configPath := homeDirectory() + "/lazygit/" + if _, err := os.Stat(filepath.FromSlash(configPath + "config.json")); !os.IsNotExist(err) { + v.AddConfigPath(configPath) + err = v.MergeInConfig() + if err != nil { + return nil, err + } + } + + return v, nil +} + +func getDefaultConfig() []byte { + return []byte(` + { + "gui": { + "scrollHeight": 1 + }, + "git": {}, + "os": {} + } +`) +} + +func homeDirectory() string { + usr, err := user.Current() + if err != nil { + log.Fatal(err) + } + return usr.HomeDir +} diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 3b7876ede..b5b19a892 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -19,6 +19,7 @@ import ( "github.com/golang-collections/collections/stack" "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands" + "github.com/spf13/viper" ) // OverlappingEdges determines if panel edges overlap @@ -39,6 +40,7 @@ type Gui struct { Version string SubProcess *exec.Cmd State guiState + Config *viper.Viper } type guiState struct { @@ -57,7 +59,7 @@ type guiState struct { } // NewGui builds a new gui handler -func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, version string) (*Gui, error) { +func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, version string, userConfig *viper.Viper) (*Gui, error) { initialState := guiState{ Files: make([]commands.File, 0), PreviousView: "files", @@ -77,6 +79,7 @@ func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *comm OSCommand: oSCommand, Version: version, State: initialState, + Config: userConfig, }, nil } @@ -84,7 +87,7 @@ func (gui *Gui) scrollUpMain(g *gocui.Gui, v *gocui.View) error { mainView, _ := g.View("main") ox, oy := mainView.Origin() if oy >= 1 { - return mainView.SetOrigin(ox, oy-1) + return mainView.SetOrigin(ox, oy-gui.Config.GetInt("gui.scrollHeight")) } return nil } @@ -93,7 +96,7 @@ func (gui *Gui) scrollDownMain(g *gocui.Gui, v *gocui.View) error { mainView, _ := g.View("main") ox, oy := mainView.Origin() if oy < len(mainView.BufferLines()) { - return mainView.SetOrigin(ox, oy+1) + return mainView.SetOrigin(ox, oy+gui.Config.GetInt("gui.scrollHeight")) } return nil } -- cgit v1.2.3