1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/config/app_config.go

290 lines
6.5 KiB
Go
Raw Normal View History

package config
2018-08-15 13:34:25 +02:00
import (
2018-08-25 07:55:49 +02:00
"io/ioutil"
2019-02-18 12:29:43 +02:00
"os"
2018-08-25 07:55:49 +02:00
"path/filepath"
"strings"
2018-08-15 13:34:25 +02:00
2020-10-03 06:54:55 +02:00
"github.com/OpenPeeDeeP/xdg"
yaml "github.com/jesseduffield/yaml"
2018-08-15 13:34:25 +02:00
)
// AppConfig contains the base configuration fields required for lazygit.
type AppConfig struct {
2020-10-03 06:54:55 +02:00
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"`
BuildSource string `long:"build-source" env:"BUILD_SOURCE" default:""`
UserConfig *UserConfig
UserConfigDir string
UserConfigPath string
AppState *AppState
IsNewRepo bool
}
// 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
2018-08-25 07:55:49 +02:00
GetBuildSource() string
2020-10-03 06:54:55 +02:00
GetUserConfig() *UserConfig
2019-09-15 11:19:39 +02:00
GetUserConfigDir() string
2020-10-03 06:54:55 +02:00
GetUserConfigPath() string
2018-08-25 07:55:49 +02:00
GetAppState() *AppState
SaveAppState() error
2018-12-10 14:45:03 +02:00
SetIsNewRepo(bool)
GetIsNewRepo() bool
ReloadUserConfig() error
2018-08-15 13:34:25 +02:00
}
// NewAppConfig makes a new app config
2019-02-18 12:29:43 +02:00
func NewAppConfig(name, version, commit, date string, buildSource string, debuggingFlag bool) (*AppConfig, error) {
2020-10-04 00:53:56 +02:00
configDir, err := findOrCreateConfigDir()
2020-10-03 06:54:55 +02:00
if err != nil {
return nil, err
}
userConfig, err := loadUserConfigWithDefaults(configDir)
2018-08-15 13:34:25 +02:00
if err != nil {
2018-08-25 07:55:49 +02:00
return nil, err
2018-08-15 13:34:25 +02:00
}
2019-02-18 12:29:43 +02:00
if os.Getenv("DEBUG") == "TRUE" {
debuggingFlag = true
}
appState, err := loadAppState()
if err != nil {
return nil, err
}
2018-08-15 13:34:25 +02:00
appConfig := &AppConfig{
2020-10-03 06:54:55 +02:00
Name: "lazygit",
Version: version,
Commit: commit,
BuildDate: date,
Debug: debuggingFlag,
BuildSource: buildSource,
UserConfig: userConfig,
UserConfigDir: configDir,
UserConfigPath: filepath.Join(configDir, "config.yml"),
AppState: appState,
2020-10-03 06:54:55 +02:00
IsNewRepo: false,
2018-08-25 07:55:49 +02:00
}
2018-08-15 13:34:25 +02:00
return appConfig, nil
}
2020-11-22 15:51:48 +02:00
func ConfigDir() string {
legacyConfigDirectory := configDirForVendor("jesseduffield")
2020-11-21 17:17:35 +02:00
if _, err := os.Stat(legacyConfigDirectory); !os.IsNotExist(err) {
return legacyConfigDirectory
}
2020-11-22 15:51:48 +02:00
configDirectory := configDirForVendor("")
2020-11-21 17:17:35 +02:00
return configDirectory
}
2020-11-22 15:51:48 +02:00
func configDirForVendor(vendor string) string {
2020-10-04 13:05:39 +02:00
envConfigDir := os.Getenv("CONFIG_DIR")
if envConfigDir != "" {
return envConfigDir
}
configDirs := xdg.New(vendor, "lazygit")
2020-10-04 00:53:56 +02:00
return configDirs.ConfigHome()
}
func findOrCreateConfigDir() (string, error) {
2020-11-22 15:51:48 +02:00
folder := ConfigDir()
2020-10-03 06:54:55 +02:00
err := os.MkdirAll(folder, 0755)
if err != nil {
return "", err
}
return folder, nil
}
func loadUserConfigWithDefaults(configDir string) (*UserConfig, error) {
return loadUserConfig(configDir, GetDefaultConfig())
}
func loadUserConfig(configDir string, base *UserConfig) (*UserConfig, error) {
fileName := filepath.Join(configDir, "config.yml")
if _, err := os.Stat(fileName); err != nil {
if os.IsNotExist(err) {
file, err := os.Create(fileName)
if err != nil {
if strings.Contains(err.Error(), "read-only file system") {
return base, nil
}
2020-10-03 06:54:55 +02:00
return nil, err
}
file.Close()
} else {
return nil, err
}
}
content, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(content, base); err != nil {
return nil, err
}
return base, nil
}
2018-12-10 14:45:03 +02:00
// GetIsNewRepo returns known repo boolean
func (c *AppConfig) GetIsNewRepo() bool {
return c.IsNewRepo
}
// SetIsNewRepo set if the current repo is known
func (c *AppConfig) SetIsNewRepo(toSet bool) {
c.IsNewRepo = toSet
}
// 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
}
2018-08-15 13:34:25 +02:00
2018-08-25 07:55:49 +02:00
// GetBuildSource returns the source of the build. For builds from goreleaser
// this will be binaryBuild
func (c *AppConfig) GetBuildSource() string {
return c.BuildSource
}
2018-08-15 13:34:25 +02:00
// GetUserConfig returns the user config
2020-10-03 06:54:55 +02:00
func (c *AppConfig) GetUserConfig() *UserConfig {
2018-08-15 13:34:25 +02:00
return c.UserConfig
}
2020-10-03 06:54:55 +02:00
// GetUserConfig returns the user config
func (c *AppConfig) GetUserConfigPath() string {
return c.UserConfigPath
}
2018-08-25 07:55:49 +02:00
// GetAppState returns the app state
func (c *AppConfig) GetAppState() *AppState {
return c.AppState
}
2019-09-15 11:19:39 +02:00
func (c *AppConfig) GetUserConfigDir() string {
return c.UserConfigDir
}
func (c *AppConfig) ReloadUserConfig() error {
userConfig, err := loadUserConfigWithDefaults(c.UserConfigDir)
if err != nil {
return err
}
c.UserConfig = userConfig
return nil
}
2020-10-03 06:54:55 +02:00
func configFilePath(filename string) (string, error) {
2020-10-04 00:53:56 +02:00
folder, err := findOrCreateConfigDir()
if err != nil {
2020-10-03 06:54:55 +02:00
return "", err
2018-08-18 05:22:05 +02:00
}
2020-10-03 06:54:55 +02:00
return filepath.Join(folder, filename), nil
2018-08-18 05:22:05 +02:00
}
2020-10-03 06:54:55 +02:00
// ConfigFilename returns the filename of the current config file
func (c *AppConfig) ConfigFilename() string {
return filepath.Join(c.UserConfigDir, "config.yml")
2018-08-15 13:34:25 +02:00
}
2019-05-22 15:34:29 +02:00
// SaveAppState marshalls the AppState struct and writes it to the disk
2018-08-25 07:55:49 +02:00
func (c *AppConfig) SaveAppState() error {
marshalledAppState, err := yaml.Marshal(c.AppState)
if err != nil {
return err
}
2020-10-03 06:54:55 +02:00
filepath, err := configFilePath("state.yml")
2018-08-25 07:55:49 +02:00
if err != nil {
return err
}
return ioutil.WriteFile(filepath, marshalledAppState, 0644)
}
// loadAppState loads recorded AppState from file
func loadAppState() (*AppState, error) {
2020-10-03 06:54:55 +02:00
filepath, err := configFilePath("state.yml")
2018-08-25 07:55:49 +02:00
if err != nil {
return nil, err
2018-08-25 07:55:49 +02:00
}
2018-08-25 07:55:49 +02:00
appStateBytes, err := ioutil.ReadFile(filepath)
2020-10-04 13:05:39 +02:00
if err != nil && !os.IsNotExist(err) {
return nil, err
2018-08-25 07:55:49 +02:00
}
2018-08-25 07:55:49 +02:00
if len(appStateBytes) == 0 {
return getDefaultAppState(), nil
}
appState := &AppState{}
err = yaml.Unmarshal(appStateBytes, appState)
if err != nil {
return nil, err
2018-08-25 07:55:49 +02:00
}
return appState, nil
2018-08-25 07:55:49 +02:00
}
// AppState stores data between runs of the app like when the last update check
// was performed and which other repos have been checked out
type AppState struct {
LastUpdateCheck int64
RecentRepos []string
StartupPopupVersion int
2018-08-25 07:55:49 +02:00
}
func getDefaultAppState() *AppState {
return &AppState{
LastUpdateCheck: 0,
RecentRepos: []string{},
StartupPopupVersion: 0,
}
2018-08-15 13:34:25 +02:00
}
2020-10-03 06:54:55 +02:00
func LogPath() (string, error) {
return configFilePath("development.log")
}