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 --- pkg/config/app_config.go | 87 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 5 deletions(-) (limited to 'pkg/config') 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 +} -- cgit v1.2.3