-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
111 lines (93 loc) 路 2.35 KB
/
commands.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
110
111
package main
import (
"fmt"
"github.com/tidwall/gjson"
"github.com/tlowerison/credential-1password/op"
"github.com/tlowerison/credential-1password/util"
)
var ConfigKeys = []string{
util.VaultKey,
}
// Get retrieves a credential from 1Password
func Get(ctx *util.Context) error {
query, err := ctx.GetOpQuery()
if err != nil {
return err
}
// err should only occur if document
// does not exist, don't want to log
document, err := op.GetDocument(ctx.OpFunc, *query)
if err == nil {
fmt.Println(document)
}
return nil
}
func Op(ctx *util.Context, args []string) error {
sessionToken, err := ctx.GetSessionToken()
if err != nil {
return err
}
output, err := op.Op("", append([]string{"--session", sessionToken}, args...))
if err == nil {
fmt.Println(output)
}
return err
}
// Store upserts a credential in 1Password
func Store(ctx *util.Context) error {
query, err := ctx.GetOpQuery()
if err != nil {
return err
}
// ignore error because missing documents are returned as errors
item, _ := op.GetItem(ctx.OpFunc, *query)
uuid := gjson.Get(item, "uuid").String()
mode := string(ctx.GetMode())
title := query.Key
query.Key = uuid
input := op.DocumentUpsert{
Query: *query,
Content: ctx.GetInput(),
FileName: fmt.Sprintf("%s-credentials", mode),
Title: title,
}
if uuid == "" {
_, err = op.CreateDocument(ctx.OpFunc, input)
} else {
_, err = op.EditDocument(ctx.OpFunc, input)
}
return err
}
// Erase removes a credential in 1Password
func Erase(ctx *util.Context) error {
query, err := ctx.GetOpQuery()
if err != nil {
return err
}
return op.DeleteDocument(ctx.OpFunc, *query)
}
func Config(ctx *util.Context, args []string) error {
key := args[0]
switch key {
case util.VaultKey:
return ConfigVault(ctx, args[1:])
default:
return fmt.Errorf("unknown config option %s", args[0])
}
}
// ConfigVault gets/sets which 1Password vault credentials should be stored in.
func ConfigVault(ctx *util.Context, args []string) error {
vaultName, err := ctx.GetVaultName()
if err != nil {
return err
}
if len(args) == 0 {
if ctx.Flags.Config_Vault_Create {
return ctx.SetVaultName(vaultName, true)
} else {
fmt.Println(vaultName)
return nil
}
}
return ctx.SetVaultName(args[0], ctx.Flags.Config_Vault_Create)
}