Skip to content

Commit

Permalink
Do not fail if reminder can not be initialized (gopasspw#1835)
Browse files Browse the repository at this point in the history
Fixes gopasspw#1832

RELEASE_NOTES=[BUGFIX] Do not fail if reminder is unavailable

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
  • Loading branch information
dominikschulz authored Mar 12, 2021
1 parent 6a94849 commit 7cf2f2d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
8 changes: 6 additions & 2 deletions internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions internal/cache/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion internal/reminder/reminder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package reminder

import (
"fmt"
"time"

"github.com/gopasspw/gopass/internal/cache"
Expand All @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7cf2f2d

Please sign in to comment.