diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 0a1a9d26f..35ed33699 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -721,11 +721,6 @@ func (c *AppConfig) GetUserConfig() *UserConfig { return c.userConfig } -// GetAppState returns the app state -func (c *AppConfig) GetAppState() *AppState { - return c.appState -} - func (c *AppConfig) GetCachedGithubPullRequests(repoPath string) ([]CachedPullRequest, error) { if c.githubPullRequestCache == nil { return nil, nil @@ -826,59 +821,6 @@ func stateFilePath(filename string) (string, error) { return xdg.StateFile(filepath.Join("lazygit", filename)) } -// SaveAppState marshalls the AppState struct and writes it to the disk -func (c *AppConfig) SaveAppState() error { - marshalledAppState, err := yaml.Marshal(c.appState) - if err != nil { - return err - } - - filepath, err := stateFilePath(stateFileName) - if err != nil { - return err - } - - err = os.WriteFile(filepath, marshalledAppState, 0o644) - if err != nil && os.IsPermission(err) { - // apparently when people have read-only permissions they prefer us to fail silently - return nil - } - - return err -} - -var stateFileName = "state.yml" - -// loadAppState loads recorded AppState from file -func loadAppState() (*AppState, error) { - appState := getDefaultAppState() - - filepath, err := stateFilePath(stateFileName) - if err != nil { - if os.IsPermission(err) { - // apparently when people have read-only permissions they prefer us to fail silently - return appState, nil - } - return nil, err - } - - appStateBytes, err := os.ReadFile(filepath) - if err != nil && !os.IsNotExist(err) { - return nil, err - } - - if len(appStateBytes) == 0 { - return appState, nil - } - - err = yaml.Unmarshal(appStateBytes, appState) - if err != nil { - return nil, err - } - - return appState, nil -} - // SaveGlobalUserConfig saves the UserConfig back to disk. This is only used in // integration tests, so we are a bit sloppy with error handling. func (c *AppConfig) SaveGlobalUserConfig() { @@ -897,25 +839,6 @@ func (c *AppConfig) SaveGlobalUserConfig() { } } -// 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 - LastVersion string // this is the last version the user was using, for the purpose of showing release notes - - // these are for shell commands typed in directly, not for custom commands in the lazygit config. - // For backwards compatibility we keep the old name in yaml files. - ShellCommandsHistory []string `yaml:"customcommandshistory"` - - HideCommandLog bool -} - -func getDefaultAppState() *AppState { - return &AppState{} -} - func LogPath() (string, error) { if os.Getenv("LAZYGIT_LOG_PATH") != "" { return os.Getenv("LAZYGIT_LOG_PATH"), nil diff --git a/pkg/config/app_state.go b/pkg/config/app_state.go new file mode 100644 index 000000000..231d28d01 --- /dev/null +++ b/pkg/config/app_state.go @@ -0,0 +1,84 @@ +package config + +import ( + "os" + + "gopkg.in/yaml.v3" +) + +// GetAppState returns the app state +func (c *AppConfig) GetAppState() *AppState { + return c.appState +} + +// SaveAppState marshalls the AppState struct and writes it to the disk +func (c *AppConfig) SaveAppState() error { + marshalledAppState, err := yaml.Marshal(c.appState) + if err != nil { + return err + } + + filepath, err := stateFilePath(stateFileName) + if err != nil { + return err + } + + err = os.WriteFile(filepath, marshalledAppState, 0o644) + if err != nil && os.IsPermission(err) { + // apparently when people have read-only permissions they prefer us to fail silently + return nil + } + + return err +} + +var stateFileName = "state.yml" + +// loadAppState loads recorded AppState from file +func loadAppState() (*AppState, error) { + appState := getDefaultAppState() + + filepath, err := stateFilePath(stateFileName) + if err != nil { + if os.IsPermission(err) { + // apparently when people have read-only permissions they prefer us to fail silently + return appState, nil + } + return nil, err + } + + appStateBytes, err := os.ReadFile(filepath) + if err != nil && !os.IsNotExist(err) { + return nil, err + } + + if len(appStateBytes) == 0 { + return appState, nil + } + + err = yaml.Unmarshal(appStateBytes, appState) + if err != nil { + return nil, err + } + + return appState, nil +} + +// 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 + LastVersion string // this is the last version the user was using, for the purpose of showing release notes + + // these are for shell commands typed in directly, not for custom commands in the lazygit config. + // For backwards compatibility we keep the old name in yaml files. + ShellCommandsHistory []string `yaml:"customcommandshistory"` + + HideCommandLog bool +} + +func getDefaultAppState() *AppState { + return &AppState{} +}