forked from gopasspw/gopass
/
version.go
101 lines (83 loc) · 2.49 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/blang/semver"
"github.com/gopasspw/gopass/pkg/out"
"github.com/gopasspw/gopass/pkg/protect"
"github.com/gopasspw/gopass/pkg/updater"
"github.com/fatih/color"
"github.com/urfave/cli"
)
// Version prints the gopass version
func (s *Action) Version(ctx context.Context, c *cli.Context) error {
version := make(chan string, 1)
go s.checkVersion(ctx, version)
_ = s.Initialized(ctx, c)
cli.VersionPrinter(c)
cryptoVer := versionInfo(ctx, s.Store.Crypto(ctx, ""))
rcsVer := versionInfo(ctx, s.Store.RCS(ctx, ""))
storageVer := versionInfo(ctx, s.Store.Storage(ctx, ""))
tpl := "%-10s - %10s - %10s - %10s\n"
fmt.Fprintf(stdout, tpl, "<root>", cryptoVer, rcsVer, storageVer)
// report all used crypto, sync and fs backends
for _, mp := range s.Store.MountPoints() {
cv := versionInfo(ctx, s.Store.Crypto(ctx, mp))
rv := versionInfo(ctx, s.Store.RCS(ctx, mp))
sv := versionInfo(ctx, s.Store.Storage(ctx, mp))
if cv != cryptoVer || rv != rcsVer || sv != storageVer {
fmt.Fprintf(stdout, tpl, mp, cv, rv, sv)
}
}
select {
case vi := <-version:
if vi != "" {
fmt.Fprintln(stdout, vi)
}
case <-time.After(2 * time.Second):
out.Error(ctx, "Version check timed out")
case <-ctx.Done():
return ExitError(ctx, ExitAborted, nil, "user aborted")
}
return nil
}
type versioner interface {
Name() string
Version(context.Context) semver.Version
}
func versionInfo(ctx context.Context, v versioner) string {
if v == nil {
return "<none>"
}
return fmt.Sprintf("%s %s", v.Name(), v.Version(ctx))
}
func (s *Action) checkVersion(ctx context.Context, u chan string) {
if disabled := os.Getenv("CHECKPOINT_DISABLE"); disabled != "" {
u <- ""
return
}
if strings.HasSuffix(s.version.String(), "+HEAD") || protect.ProtectEnabled {
// chan not check version against HEAD or
// against pledge(2)'d OpenBSD
u <- ""
return
}
r, err := updater.LatestRelease(ctx, len(s.version.Pre) > 0)
if err != nil {
u <- color.RedString("\nError checking latest version: %s", err)
return
}
if s.version.LT(r.Version()) {
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 www.justwatch.com/gopass"
if err := updater.IsUpdateable(ctx); err == nil {
notice += " by running 'gopass update'"
}
notice += " or via your package manager"
u <- color.YellowString(notice)
}
u <- ""
}