mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-03-27 22:01:46 +02:00
add user configuration in json file
This commit is contained in:
parent
905e6c16ba
commit
29ed971558
20
main.go
20
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()
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user