forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (119 loc) · 2.52 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"os/signal"
"runtime"
"time"
_ "github.com/gopasspw/gopass/pkg/backend/crypto"
_ "github.com/gopasspw/gopass/pkg/backend/rcs"
_ "github.com/gopasspw/gopass/pkg/backend/storage"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/protect"
"github.com/blang/semver"
"github.com/fatih/color"
colorable "github.com/mattn/go-colorable"
"github.com/urfave/cli"
)
const (
name = "gopass"
)
var (
// Version is the released version of gopass
version string
// BuildTime is the time the binary was built
date string
// Commit is the git hash the binary was built from
commit string
)
func main() {
if err := protect.Pledge("stdio rpath wpath cpath tty proc exec"); err != nil {
panic(err)
}
ctx := context.Background()
// trap Ctrl+C and call cancel on the context
ctx, cancel := context.WithCancel(ctx)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
defer func() {
signal.Stop(sigChan)
cancel()
}()
go func() {
select {
case <-sigChan:
cancel()
case <-ctx.Done():
}
}()
cli.ErrWriter = errorWriter{
out: colorable.NewColorableStderr(),
}
sv := getVersion()
cli.VersionPrinter = makeVersionPrinter(os.Stdout, sv)
app := setupApp(ctx, sv)
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func makeVersionPrinter(out io.Writer, sv semver.Version) func(c *cli.Context) {
return func(c *cli.Context) {
buildtime := ""
if bt, err := time.Parse("2006-01-02T15:04:05-0700", date); err == nil {
buildtime = bt.Format("2006-01-02 15:04:05")
}
buildInfo := ""
if commit != "" {
buildInfo = commit
}
if buildtime != "" {
if buildInfo != "" {
buildInfo += " "
}
buildInfo += buildtime
}
if buildInfo != "" {
buildInfo = "(" + buildInfo + ") "
}
fmt.Fprintf(
out,
"%s %s %s%s %s %s\n",
name,
sv.String(),
buildInfo,
runtime.Version(),
runtime.GOOS,
runtime.GOARCH,
)
}
}
type errorWriter struct {
out io.Writer
}
func (e errorWriter) Write(p []byte) (int, error) {
return e.out.Write([]byte("\n" + color.RedString("Error: %s", p)))
}
func withGlobalFlags(ctx context.Context, c *cli.Context) context.Context {
if c.GlobalBool("yes") {
ctx = ctxutil.WithAlwaysYes(ctx, true)
}
return ctx
}
func getVersion() semver.Version {
sv, err := semver.Parse(version)
if err == nil {
return sv
}
return semver.Version{
Major: 1,
Minor: 8,
Patch: 6,
Pre: []semver.PRVersion{
{VersionStr: "git"},
},
Build: []string{"HEAD"},
}
}