From bf426e64a1b5e69078b0bf1d97947b663df434fe Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Fri, 1 Dec 2023 12:04:49 +0100 Subject: [PATCH] [feature] Allow setting autosync.interval in different time units (#2731) Solves https://github.com/gopasspw/gopass/issues/2730 Signed-off-by: Daniel Lublin --- docs/config.md | 2 +- go.mod | 1 + go.sum | 2 ++ internal/action/sync.go | 23 ++++++++++++++++------- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/docs/config.md b/docs/config.md index 092332a625..c583ecc0b8 100644 --- a/docs/config.md +++ b/docs/config.md @@ -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` | diff --git a/go.mod b/go.mod index 5706d7ebb6..40887caa3a 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 1c3d339391..183daa3177 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/action/sync.go b/internal/action/sync.go index 2757a973e0..e50e78691b 100644 --- a/internal/action/sync.go +++ b/internal/action/sync.go @@ -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() { @@ -40,7 +41,7 @@ func init() { return } - autosyncIntervalDays = iv + autosyncInterval = time.Duration(iv*24) * time.Hour } // Sync all stores with their remotes. @@ -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()