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

329 lines
7.7 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 {
2021-07-27 22:03:37 +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
UserConfigFiles []string
DeafultConfFiles bool
UserConfigPath string
UserConfigDir string
TempDir 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
2021-07-27 22:03:37 +02:00
GetUserConfigFiles() []string
2019-09-15 11:19:39 +02:00
GetUserConfigDir() string
2020-10-03 06:54:55 +02:00
GetUserConfigPath() string
2021-09-27 15:39:20 +02:00
GetTempDir() 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()
2021-07-27 22:03:37 +02:00
if err != nil && !os.IsPermission(err) {
2020-10-03 06:54:55 +02:00
return nil, err
}
2021-07-27 22:03:37 +02:00
var userConfigFiles []string
2021-10-16 03:07:24 +02:00
customConfigFiles := os.Getenv("LG_CONFIG_FILE")
if customConfigFiles != "" {
2021-07-27 22:03:37 +02:00
// Load user defined config files
2021-10-16 03:07:24 +02:00
userConfigFiles = strings.Split(customConfigFiles, ",")
2021-07-27 22:03:37 +02:00
} else {
// Load default config files
userConfigFiles = []string{filepath.Join(configDir, ConfigFilename)}
}
2021-10-16 03:07:24 +02:00
userConfig, err := loadUserConfigWithDefaults(userConfigFiles)
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
}
2021-09-27 15:39:20 +02:00
tempDir := filepath.Join(os.TempDir(), "lazygit")
appState, err := loadAppState()
if err != nil {
return nil, err
}
2018-08-15 13:34:25 +02:00
appConfig := &AppConfig{
2021-10-16 03:07:24 +02:00
Name: "lazygit",
Version: version,
Commit: commit,
BuildDate: date,
Debug: debuggingFlag,
BuildSource: buildSource,
UserConfig: userConfig,
UserConfigFiles: userConfigFiles,
UserConfigDir: configDir,
UserConfigPath: filepath.Join(configDir, "config.yml"),
TempDir: tempDir,
AppState: appState,
IsNewRepo: false,
2018-08-25 07:55:49 +02:00
}
2018-08-15 13:34:25 +02:00
return appConfig, nil
}
2021-10-16 03:07:24 +02:00
func isCustomConfigFile(path string) bool {
return path != filepath.Join(ConfigDir(), ConfigFilename)
}
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()
2021-07-27 22:03:37 +02:00
return folder, os.MkdirAll(folder, 0755)
2020-10-03 06:54:55 +02:00
}
2021-10-16 03:07:24 +02:00
func loadUserConfigWithDefaults(configFiles []string) (*UserConfig, error) {
return loadUserConfig(configFiles, GetDefaultConfig())
2020-10-03 06:54:55 +02:00
}
2021-10-16 03:07:24 +02:00
func loadUserConfig(configFiles []string, base *UserConfig) (*UserConfig, error) {
for _, path := range configFiles {
if _, err := os.Stat(path); err != nil {
if !os.IsNotExist(err) {
return nil, err
2021-07-27 22:03:37 +02:00
}
2021-10-16 03:07:24 +02:00
// if use has supplied their own custom config file path(s), we assume
// the files have already been created, so we won't go and create them here.
if isCustomConfigFile(path) {
return nil, err
2021-07-27 22:03:37 +02:00
}
2020-10-03 06:54:55 +02:00
2021-10-16 03:07:24 +02:00
file, err := os.Create(path)
2020-10-03 06:54:55 +02:00
if err != nil {
2021-07-27 22:03:37 +02:00
if os.IsPermission(err) {
2021-10-16 03:07:24 +02:00
// apparently when people have read-only permissions they prefer us to fail silently
2021-07-27 22:03:37 +02:00
continue
}
2021-10-16 03:07:24 +02:00
return nil, err
2020-10-03 06:54:55 +02:00
}
file.Close()
}
2021-10-16 03:07:24 +02:00
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
2021-07-27 22:03:37 +02:00
if err := yaml.Unmarshal(content, base); err != nil {
return nil, err
}
2020-10-03 06:54:55 +02:00
}
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
}
2018-08-25 07:55:49 +02:00
// GetAppState returns the app state
func (c *AppConfig) GetAppState() *AppState {
return c.AppState
}
2021-07-27 22:03:37 +02:00
func (c *AppConfig) GetUserConfigFiles() []string {
return c.UserConfigFiles
}
2019-09-15 11:19:39 +02:00
func (c *AppConfig) GetUserConfigDir() string {
return c.UserConfigDir
}
2021-09-27 15:39:20 +02:00
func (c *AppConfig) GetTempDir() string {
return c.TempDir
}
func (c *AppConfig) ReloadUserConfig() error {
2021-10-16 03:07:24 +02:00
userConfig, err := loadUserConfigWithDefaults(c.UserConfigFiles)
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
}
2021-07-27 22:03:37 +02:00
var ConfigFilename = "config.yml"
// ConfigFilename returns the filename of the deafult config file
2020-10-03 06:54:55 +02:00
func (c *AppConfig) ConfigFilename() string {
2021-07-27 22:03:37 +02:00
return filepath.Join(c.UserConfigDir, ConfigFilename)
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
}
2021-10-16 03:07:24 +02:00
err = ioutil.WriteFile(filepath, marshalledAppState, 0644)
if err != nil && os.IsPermission(err) {
// apparently when people have read-only permissions they prefer us to fail silently
return nil
}
return err
2018-08-25 07:55:49 +02:00
}
// 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 {
2021-07-27 22:03:37 +02:00
if os.IsPermission(err) {
return getDefaultAppState(), 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")
}