From ffda51014d9619712dd8313d3e712c7489bbd641 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 3 May 2025 20:44:07 +0200 Subject: [PATCH] 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. --- pkg/config/app_config.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 180457fdf..ae1b0d38e 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -96,7 +96,7 @@ func NewAppConfig( configFiles = []*ConfigFile{configFile} } - userConfig, err := loadUserConfigWithDefaults(configFiles) + userConfig, err := loadUserConfigWithDefaults(configFiles, false) if err != nil { return nil, err } @@ -145,11 +145,11 @@ func findOrCreateConfigDir() (string, error) { return folder, os.MkdirAll(folder, 0o755) } -func loadUserConfigWithDefaults(configFiles []*ConfigFile) (*UserConfig, error) { - return loadUserConfig(configFiles, GetDefaultConfig()) +func loadUserConfigWithDefaults(configFiles []*ConfigFile, isGuiInitialized bool) (*UserConfig, error) { + 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 { path := configFile.Path statInfo, err := os.Stat(path) @@ -194,7 +194,7 @@ func loadUserConfig(configFiles []*ConfigFile, base *UserConfig) (*UserConfig, e return nil, err } - content, err = migrateUserConfig(path, content) + content, err = migrateUserConfig(path, content, isGuiInitialized) if err != nil { 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 // from one container to another, or changing the type of a key (e.g. from bool // 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) if err != nil { return nil, err @@ -231,11 +231,15 @@ func migrateUserConfig(path string, content []byte) ([]byte, error) { } // 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 { 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 } @@ -472,7 +476,7 @@ func (c *AppConfig) GetUserConfigDir() string { func (c *AppConfig) ReloadUserConfigForRepo(repoConfigFiles []*ConfigFile) error { configFiles := append(c.globalUserConfigFiles, repoConfigFiles...) - userConfig, err := loadUserConfigWithDefaults(configFiles) + userConfig, err := loadUserConfigWithDefaults(configFiles, true) if err != nil { return err } @@ -497,7 +501,7 @@ func (c *AppConfig) ReloadChangedUserConfigFiles() (error, bool) { return nil, false } - userConfig, err := loadUserConfigWithDefaults(c.userConfigFiles) + userConfig, err := loadUserConfigWithDefaults(c.userConfigFiles, true) if err != nil { return err, false }