small changes

This commit is contained in:
Jesse Duffield 2021-10-16 12:07:24 +11:00
parent 913a2fd065
commit d02e52989e
8 changed files with 73 additions and 68 deletions

View file

@ -265,10 +265,19 @@ os:
`{{editor}}` in `editCommandTemplate` is replaced with the value of `editCommand`. `{{editor}}` in `editCommandTemplate` is replaced with the value of `editCommand`.
### Change config file used ### Overriding default config file location
- Use `--config-file=~/.base_lg_conf,~/.light_theme_lg_conf` or `$LG_CONFIG_FILE="~/.base_lg_conf,~/.light_theme_lg_conf"` To override the default config directory, use `$CONFIG_DIR="~/.config/lazygit"`. This directory contains the config file in addition to some other files lazygit uses to keep track of state across sessions.
- Change the default config directory `$CONFIG_DIR="~/.config/lazygit"`
To override the individual config file used, use the `--use-config-file` arg or the `LG_CONFIG_FILE` env var.
If you want to merge a specific config file into a more general config file, perhaps for the sake of setting some theme-specific options, you can supply a list of comma-separated config file paths, like so:
```sh
lazygit --use-config-file=~/.base_lg_conf,~/.light_theme_lg_conf
or
LG_CONFIG_FILE="~/.base_lg_conf,~/.light_theme_lg_conf" lazygit
```
### Recommended Config Values ### Recommended Config Values

View file

@ -62,7 +62,7 @@ func main() {
flaggy.String(&gitDir, "g", "git-dir", "equivalent of the --git-dir git argument") flaggy.String(&gitDir, "g", "git-dir", "equivalent of the --git-dir git argument")
customConfig := "" customConfig := ""
flaggy.String(&customConfig, "cf", "config-file", "Comma seperated list to custom config file(s)") flaggy.String(&customConfig, "ucf", "use-config-file", "Comma seperated list to custom config file(s)")
flaggy.Parse() flaggy.Parse()

View file

@ -57,19 +57,16 @@ func NewAppConfig(name, version, commit, date string, buildSource string, debugg
} }
var userConfigFiles []string var userConfigFiles []string
customConfigFiles := os.Getenv("LG_CONFIG_FILE")
userConfigFilesOverwrite := os.Getenv("LG_CONFIG_FILE") if customConfigFiles != "" {
deafultConfFiles := true
if userConfigFilesOverwrite != "" {
// Load user defined config files // Load user defined config files
userConfigFiles = strings.Split(userConfigFilesOverwrite, ",") userConfigFiles = strings.Split(customConfigFiles, ",")
deafultConfFiles = false
} else { } else {
// Load default config files // Load default config files
userConfigFiles = []string{filepath.Join(configDir, ConfigFilename)} userConfigFiles = []string{filepath.Join(configDir, ConfigFilename)}
} }
userConfig, err := loadUserConfigWithDefaults(userConfigFiles, deafultConfFiles) userConfig, err := loadUserConfigWithDefaults(userConfigFiles)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -86,25 +83,28 @@ func NewAppConfig(name, version, commit, date string, buildSource string, debugg
} }
appConfig := &AppConfig{ appConfig := &AppConfig{
Name: "lazygit", Name: "lazygit",
Version: version, Version: version,
Commit: commit, Commit: commit,
BuildDate: date, BuildDate: date,
Debug: debuggingFlag, Debug: debuggingFlag,
BuildSource: buildSource, BuildSource: buildSource,
UserConfig: userConfig, UserConfig: userConfig,
UserConfigFiles: userConfigFiles, UserConfigFiles: userConfigFiles,
UserConfigDir: configDir, UserConfigDir: configDir,
DeafultConfFiles: deafultConfFiles, UserConfigPath: filepath.Join(configDir, "config.yml"),
UserConfigPath: filepath.Join(configDir, "config.yml"), TempDir: tempDir,
TempDir: tempDir, AppState: appState,
AppState: appState, IsNewRepo: false,
IsNewRepo: false,
} }
return appConfig, nil return appConfig, nil
} }
func isCustomConfigFile(path string) bool {
return path != filepath.Join(ConfigDir(), ConfigFilename)
}
func ConfigDir() string { func ConfigDir() string {
legacyConfigDirectory := configDirForVendor("jesseduffield") legacyConfigDirectory := configDirForVendor("jesseduffield")
if _, err := os.Stat(legacyConfigDirectory); !os.IsNotExist(err) { if _, err := os.Stat(legacyConfigDirectory); !os.IsNotExist(err) {
@ -128,37 +128,39 @@ func findOrCreateConfigDir() (string, error) {
return folder, os.MkdirAll(folder, 0755) return folder, os.MkdirAll(folder, 0755)
} }
func loadUserConfigWithDefaults(configFiles []string, deafultConfFiles bool) (*UserConfig, error) { func loadUserConfigWithDefaults(configFiles []string) (*UserConfig, error) {
return loadUserConfig(configFiles, GetDefaultConfig(), deafultConfFiles) return loadUserConfig(configFiles, GetDefaultConfig())
} }
func loadUserConfig(configFiles []string, base *UserConfig, deafultConfFiles bool) (*UserConfig, error) { func loadUserConfig(configFiles []string, base *UserConfig) (*UserConfig, error) {
for _, fileName := range configFiles { for _, path := range configFiles {
content, readConfFileErr := ioutil.ReadFile(fileName) if _, err := os.Stat(path); err != nil {
if readConfFileErr != nil {
if !deafultConfFiles {
return nil, readConfFileErr
}
_, err := os.Stat(fileName)
if err == nil {
return nil, readConfFileErr
}
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return nil, readConfFileErr return nil, err
} }
file, err := os.Create(fileName) // 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
}
file, err := os.Create(path)
if err != nil { if err != nil {
if os.IsPermission(err) { if os.IsPermission(err) {
// apparently when people have read-only permissions they prefer us to fail silently
continue continue
} else {
return nil, readConfFileErr
} }
return nil, err
} }
file.Close() file.Close()
} }
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(content, base); err != nil { if err := yaml.Unmarshal(content, base); err != nil {
return nil, err return nil, err
} }
@ -231,7 +233,7 @@ func (c *AppConfig) GetTempDir() string {
} }
func (c *AppConfig) ReloadUserConfig() error { func (c *AppConfig) ReloadUserConfig() error {
userConfig, err := loadUserConfigWithDefaults(c.UserConfigFiles, c.DeafultConfFiles) userConfig, err := loadUserConfigWithDefaults(c.UserConfigFiles)
if err != nil { if err != nil {
return err return err
} }
@ -268,7 +270,13 @@ func (c *AppConfig) SaveAppState() error {
return err return err
} }
return ioutil.WriteFile(filepath, marshalledAppState, 0644) 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
} }
// loadAppState loads recorded AppState from file // loadAppState loads recorded AppState from file

View file

@ -656,11 +656,7 @@ func (gui *Gui) showIntroPopupMessage(done chan struct{}) error {
onConfirm := func() error { onConfirm := func() error {
done <- struct{}{} done <- struct{}{}
gui.Config.GetAppState().StartupPopupVersion = StartupPopupVersion gui.Config.GetAppState().StartupPopupVersion = StartupPopupVersion
err := gui.Config.SaveAppState() return gui.Config.SaveAppState()
if err != nil && os.IsPermission(err) {
return nil
}
return err
} }
return gui.ask(askOpts{ return gui.ask(askOpts{

View file

@ -114,11 +114,7 @@ func (gui *Gui) updateRecentRepoList() error {
known, recentRepos := newRecentReposList(recentRepos, currentRepo) known, recentRepos := newRecentReposList(recentRepos, currentRepo)
gui.Config.SetIsNewRepo(known) gui.Config.SetIsNewRepo(known)
gui.Config.GetAppState().RecentRepos = recentRepos gui.Config.GetAppState().RecentRepos = recentRepos
err = gui.Config.SaveAppState() return gui.Config.SaveAppState()
if err != nil && os.IsPermission(err) {
return nil
}
return err
} }
// newRecentReposList returns a new repo list with a new entry but only when it doesn't exist yet // newRecentReposList returns a new repo list with a new entry but only when it doesn't exist yet

View file

@ -119,7 +119,7 @@ func (gui *Gui) askForConfigFile(action func(file string) error) error {
confFiles := gui.Config.GetUserConfigFiles() confFiles := gui.Config.GetUserConfigFiles()
switch len(confFiles) { switch len(confFiles) {
case 0: case 0:
return errors.New("no config file found") return errors.New(gui.Tr.NoConfigFileFoundErr)
case 1: case 1:
return action(confFiles[0]) return action(confFiles[0])
default: default:
@ -133,20 +133,16 @@ func (gui *Gui) askForConfigFile(action func(file string) error) error {
}, },
} }
} }
return gui.createMenu("select config file", menuItems, createMenuOptions{}) return gui.createMenu(gui.Tr.SelectConfigFile, menuItems, createMenuOptions{})
} }
} }
func (gui *Gui) handleOpenConfig() error { func (gui *Gui) handleOpenConfig() error {
return gui.askForConfigFile(func(file string) error { return gui.askForConfigFile(gui.openFile)
return gui.openFile(file)
})
} }
func (gui *Gui) handleEditConfig() error { func (gui *Gui) handleEditConfig() error {
return gui.askForConfigFile(func(file string) error { return gui.askForConfigFile(gui.editFile)
return gui.editFile(file)
})
} }
func lazygitTitle() string { func lazygitTitle() string {

View file

@ -427,6 +427,8 @@ type TranslationSet struct {
LcSelectBranch string LcSelectBranch string
CreatePullRequest string CreatePullRequest string
CreatingPullRequestAtUrl string CreatingPullRequestAtUrl string
SelectConfigFile string
NoConfigFileFoundErr string
Spans Spans Spans Spans
} }
@ -947,6 +949,8 @@ func englishTranslationSet() TranslationSet {
LcDefaultBranch: "default branch", LcDefaultBranch: "default branch",
LcSelectBranch: "select branch", LcSelectBranch: "select branch",
CreatingPullRequestAtUrl: "Creating pull request at URL: %s", CreatingPullRequestAtUrl: "Creating pull request at URL: %s",
SelectConfigFile: "Select config file",
NoConfigFileFoundErr: "No config file found",
Spans: Spans{ Spans: Spans{
// TODO: combine this with the original keybinding descriptions (those are all in lowercase atm) // TODO: combine this with the original keybinding descriptions (those are all in lowercase atm)
CheckoutCommit: "Checkout commit", CheckoutCommit: "Checkout commit",

View file

@ -76,11 +76,7 @@ func (u *Updater) getLatestVersionNumber() (string, error) {
// RecordLastUpdateCheck records last time an update check was performed // RecordLastUpdateCheck records last time an update check was performed
func (u *Updater) RecordLastUpdateCheck() error { func (u *Updater) RecordLastUpdateCheck() error {
u.Config.GetAppState().LastUpdateCheck = time.Now().Unix() u.Config.GetAppState().LastUpdateCheck = time.Now().Unix()
err := u.Config.SaveAppState() return u.Config.SaveAppState()
if err != nil && os.IsPermission(err) {
return nil
}
return err
} }
// expecting version to be of the form `v12.34.56` // expecting version to be of the form `v12.34.56`