Skip to content

Commit

Permalink
Add autosync once a week (gopasspw#2191)
Browse files Browse the repository at this point in the history
RELEASE_NOTES=[ENHANCEMENT] Automatically sync once a week

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
  • Loading branch information
dominikschulz authored Jul 18, 2022
1 parent 08d7361 commit 832d2cd
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Some configuration options are only available through setting environment variab
| `GOPASS_CLIPBOARD_CLEAR_CMD` | `string` | Use an external command to remove a password from the clipboard. See [GPaste](usecases/gpaste.md) for an example |
| `GOPASS_GPG_BINARY` | `string` | Set this to the absolute path to the GPG binary if you need to override the value returned by `gpgconf`, e.g. [QubesOS](https://www.qubes-os.org/doc/split-gpg/). |
| `GOPASS_PW_DEFAULT_LENGTH` | `int` | Set to any integer value larger than zero to define a different default length in the `generate` command. By default the length is 24 characters. |
| `GOPASS_AUTOSYNC_INTERVAL` | `int` | Set this to the number of days between autosync runs. |
| `GOPASS_NO_AUTOSYNC` | `bool` | Set this to `true` to disable autosync. |

Variables not exclusively used by gopass

Expand Down
1 change: 1 addition & 0 deletions internal/action/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (s *Action) IsInitialized(c *cli.Context) error {
if inited {
debug.Log("Store is fully initialized and ready to go\n\nAll systems go. 🚀\n")
s.printReminder(ctx)
_ = s.autoSync(ctx)

return nil
}
Expand Down
1 change: 1 addition & 0 deletions internal/action/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestProcess(t *testing.T) { //nolint:paralleltest

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithInteractive(ctx, false)

buf := &bytes.Buffer{}
out.Stdout = buf
Expand Down
43 changes: 43 additions & 0 deletions internal/action/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"errors"
"fmt"
"os"
"strconv"
"time"

"github.com/fatih/color"
"github.com/gopasspw/gopass/internal/backend"
Expand All @@ -18,11 +21,51 @@ import (
"github.com/urfave/cli/v2"
)

var autosyncIntervalDays = 3

func init() {
sv := os.Getenv("GOPASS_AUTOSYNC_INTERVAL")
if sv == "" {
return
}

iv, err := strconv.Atoi(sv)
if err != nil {
return
}

autosyncIntervalDays = iv
}

// Sync all stores with their remotes.
func (s *Action) Sync(c *cli.Context) error {
return s.sync(ctxutil.WithGlobalFlags(c), c.String("store"))
}

func (s *Action) autoSync(ctx context.Context) error {
if !ctxutil.IsInteractive(ctx) {
return nil
}

if !ctxutil.IsTerminal(ctx) {
return nil
}

if sv := os.Getenv("GOPASS_NO_AUTOSYNC"); sv != "" {
return nil
}

ls := s.rem.LastSeen("autosync")
debug.Log("autosync - last seen: %s", ls)
if time.Since(ls) > time.Duration(autosyncIntervalDays)*24*time.Hour {
_ = s.rem.Reset("autosync")

return s.sync(ctx, "")
}

return nil
}

func (s *Action) sync(ctx context.Context, store string) error {
out.Printf(ctx, "🚥 Syncing with all remotes ...")

Expand Down
7 changes: 4 additions & 3 deletions internal/reminder/reminder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func New() (*Store, error) {
}, nil
}

func (s *Store) lastSeen(key string) time.Time {
// LastSeen returns the time when the key was last reset.
func (s *Store) LastSeen(key string) time.Time {
t := time.Time{}
if s == nil {
return t
Expand Down Expand Up @@ -70,11 +71,11 @@ func (s *Store) Overdue(key string) bool {
return false
}

if time.Since(s.lastSeen("overdue")) < 24*time.Hour {
if time.Since(s.LastSeen("overdue")) < 24*time.Hour {
return false
}

_ = s.Reset("overdue")

return time.Since(s.lastSeen(key)) > 90*24*time.Hour
return time.Since(s.LastSeen(key)) > 90*24*time.Hour
}

0 comments on commit 832d2cd

Please sign in to comment.