summaryrefslogtreecommitdiffstats
path: root/pkg/config
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2018-08-15 21:34:25 +1000
committerJesse Duffield <jessedduffield@gmail.com>2018-08-15 22:06:37 +1000
commit29ed9715580e991eb8c2c508cbf58910d544be6d (patch)
treeda61a8d762980aa75430da251725ede9a7cb4233 /pkg/config
parent905e6c16ba6e6fbfbd01338538f730b93504303e (diff)
add user configuration in json file
Diffstat (limited to 'pkg/config')
-rw-r--r--pkg/config/app_config.go87
1 files changed, 82 insertions, 5 deletions
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
+}