This repository has been archived by the owner on Oct 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
74 lines (64 loc) · 1.65 KB
/
cli.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
package op
import (
"encoding/json"
"errors"
"fmt"
"os/exec"
"strings"
)
// CLI represents the 1Password CLI.
type CLI struct {
// Account specifies the account to execute the commands by account shorthand, sign-in address, account ID, or user ID.
Account string
// Config specifies the 1Password CLI configuration directory to use.
Config string
// Path specifies an absolute path to the 1Password CLI executable.
// If not set, exec.LookPath() will be utilized to attempt to find the `op` executable on $PATH.
Path string
}
// Version returns 1Password CLI version.
func (c CLI) Version() (string, error) {
b, err := c.execRaw([]string{"--version"}, nil)
return strings.TrimSpace(string(b)), err
}
func (c CLI) execRaw(cmd []string, args []string) ([]byte, error) {
if c.Account != "" {
cmd = append(cmd, fmt.Sprintf("--account=%s", c.Account))
}
if c.Config != "" {
cmd = append(cmd, fmt.Sprintf("--config=%s", c.Config))
}
cmd = append(cmd, args...)
path := c.Path
if path == "" {
p, err := exec.LookPath("op")
if err != nil && !errors.Is(err, exec.ErrDot) {
return nil, err
}
path = p
}
op := &exec.Cmd{
Path: path,
Args: append([]string{path}, cmd...),
}
b, err := op.Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
return nil, fmt.Errorf("%s: %s", ee, ee.Stderr)
}
return nil, err
}
return b, err
}
func (c CLI) execJSON(cmd []string, args []string, v any) error {
cmd = append(cmd, "--format", "json", "--iso-timestamps")
b, err := c.execRaw(cmd, args)
if err != nil {
return err
}
err = json.Unmarshal(b, &v)
return err
}
func sanitize(s string) string {
return fmt.Sprintf("%q", s)
}