add user configuration in json file

This commit is contained in:
Jesse Duffield 2018-08-15 21:34:25 +10:00
parent 905e6c16ba
commit 29ed971558
4 changed files with 93 additions and 25 deletions

20
main.go
View file

@ -4,9 +4,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"os/user"
"path/filepath" "path/filepath"
"github.com/jesseduffield/lazygit/pkg/app" "github.com/jesseduffield/lazygit/pkg/app"
@ -22,14 +20,6 @@ var (
versionFlag = flag.Bool("v", false, "Print the current version") 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 { func projectPath(path string) string {
gopath := os.Getenv("GOPATH") gopath := os.Getenv("GOPATH")
return filepath.FromSlash(gopath + "/src/github.com/jesseduffield/lazygit/" + path) 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) fmt.Printf("commit=%s, build date=%s, version=%s\n", commit, date, version)
os.Exit(0) os.Exit(0)
} }
appConfig := &config.AppConfig{ appConfig, err := config.NewAppConfig("lazygit", version, commit, date, debuggingFlag)
Name: "lazygit", if err != nil {
Version: version, panic(err)
Commit: commit,
BuildDate: date,
Debug: *debuggingFlag,
} }
app, err := app.NewApp(appConfig) app, err := app.NewApp(appConfig)
app.Log.Info(err) app.Log.Info(err)
app.GitCommand.SetupGit() app.GitCommand.SetupGit()

View file

@ -52,7 +52,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, config.GetVersion()) app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, config.GetVersion(), config.GetUserConfig())
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -1,12 +1,23 @@
package config package config
import (
"bytes"
"log"
"os"
"os/user"
"path/filepath"
"github.com/spf13/viper"
)
// AppConfig contains the base configuration fields required for lazygit. // AppConfig contains the base configuration fields required for lazygit.
type AppConfig struct { type AppConfig struct {
Debug bool `long:"debug" env:"DEBUG" default:"false"` Debug bool `long:"debug" env:"DEBUG" default:"false"`
Version string `long:"version" env:"VERSION" default:"unversioned"` Version string `long:"version" env:"VERSION" default:"unversioned"`
Commit string `long:"commit" env:"COMMIT"` Commit string `long:"commit" env:"COMMIT"`
BuildDate string `long:"build-date" env:"BUILD_DATE"` BuildDate string `long:"build-date" env:"BUILD_DATE"`
Name string `long:"name" env:"NAME" default:"lazygit"` Name string `long:"name" env:"NAME" default:"lazygit"`
UserConfig *viper.Viper
} }
// AppConfigurer interface allows individual app config structs to inherit Fields // AppConfigurer interface allows individual app config structs to inherit Fields
@ -17,6 +28,25 @@ type AppConfigurer interface {
GetCommit() string GetCommit() string
GetBuildDate() string GetBuildDate() string
GetName() 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 // GetDebug returns debug flag
@ -43,3 +73,50 @@ func (c *AppConfig) GetBuildDate() string {
func (c *AppConfig) GetName() string { func (c *AppConfig) GetName() string {
return c.Name 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
}

View file

@ -19,6 +19,7 @@ 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/spf13/viper"
) )
// OverlappingEdges determines if panel edges overlap // OverlappingEdges determines if panel edges overlap
@ -39,6 +40,7 @@ type Gui struct {
Version string Version string
SubProcess *exec.Cmd SubProcess *exec.Cmd
State guiState State guiState
Config *viper.Viper
} }
type guiState struct { type guiState struct {
@ -57,7 +59,7 @@ type guiState struct {
} }
// NewGui builds a new gui handler // 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{ initialState := guiState{
Files: make([]commands.File, 0), Files: make([]commands.File, 0),
PreviousView: "files", PreviousView: "files",
@ -77,6 +79,7 @@ func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *comm
OSCommand: oSCommand, OSCommand: oSCommand,
Version: version, Version: version,
State: initialState, State: initialState,
Config: userConfig,
}, nil }, nil
} }
@ -84,7 +87,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-1) return mainView.SetOrigin(ox, oy-gui.Config.GetInt("gui.scrollHeight"))
} }
return nil return nil
} }
@ -93,7 +96,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+1) return mainView.SetOrigin(ox, oy+gui.Config.GetInt("gui.scrollHeight"))
} }
return nil return nil
} }