forked from gopasspw/gopass-jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (108 loc) · 2.69 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"strings"
"github.com/gopasspw/gopass/pkg/gopass/api"
"github.com/urfave/cli/v2"
)
const (
name = "gopass-jsonapi"
)
var (
// Version is the released version of gopass
version string
)
func main() {
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():
}
}()
gp, err := api.New(ctx)
if err != nil {
fmt.Printf("Failed to initialize gopass API: %s\n", err)
os.Exit(1)
}
ja := &jsonapiCLI{
gp: gp,
}
app := cli.NewApp()
app.Name = name
app.Version = version
app.Usage = "Setup and run gopass-jsonapi as native messaging hosts, e.g. for browser plugins"
app.EnableBashCompletion = true
app.Action = func(c *cli.Context) error {
if strings.HasSuffix(os.Args[0], "native_host") || strings.HasSuffix(os.Args[0], "native_host.exe") {
return ja.listen(c)
}
return cli.ShowAppHelp(c)
}
app.Commands = []*cli.Command{
{
Name: "listen",
Usage: "Listen and respond to messages via stdin/stdout",
Description: "Gopass-jsonapi is started in listen mode from browser plugins using a wrapper specified in native messaging host manifests",
Hidden: true,
Action: func(c *cli.Context) error {
return ja.listen(c)
},
},
{
Name: "configure",
Usage: "Setup gopass-jsonapi native messaging manifest for selected browser",
Description: "To access gopass from browser plugins, a native app manifest must be installed at the correct location",
Action: func(c *cli.Context) error {
return ja.setup(c)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "browser",
Usage: "One of 'chrome' and 'firefox'",
},
&cli.StringFlag{
Name: "path",
Usage: "Path to install 'gopass_wrapper.sh' to",
},
&cli.StringFlag{
Name: "manifest-path",
Usage: "Path to install 'com.justwatch.gopass.json' to",
},
&cli.BoolFlag{
Name: "global",
Usage: "Install for all users, requires superuser rights",
},
&cli.StringFlag{
Name: "libpath",
Usage: "Library path for global installation on linux. Default is /usr/lib",
},
&cli.StringFlag{
Name: "gopass-path",
Usage: "Path to gopass binary. Default is auto detected",
},
&cli.BoolFlag{
Name: "print",
Usage: "Print installation summary before creating any files",
Value: true,
},
},
},
}
if err := app.RunContext(ctx, os.Args); err != nil {
log.Fatal(err)
}
}