diff --git a/internal/action/action.go b/internal/action/action.go index 7c32472ccf..009e0f9007 100644 --- a/internal/action/action.go +++ b/internal/action/action.go @@ -8,6 +8,7 @@ import ( "github.com/gopasspw/gopass/internal/config" "github.com/gopasspw/gopass/internal/reminder" "github.com/gopasspw/gopass/internal/store/root" + "github.com/gopasspw/gopass/pkg/debug" "github.com/blang/semver/v4" ) @@ -47,9 +48,12 @@ func newAction(cfg *config.Config, sv semver.Version, remind bool) (*Action, err if remind { r, err := reminder.New() if err != nil { - return nil, err + debug.Log("failed to init reminder: %s", err) + } else { + // only populate the reminder variable on success, the implementation + // can handle being called on a nil pointer + act.rem = r } - act.rem = r } return act, nil diff --git a/internal/cache/disk.go b/internal/cache/disk.go index 2132fc13e3..ad1272cb57 100644 --- a/internal/cache/disk.go +++ b/internal/cache/disk.go @@ -23,9 +23,11 @@ type OnDisk struct { func NewOnDisk(name string, ttl time.Duration) (*OnDisk, error) { d := filepath.Join(appdir.UserCache(), "gopass", name) if err := os.MkdirAll(d, 0755); err != nil { - return nil, err + return nil, fmt.Errorf("failed to create ondisk cache dir %s: %w", d, err) } + debug.Log("New on disk cache %s created at %s", name, d) + return &OnDisk{ ttl: ttl, name: name, @@ -39,20 +41,27 @@ func (o *OnDisk) Get(key string) ([]string, error) { fn := filepath.Join(o.dir, key) fi, err := os.Stat(fn) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to stat %s: %w", fn, err) } + if time.Now().After(fi.ModTime().Add(o.ttl)) { return nil, fmt.Errorf("expired") } + buf, err := os.ReadFile(fn) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to read file %s: %w", fn, err) } + return strings.Split(string(buf), "\n"), nil } // Set adds an entry to the cache. func (o *OnDisk) Set(key string, value []string) error { key = fsutil.CleanFilename(key) - return os.WriteFile(filepath.Join(o.dir, key), []byte(strings.Join(value, "\n")), 0644) + fn := filepath.Join(o.dir, key) + if err := os.WriteFile(fn, []byte(strings.Join(value, "\n")), 0644); err != nil { + return fmt.Errorf("failed to write %s to %s: %w", key, fn, err) + } + return nil } diff --git a/internal/reminder/reminder.go b/internal/reminder/reminder.go index d761e472f9..90ccbe4f6d 100644 --- a/internal/reminder/reminder.go +++ b/internal/reminder/reminder.go @@ -1,6 +1,7 @@ package reminder import ( + "fmt" "time" "github.com/gopasspw/gopass/internal/cache" @@ -16,7 +17,7 @@ type Store struct { func New() (*Store, error) { od, err := cache.NewOnDisk("reminder", 90*24*time.Hour) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to init reminder cache: %w", err) } return &Store{ diff --git a/main.go b/main.go index 34b502b82b..3c3f7b715c 100644 --- a/main.go +++ b/main.go @@ -115,8 +115,8 @@ func setupApp(ctx context.Context, sv semver.Version) (context.Context, *cli.App // initialize action handlers action, err := ap.New(cfg, sv) if err != nil { - out.Errorf(ctx, "No gpg binary found: %s", err) - os.Exit(ap.ExitGPG) + out.Errorf(ctx, "failed to initialize gopass: %s", err) + os.Exit(ap.ExitUnknown) } // set some action callbacks