summaryrefslogtreecommitdiffstats
path: root/pkg/config/app_config.go
blob: 68c065e33f2f6cceae281146ae032da84be0216f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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"`
	UserConfig *viper.Viper
}

// AppConfigurer interface allows individual app config structs to inherit Fields
// from AppConfig and still be used by lazygit.
type AppConfigurer interface {
	GetDebug() bool
	GetVersion() string
	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
func (c *AppConfig) GetDebug() bool {
	return c.Debug
}

// GetVersion returns debug flag
func (c *AppConfig) GetVersion() string {
	return c.Version
}

// GetCommit returns debug flag
func (c *AppConfig) GetCommit() string {
	return c.Commit
}

// GetBuildDate returns debug flag
func (c *AppConfig) GetBuildDate() string {
	return c.BuildDate
}

// GetName returns debug flag
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("yaml")
	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.yaml")); !os.IsNotExist(err) {
		v.AddConfigPath(configPath)
		err = v.MergeInConfig()
		if err != nil {
			return nil, err
		}
	}

	return v, nil
}

func getDefaultConfig() []byte {
	return []byte(`
  gui:
    ## stuff relating to the UI
    scrollHeight: 2
  git:
    # stuff relating to git
  os:
    # stuff relating to the OS

`)
}

func homeDirectory() string {
	usr, err := user.Current()
	if err != nil {
		log.Fatal(err)
	}
	return usr.HomeDir
}