mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-08-08 22:36:49 +02:00
Print migration hints only when GUI hasn't started yet
Most migrations happen at startup when loading the global config file, at a time where the GUI hasn't been initialized yet. We can safely print to the console at that point. However, it is also possible that repo-local config files need to be migrated, and this happens when the GUI has already started, at which point we had better not print anything to stdout; this totally messes up the UI. In this commit we simply suppress the logging when the GUI is running already. This is probably good enough, because the logging is mostly useful in the case that writing back the migrated config file fails, so that users understand better why lazygit doesn't start up; and this is very unlikely to happen for repo-local config files, because why would users make them read-only.
This commit is contained in:
@@ -96,7 +96,7 @@ func NewAppConfig(
|
|||||||
configFiles = []*ConfigFile{configFile}
|
configFiles = []*ConfigFile{configFile}
|
||||||
}
|
}
|
||||||
|
|
||||||
userConfig, err := loadUserConfigWithDefaults(configFiles)
|
userConfig, err := loadUserConfigWithDefaults(configFiles, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -145,11 +145,11 @@ func findOrCreateConfigDir() (string, error) {
|
|||||||
return folder, os.MkdirAll(folder, 0o755)
|
return folder, os.MkdirAll(folder, 0o755)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadUserConfigWithDefaults(configFiles []*ConfigFile) (*UserConfig, error) {
|
func loadUserConfigWithDefaults(configFiles []*ConfigFile, isGuiInitialized bool) (*UserConfig, error) {
|
||||||
return loadUserConfig(configFiles, GetDefaultConfig())
|
return loadUserConfig(configFiles, GetDefaultConfig(), isGuiInitialized)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadUserConfig(configFiles []*ConfigFile, base *UserConfig) (*UserConfig, error) {
|
func loadUserConfig(configFiles []*ConfigFile, base *UserConfig, isGuiInitialized bool) (*UserConfig, error) {
|
||||||
for _, configFile := range configFiles {
|
for _, configFile := range configFiles {
|
||||||
path := configFile.Path
|
path := configFile.Path
|
||||||
statInfo, err := os.Stat(path)
|
statInfo, err := os.Stat(path)
|
||||||
@@ -194,7 +194,7 @@ func loadUserConfig(configFiles []*ConfigFile, base *UserConfig) (*UserConfig, e
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err = migrateUserConfig(path, content)
|
content, err = migrateUserConfig(path, content, isGuiInitialized)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -219,7 +219,7 @@ func loadUserConfig(configFiles []*ConfigFile, base *UserConfig) (*UserConfig, e
|
|||||||
// config over time; examples are renaming a key to a better name, moving a key
|
// config over time; examples are renaming a key to a better name, moving a key
|
||||||
// from one container to another, or changing the type of a key (e.g. from bool
|
// from one container to another, or changing the type of a key (e.g. from bool
|
||||||
// to an enum).
|
// to an enum).
|
||||||
func migrateUserConfig(path string, content []byte) ([]byte, error) {
|
func migrateUserConfig(path string, content []byte, isGuiInitialized bool) ([]byte, error) {
|
||||||
changedContent, didChange, err := computeMigratedConfig(path, content)
|
changedContent, didChange, err := computeMigratedConfig(path, content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -231,11 +231,15 @@ func migrateUserConfig(path string, content []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write config back
|
// Write config back
|
||||||
fmt.Println("Provided user config is deprecated but auto-fixable. Attempting to write fixed version back to file...")
|
if !isGuiInitialized {
|
||||||
|
fmt.Println("Provided user config is deprecated but auto-fixable. Attempting to write fixed version back to file...")
|
||||||
|
}
|
||||||
if err := os.WriteFile(path, changedContent, 0o644); err != nil {
|
if err := os.WriteFile(path, changedContent, 0o644); err != nil {
|
||||||
return nil, fmt.Errorf("While attempting to write back fixed user config to %s, an error occurred: %s", path, err)
|
return nil, fmt.Errorf("While attempting to write back fixed user config to %s, an error occurred: %s", path, err)
|
||||||
}
|
}
|
||||||
fmt.Printf("Success. New config written to %s\n", path)
|
if !isGuiInitialized {
|
||||||
|
fmt.Printf("Success. New config written to %s\n", path)
|
||||||
|
}
|
||||||
return changedContent, nil
|
return changedContent, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -472,7 +476,7 @@ func (c *AppConfig) GetUserConfigDir() string {
|
|||||||
|
|
||||||
func (c *AppConfig) ReloadUserConfigForRepo(repoConfigFiles []*ConfigFile) error {
|
func (c *AppConfig) ReloadUserConfigForRepo(repoConfigFiles []*ConfigFile) error {
|
||||||
configFiles := append(c.globalUserConfigFiles, repoConfigFiles...)
|
configFiles := append(c.globalUserConfigFiles, repoConfigFiles...)
|
||||||
userConfig, err := loadUserConfigWithDefaults(configFiles)
|
userConfig, err := loadUserConfigWithDefaults(configFiles, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -497,7 +501,7 @@ func (c *AppConfig) ReloadChangedUserConfigFiles() (error, bool) {
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
userConfig, err := loadUserConfigWithDefaults(c.userConfigFiles)
|
userConfig, err := loadUserConfigWithDefaults(c.userConfigFiles, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err, false
|
return err, false
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user