-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
version.go
101 lines (80 loc) · 2.53 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package action
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/fatih/color"
"github.com/gopasspw/gopass/internal/action/exit"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/internal/updater"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/gopasspw/gopass/pkg/protect"
"github.com/urfave/cli/v2"
)
// Version prints the gopass version.
func (s *Action) Version(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
version := make(chan string, 1)
go s.checkVersion(ctx, version)
cli.VersionPrinter(c)
select {
case vi := <-version:
if vi != "" {
fmt.Fprintln(stdout, vi)
}
case <-time.After(2 * time.Second):
out.Errorf(ctx, "Version check timed out")
case <-ctx.Done():
return exit.Error(exit.Aborted, nil, "user aborted")
}
return nil
}
func (s *Action) checkVersion(ctx context.Context, u chan string) {
msg := ""
defer func() {
u <- msg
}()
if disabled := os.Getenv("CHECKPOINT_DISABLE"); disabled != "" {
debug.Log("remote version check disabled by CHECKPOINT_DISABLE")
return
}
// NB: "updater.check" isn't supported as a local config option, hence no mount point here
if cfg, _ := config.FromContext(ctx); cfg.IsSet("updater.check") && !cfg.GetBool("updater.check") {
debug.Log("remote version check disabled by updater.check = false")
return
}
// force checking for updates, mainly for testing.
force := os.Getenv("GOPASS_FORCE_CHECK") != ""
if !force && strings.HasSuffix(s.version.String(), "+HEAD") {
// chan not check version against HEAD.
debug.Log("remote version check disabled for dev version")
return
}
if !force && protect.ProtectEnabled {
// chan not check version
// against pledge(2)'d OpenBSD.
debug.Log("remote version check disabled for pledge(2)'d version")
return
}
r, err := updater.FetchLatestRelease(ctx)
if err != nil {
msg = color.RedString("\nError checking latest version: %s", err)
return
}
if s.version.GTE(r.Version) {
_ = s.rem.Reset("update")
debug.Log("gopass is up-to-date (local: %q, GitHub: %q)", s.version, r.Version)
return
}
notice := fmt.Sprintf("\nYour version (%s) of gopass is out of date!\nThe latest version is %s.\n", s.version, r.Version.String())
notice += "You can update by downloading from https://www.gopass.pw/#install"
if err := updater.IsUpdateable(ctx); err == nil {
notice += " by running 'gopass update'"
}
notice += " or via your package manager"
msg = color.YellowString(notice)
}