Skip to content

Commit 3d2520a

Browse files
committed
[feature] Allow setting autosync.interval in different time units
Solves #2730 Signed-off-by: Daniel Lublin <[email protected]>
1 parent ac50558 commit 3d2520a

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

docs/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ This is a list of available options:
8484
| `audit.concurrency` | `int` | Number of concurrent audit workers. | `` |
8585
| `audit.hibp-dump-file` | `string` | Specify to a HIBPv2 Dump file (sorted) if you want `audit` to check password hashes against this file. | `None` |
8686
| `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` |
87-
| `autosync.interval` | `int` | AutoSync interval in days. | `3` |
87+
| `autosync.interval` | `string` | AutoSync interval, for example `2d`, `4h`, `2m` (for days, hours, minutes). A plain number without suffix is taken as days. | `3` |
8888
| `core.autoimport` | `bool` | Import missing keys stored in the pass repository without asking. | `false` |
8989
| `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` |
9090
| `core.autosync` | `bool` | Automatically sync (fetch & push) the git remote on an interval. | `true` |

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ require (
3737
github.com/stretchr/testify v1.8.4
3838
github.com/twpayne/go-pinentry v0.3.0
3939
github.com/urfave/cli/v2 v2.25.7
40+
github.com/xhit/go-str2duration v1.2.0
4041
github.com/zalando/go-keyring v0.2.3
4142
github.com/zeebo/blake3 v0.2.3
4243
golang.org/x/crypto v0.15.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
192192
github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
193193
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
194194
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
195+
github.com/xhit/go-str2duration v1.2.0 h1:BcV5u025cITWxEQKGWr1URRzrcXtu7uk8+luz3Yuhwc=
196+
github.com/xhit/go-str2duration v1.2.0/go.mod h1:3cPSlfZlUHVlneIVfePFWcJZsuwf+P1v2SRTV4cUmp4=
195197
github.com/xrash/smetrics v0.0.0-20170218160415-a3153f7040e9/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
196198
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
197199
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=

internal/action/sync.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import (
2020
"github.com/gopasspw/gopass/pkg/ctxutil"
2121
"github.com/gopasspw/gopass/pkg/debug"
2222
"github.com/urfave/cli/v2"
23+
"github.com/xhit/go-str2duration"
2324
)
2425

2526
var (
26-
autosyncIntervalDays = 3
27-
autosyncLastRun time.Time
27+
autosyncInterval = time.Duration(3*24) * time.Hour
28+
autosyncLastRun time.Time
2829
)
2930

3031
func init() {
@@ -40,7 +41,7 @@ func init() {
4041
return
4142
}
4243

43-
autosyncIntervalDays = iv
44+
autosyncInterval = time.Duration(iv*24) * time.Hour
4445
}
4546

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

7071
ls := s.rem.LastSeen("autosync")
7172
debug.Log("autosync - last seen: %s", ls)
72-
syncInterval := autosyncIntervalDays
73+
syncInterval := autosyncInterval
7374

74-
if s.cfg.IsSet("autosync.interval") {
75-
syncInterval = s.cfg.GetInt("autosync.interval")
75+
if intervalStr := s.cfg.Get("autosync.interval"); intervalStr != "" {
76+
if _, err := strconv.Atoi(intervalStr); err == nil {
77+
intervalStr += "d"
78+
}
79+
if duration, err := str2duration.Str2Duration(intervalStr); err != nil {
80+
out.Warningf(ctx, "failed to parse autosync.interval %q: %q", intervalStr, err)
81+
} else {
82+
syncInterval = duration
83+
}
7684
}
85+
debug.Log("autosync - interval: %s", syncInterval)
7786

78-
if time.Since(ls) > time.Duration(syncInterval)*24*time.Hour {
87+
if time.Since(ls) > syncInterval {
7988
err := s.sync(ctx, "")
8089
if err != nil {
8190
autosyncLastRun = time.Now()

0 commit comments

Comments
 (0)