Skip to content

Commit

Permalink
[feature] Allow setting autosync.interval in different time units (#2731
Browse files Browse the repository at this point in the history
)

Solves #2730

Signed-off-by: Daniel Lublin <[email protected]>
  • Loading branch information
quite authored Dec 1, 2023
1 parent 73ea5ca commit bf426e6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ This is a list of available options:
| `audit.concurrency` | `int` | Number of concurrent audit workers. | `` |
| `audit.hibp-dump-file` | `string` | Specify to a HIBPv2 Dump file (sorted) if you want `audit` to check password hashes against this file. | `None` |
| `audit.hibp-use-api` | `bool` | Set to true if you want `gopass audit` to check your secrets against the public HIBPv2 API. Use with caution. This will leak a few bit of entropy. | `false` |
| `autosync.interval` | `int` | AutoSync interval in days. | `3` |
| `autosync.interval` | `string` | AutoSync interval, for example `2d`, `4h`, `2m` (for days, hours, minutes). A plain number without suffix is taken as days. | `3` |
| `core.autoimport` | `bool` | Import missing keys stored in the pass repository without asking. | `false` |
| `core.autopush` | `bool` | Always do a `git push` after a commit to the store. Makes sure your local changes are always available on your git remote. | `true` |
| `core.autosync` | `bool` | Automatically sync (fetch & push) the git remote on an interval. | `true` |
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/stretchr/testify v1.8.4
github.com/twpayne/go-pinentry v0.3.0
github.com/urfave/cli/v2 v2.25.7
github.com/xhit/go-str2duration v1.2.0
github.com/zalando/go-keyring v0.2.3
github.com/zeebo/blake3 v0.2.3
golang.org/x/crypto v0.15.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xhit/go-str2duration v1.2.0 h1:BcV5u025cITWxEQKGWr1URRzrcXtu7uk8+luz3Yuhwc=
github.com/xhit/go-str2duration v1.2.0/go.mod h1:3cPSlfZlUHVlneIVfePFWcJZsuwf+P1v2SRTV4cUmp4=
github.com/xrash/smetrics v0.0.0-20170218160415-a3153f7040e9/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
Expand Down
23 changes: 16 additions & 7 deletions internal/action/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/urfave/cli/v2"
"github.com/xhit/go-str2duration"
)

var (
autosyncIntervalDays = 3
autosyncLastRun time.Time
autosyncInterval = time.Duration(3*24) * time.Hour
autosyncLastRun time.Time
)

func init() {
Expand All @@ -40,7 +41,7 @@ func init() {
return
}

autosyncIntervalDays = iv
autosyncInterval = time.Duration(iv*24) * time.Hour
}

// Sync all stores with their remotes.
Expand Down Expand Up @@ -69,13 +70,21 @@ func (s *Action) autoSync(ctx context.Context) error {

ls := s.rem.LastSeen("autosync")
debug.Log("autosync - last seen: %s", ls)
syncInterval := autosyncIntervalDays
syncInterval := autosyncInterval

if s.cfg.IsSet("autosync.interval") {
syncInterval = s.cfg.GetInt("autosync.interval")
if intervalStr := s.cfg.Get("autosync.interval"); intervalStr != "" {
if _, err := strconv.Atoi(intervalStr); err == nil {
intervalStr += "d"
}
if duration, err := str2duration.Str2Duration(intervalStr); err != nil {
out.Warningf(ctx, "failed to parse autosync.interval %q: %q", intervalStr, err)
} else {
syncInterval = duration
}
}
debug.Log("autosync - interval: %s", syncInterval)

if time.Since(ls) > time.Duration(syncInterval)*24*time.Hour {
if time.Since(ls) > syncInterval {
err := s.sync(ctx, "")
if err != nil {
autosyncLastRun = time.Now()
Expand Down

0 comments on commit bf426e6

Please sign in to comment.