-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
1password.go
69 lines (59 loc) · 1.49 KB
/
1password.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os/exec"
)
type opResponse struct {
UUID string `json:"uuid"`
Title string `json:"title"`
Category string `json:"category"`
Fields []opResponseField `json:"fields"`
}
type opResponseField struct {
Id string `json:"id"`
Value string `json:"value"`
Type string `json:"type"`
Label string `json:"label"`
}
var defaultOpGet = func(itemName string) (*opResponse, error) {
cmd := exec.Command("op", "item", "get", itemName, "--format=json", "--debug")
var outb, errb bytes.Buffer
cmd.Stdout = &outb
cmd.Stderr = &errb
err := cmd.Run()
if err != nil {
return nil, err
}
var resp opResponse
err = json.Unmarshal(outb.Bytes(), &resp)
if err != nil {
fmt.Println("Error unmarshaling data")
return nil, err
}
return &resp, nil
}
func opgetter(itemName string) (string, error) {
resp, err := defaultOpGet(itemName)
if err != nil {
return "", err
}
for _, v := range resp.Fields {
if v.Label == "credential" {
return v.Value, nil
}
}
return "", errors.New("unable to find credential")
}
func opsetter(itemName, secret string) error {
// create a string that will be passed to the op command to store our secret
credString := "credential[concealed]=" + secret
stdoutStderr, err := exec.Command("op", "item", "create", "--category",
"API Credential", "--title", itemName, credString).CombinedOutput()
if err != nil {
fmt.Printf("%s\n", stdoutStderr)
}
return err
}