-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathop.go
More file actions
127 lines (109 loc) · 2.71 KB
/
op.go
File metadata and controls
127 lines (109 loc) · 2.71 KB
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
package main
import (
"encoding/json"
"errors"
"fmt"
"os/exec"
)
type OpListItem struct {
Id string `json:"id"`
}
type OpItemDetails struct {
Title string `json:"title"`
Sections []OpSection `json:"sections"`
Fields []OpField `json:"fields"`
FieldLabelMap map[string]OpField
SectionMap map[string][]OpField
}
func (item OpItemDetails) getHostname() string {
hostname := item.FieldLabelMap["URL"].Value
if v, ok := item.FieldLabelMap["Hostname"]; ok {
hostname = v.Value
}
return hostname
}
func (item OpItemDetails) getHost() string {
host := item.Title
if v, ok := item.FieldLabelMap["Host"]; ok {
host = v.Value
}
return host
}
func (item OpItemDetails) getUser() string {
username := item.FieldLabelMap["username"].Value
if v, ok := item.FieldLabelMap["User"]; ok {
username = v.Value
}
return username
}
type OpSection struct {
Id string `json:"id"`
Label string `json:"label"`
}
type OpField struct {
Id string `json:"id"`
Type string `json:"type"`
Label string `json:"label"`
Value string `json:"value"`
Section *OpSection `json:"section"`
}
type Op struct {
Vault string
Tag string
}
func (op *Op) exec(args ...string) ([]byte, error) {
args = append(args, "--format=json")
if op.Vault != "" {
args = append(args, fmt.Sprintf("--vault=%s", op.Vault))
}
cmd := exec.Command("op", args...)
result, err := cmd.CombinedOutput()
if err != nil {
err = fmt.Errorf("%w; %s", err, string(result))
return nil, err
}
return result, err
}
func (op *Op) whoAmI() error {
cmd := exec.Command("op", "whoami")
result, err := cmd.CombinedOutput()
e := &exec.ExitError{}
if errors.As(err, &e) {
if e.ExitCode() != 0 {
return fmt.Errorf("1Password CLI not authenticated, please first authenticate your CLI instance and try again; %w", err)
}
return fmt.Errorf("%w; unknown error: %s", err, result)
}
return err
}
func (op *Op) listItems() ([]OpListItem, error) {
result, err := op.exec("item", "list", fmt.Sprintf("--tags=%s", op.Tag), "--categories=SERVER")
if err != nil {
return nil, err
}
list := []OpListItem{}
json.Unmarshal(result, &list)
return list, err
}
func (op *Op) getItem(id string) (OpItemDetails, error) {
result, err := op.exec("item", "get", id)
if err != nil {
return OpItemDetails{}, err
}
item := OpItemDetails{}
item.FieldLabelMap = make(map[string]OpField)
item.SectionMap = make(map[string][]OpField)
json.Unmarshal(result, &item)
for _, f := range item.Fields {
item.FieldLabelMap[f.Label] = f
if f.Section != nil {
arr, ok := item.SectionMap[f.Section.Label]
if !ok {
arr = []OpField{}
}
arr = append(arr, f)
item.SectionMap[f.Section.Label] = arr
}
}
return item, err
}