From 425cd4bd9b9b1006a85ea3708828df96f90dfa70 Mon Sep 17 00:00:00 2001 From: Antoine Grondin Date: Sat, 14 Jan 2023 20:22:45 +0900 Subject: [PATCH] record path of statefile when default one is created initially --- cmd/humanlog/main.go | 2 +- internal/pkg/state/state.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/humanlog/main.go b/cmd/humanlog/main.go index 39e43cf7..6f1c05fa 100644 --- a/cmd/humanlog/main.go +++ b/cmd/humanlog/main.go @@ -227,7 +227,7 @@ func newApp() *cli.App { if !ok { return nil } - if res.hasUpdate && promptedToUpdate == nil && promptedToUpdate.LT(res.sem) { + if res.hasUpdate && promptedToUpdate == nil || promptedToUpdate.LT(res.sem) { alreadyPromptedForSameUpdate := promptedToUpdate != nil && promptedToUpdate.GTE(res.sem) if !alreadyPromptedForSameUpdate { promptToUpdate(semverVersion, res.sem) diff --git a/internal/pkg/state/state.go b/internal/pkg/state/state.go index 2988aa5e..8393c031 100644 --- a/internal/pkg/state/state.go +++ b/internal/pkg/state/state.go @@ -51,7 +51,8 @@ func ReadStateFile(path string, dflt *State) (*State, error) { if !errors.Is(err, os.ErrNotExist) { return nil, fmt.Errorf("opening state file %q: %v", path, err) } - return dflt, WriteStateFile(path, dflt) + cfg := (State{path: path}).populateEmpty(dflt) + return cfg, WriteStateFile(path, cfg) } defer stateFile.Close() var cfg State @@ -67,7 +68,6 @@ func WriteStateFile(path string, state *State) error { if err != nil { return fmt.Errorf("marshaling state file: %v", err) } - newf, err := os.CreateTemp(os.TempDir(), "humanlog_statefile") if err != nil { return fmt.Errorf("creating temporary file for statefile: %w", err) @@ -88,7 +88,7 @@ func WriteStateFile(path string, state *State) error { return fmt.Errorf("setting permissions on temporary statefile: %w", err) } if err := os.Rename(newf.Name(), path); err != nil { - return fmt.Errorf("replacing statefile: %w", err) + return fmt.Errorf("replacing statefile at %q with %q: %w", path, newf.Name(), err) } success = true return nil