-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgop.go
75 lines (64 loc) · 1.89 KB
/
gop.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
// Package main assumes a 1password json object for the op cli tool's
// get command is fed in via stdin
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
)
// variabled can be set with -ldflags "-X main.SectionName=<whatever>"
// SectionName is the 1password section containing AWS credentials
var SectionName = "Credentials"
// AccessField is the 1password field with the AWS access key
var AccessField = "access"
// SecretField is the 1password field with the AWS secret key
var SecretField = "secret"
// RegionField is the 1password field with the AWS default region
var RegionField = "region"
// OnePassItem sets up the minimal structure needed to get AWS credentials
type OnePassItem struct {
Details struct {
Sections []struct {
Title string `json:"title"`
Fields []struct {
T string `json:"t"`
V string `json:"v"`
} `json:"fields"`
} `json:"sections"`
} `json:"details"`
}
// parse1PasswordEntry takes json from the given reader and
// returns 'source'able export statements
func parse1PasswordEntry(input io.Reader) []string {
dec := json.NewDecoder(input)
var item OnePassItem
err := dec.Decode(&item)
if err != nil {
log.Fatal(err)
}
output := make([]string, 0)
for _, section := range item.Details.Sections {
if section.Title == SectionName {
for _, field := range section.Fields {
switch field.T {
case AccessField:
output = append(output, fmt.Sprintf("export AWS_ACCESS_KEY_ID='%v'", field.V))
case SecretField:
output = append(output, fmt.Sprintf("export AWS_SECRET_ACCESS_KEY='%v'", field.V))
case RegionField:
output = append(output, fmt.Sprintf("export AWS_DEFAULT_REGION='%v'", field.V))
}
}
}
}
return output
}
// main sends stdin to parse1PasswordEntry and formats the returned statements to stdout
func main() {
lines := parse1PasswordEntry(os.Stdin)
for _, line := range lines {
fmt.Println(line)
}
}