@@ -2,7 +2,6 @@ package onepassword
22
33import (
44 "bytes"
5- "encoding/base64"
65 "encoding/json"
76 "fmt"
87 "io/ioutil"
@@ -14,55 +13,30 @@ import (
1413 "github.com/mitchellh/go-homedir"
1514)
1615
17- func CreateVault (name string ) error {
18- _ , err := execOP ("create" , "vault" , name )
19- if err != nil {
20- return fmt .Errorf ("could not create vault '%s': %s" , name , err )
21- }
22- return nil
16+ type OPCLI interface {
17+ IsV2 () bool
18+ CreateVault (name string ) error
19+ CreateItem (vault string , template * ItemTemplate , title string ) error
20+ SetField (vault , item , field , value string ) error
21+ GetFields (vault , item string ) (map [string ]string , error )
22+ ExistsVault (vaultName string ) (bool , error )
23+ ExistsItemInVault (vault string , itemName string ) (bool , error )
2324}
2425
25- func CreateItem ( vault string , template * ItemTemplate , title string ) error {
26- jsonTemplate , err := json . Marshal ( template )
26+ func GetOPClient () ( OPCLI , error ) {
27+ out , err := execOP ( "--version" )
2728 if err != nil {
28- return err
29+ return nil , err
2930 }
3031
31- encodedTemplate := base64 . RawURLEncoding . EncodeToString ( jsonTemplate )
32+ version := strings . TrimSpace ( string ( out ) )
3233
33- _ , err = execOP ("create" , "item" , "apicredential" , "--vault=" + vault , encodedTemplate , "title=" + title )
34- return err
35- }
36-
37- func SetField (vault , item , field , value string ) error {
38- _ , err := execOP ("edit" , "item" , item , fmt .Sprintf (`%s=%s` , field , value ), "--vault=" + vault )
39- if err != nil {
40- return fmt .Errorf ("could not set field '%s'.'%s'.'%s'" , vault , item , field )
34+ if strings .HasPrefix (version , "2." ) {
35+ return & OPV2CLI {}, nil
36+ } else if strings .HasPrefix (version , "1." ) {
37+ return & OPV1CLI {}, nil
4138 }
42- return nil
43- }
44-
45- // GetFields returns a title-to-value map of the fields from the first section of the given 1Password item.
46- // The rest of the fields are ignored as the migration tool only stores information in the first
47- // section of each item.
48- func GetFields (vault , item string ) (map [string ]string , error ) {
49- opItem := struct {
50- Details ItemTemplate `json:"details"`
51- }{}
52- opItemJSON , err := execOP ("get" , "item" , item , "--vault=" + vault )
53- if err != nil {
54- return nil , fmt .Errorf ("could not get item '%s'.'%s' from 1Password: %s" , vault , item , err )
55- }
56- err = json .Unmarshal (opItemJSON , & opItem )
57- if err != nil {
58- return nil , fmt .Errorf ("unexpected format of 1Password item in `op get item` command output: %s" , err )
59- }
60-
61- fields := make (map [string ]string , len (opItem .Details .Sections [0 ].Fields ))
62- for _ , field := range opItem .Details .Sections [0 ].Fields {
63- fields [field .Title ] = field .Value
64- }
65- return fields , nil
39+ return nil , fmt .Errorf ("1password: op version not recognized" )
6640}
6741
6842func NewItemTemplate () * ItemTemplate {
@@ -202,54 +176,3 @@ func GetSignInAddress() (string, error) {
202176
203177 return "" , fmt .Errorf ("unexpected format of 1password config file at %s: missing account entry for latest used account" , path )
204178}
205-
206- func ExistsVault (vaultName string ) (bool , error ) {
207- vaultsBytes , err := execOP ("list" , "vaults" )
208- if err != nil {
209- return false , fmt .Errorf ("could not list vaults: %s" , err )
210- }
211-
212- vaultsJSON := make ([]struct {
213- UUID string `json:"uuid"`
214- Name string `json:"name"`
215- }, 0 )
216-
217- err = json .Unmarshal (vaultsBytes , & vaultsJSON )
218- if err != nil {
219- return false , fmt .Errorf ("unexpected format of `op list vaults`: %s" , vaultsBytes )
220- }
221-
222- for _ , vault := range vaultsJSON {
223- if vault .Name == vaultName {
224- return true , nil
225- }
226- }
227-
228- return false , nil
229- }
230-
231- func ExistsItemInVault (vault string , itemName string ) (bool , error ) {
232- itemsBytes , err := execOP ("list" , "items" , "--vault" , vault )
233- if err != nil {
234- return false , fmt .Errorf ("could not list items in vault %s: %s" , vault , err )
235- }
236-
237- itemsJSON := make ([]struct {
238- Overview struct {
239- Title string `json:"title"`
240- } `json:"overview"`
241- }, 0 )
242-
243- err = json .Unmarshal (itemsBytes , & itemsJSON )
244- if err != nil {
245- return false , fmt .Errorf ("unexpected format of `op list items`: %s" , itemsBytes )
246- }
247-
248- for _ , item := range itemsJSON {
249- if item .Overview .Title == itemName {
250- return true , nil
251- }
252- }
253-
254- return false , nil
255- }
0 commit comments