forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonapi.go
109 lines (93 loc) · 3.04 KB
/
jsonapi.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
package action
import (
"context"
"os"
"runtime"
"strings"
"github.com/fatih/color"
"github.com/justwatchcom/gopass/utils/jsonapi"
"github.com/justwatchcom/gopass/utils/jsonapi/manifest"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
// JSONAPI reads a json message on stdin and responds on stdout
func (s *Action) JSONAPI(ctx context.Context, c *cli.Context) error {
api := jsonapi.API{Store: s.Store, CliContext: c, Reader: os.Stdin, Writer: os.Stdout}
if err := api.ReadAndRespond(ctx); err != nil {
return api.RespondError(err)
}
return nil
}
// SetupNativeMessaging sets up manifest for gopass as native messaging host
func (s *Action) SetupNativeMessaging(ctx context.Context, c *cli.Context) error {
browser, err := s.getBrowser(ctx, c)
if err != nil {
return err
}
globalInstall, err := s.getGlobalInstall(ctx, c)
if err != nil {
return err
}
libpath, err := s.getLibPath(ctx, c, browser, globalInstall)
if err != nil {
return err
}
wrapperPath, err := s.getWrapperPath(ctx, c)
if err != nil {
return err
}
if err := manifest.PrintSummary(browser, wrapperPath, libpath, globalInstall); err != nil {
return err
}
if c.Bool("print-only") {
return nil
}
install, err := s.askForBool(ctx, color.BlueString("Install manifest and wrapper?"), true)
if install && err == nil {
return manifest.SetUp(browser, wrapperPath, libpath, globalInstall)
}
return err
}
func (s *Action) getBrowser(ctx context.Context, c *cli.Context) (browser string, err error) {
browser = c.String("browser")
if browser == "" {
browser, err = s.askForString(ctx, color.BlueString("For which browser do you want to install gopass native messaging? [%s]", strings.Join(manifest.ValidBrowsers[:], ",")), manifest.DefaultBrowser)
if err != nil {
return "", errors.Wrapf(err, "failed to ask for user input")
}
if !stringInSlice(browser, manifest.ValidBrowsers) {
return "", errors.Errorf("%s not one of %s", browser, strings.Join(manifest.ValidBrowsers[:], ","))
}
}
return
}
func (s *Action) getGlobalInstall(ctx context.Context, c *cli.Context) (bool, error) {
if !c.IsSet("global") {
return s.askForBool(ctx, color.BlueString("Install for all users? (might require sudo gopass)"), false)
}
return c.Bool("global"), nil
}
func (s *Action) getLibPath(ctx context.Context, c *cli.Context, browser string, global bool) (string, error) {
if !c.IsSet("libpath") && runtime.GOOS == "linux" && browser == "firefox" && global {
return s.askForString(ctx, color.BlueString("What is your lib path?"), "/usr/lib")
}
return c.String("libpath"), nil
}
func (s *Action) getWrapperPath(ctx context.Context, c *cli.Context) (path string, err error) {
path = c.String("path")
if path == "" {
path, err = s.askForString(ctx, color.BlueString("In which path should gopass_wrapper.sh be installed?"), manifest.DefaultWrapperPath)
if err != nil {
return "", errors.Wrapf(err, "failed to ask for user input")
}
}
return
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}