@@ -14,15 +14,54 @@ import (
1414 "github.com/mitchellh/go-homedir"
1515)
1616
17- func CreateVault (name string ) error {
17+ type OPClient interface {
18+ IsV2 () bool
19+ CreateVault (name string ) error
20+ CreateItem (vault string , template * ItemTemplate , title string ) error
21+ SetField (vault , item , field , value string ) error
22+ GetFields (vault , item string ) (map [string ]string , error )
23+ ExistsVault (vaultName string ) (bool , error )
24+ ExistsItemInVault (vault string , itemName string ) (bool , error )
25+ }
26+
27+ func GetOPClient () (OPClient , error ) {
28+ out , err := execOP ("--version" )
29+ if err != nil {
30+ return nil , err
31+ }
32+
33+ version := strings .TrimSpace (string (out ))
34+
35+ if strings .HasPrefix (version , "2." ) {
36+ return & OPV2Client {
37+ version : version ,
38+ isV2 : true ,
39+ }, nil
40+ }
41+ return & OPV1Client {
42+ version : version ,
43+ isV2 : false ,
44+ }, nil
45+ }
46+
47+ type OPV1Client struct {
48+ version string
49+ isV2 bool
50+ }
51+
52+ func (op * OPV1Client ) IsV2 () bool {
53+ return op .isV2
54+ }
55+
56+ func (op * OPV1Client ) CreateVault (name string ) error {
1857 _ , err := execOP ("create" , "vault" , name )
1958 if err != nil {
2059 return fmt .Errorf ("could not create vault '%s': %s" , name , err )
2160 }
2261 return nil
2362}
2463
25- func CreateItem (vault string , template * ItemTemplate , title string ) error {
64+ func ( op * OPV1Client ) CreateItem (vault string , template * ItemTemplate , title string ) error {
2665 jsonTemplate , err := json .Marshal (template )
2766 if err != nil {
2867 return err
@@ -34,7 +73,7 @@ func CreateItem(vault string, template *ItemTemplate, title string) error {
3473 return err
3574}
3675
37- func SetField (vault , item , field , value string ) error {
76+ func ( op * OPV1Client ) SetField (vault , item , field , value string ) error {
3877 _ , err := execOP ("edit" , "item" , item , fmt .Sprintf (`%s=%s` , field , value ), "--vault=" + vault )
3978 if err != nil {
4079 return fmt .Errorf ("could not set field '%s'.'%s'.'%s'" , vault , item , field )
@@ -45,7 +84,7 @@ func SetField(vault, item, field, value string) error {
4584// GetFields returns a title-to-value map of the fields from the first section of the given 1Password item.
4685// The rest of the fields are ignored as the migration tool only stores information in the first
4786// section of each item.
48- func GetFields (vault , item string ) (map [string ]string , error ) {
87+ func ( op * OPV1Client ) GetFields (vault , item string ) (map [string ]string , error ) {
4988 opItem := struct {
5089 Details ItemTemplate `json:"details"`
5190 }{}
@@ -108,7 +147,7 @@ type itemFieldTemplate struct {
108147}
109148
110149func execOP (args ... string ) ([]byte , error ) {
111- command := exec .Command ("op " , args ... )
150+ command := exec .Command ("op1 " , args ... )
112151 command .Stderr = os .Stderr
113152 var out bytes.Buffer
114153 command .Stdout = & out
@@ -203,7 +242,7 @@ func GetSignInAddress() (string, error) {
203242 return "" , fmt .Errorf ("unexpected format of 1password config file at %s: missing account entry for latest used account" , path )
204243}
205244
206- func ExistsVault (vaultName string ) (bool , error ) {
245+ func ( op * OPV1Client ) ExistsVault (vaultName string ) (bool , error ) {
207246 vaultsBytes , err := execOP ("list" , "vaults" )
208247 if err != nil {
209248 return false , fmt .Errorf ("could not list vaults: %s" , err )
@@ -228,7 +267,7 @@ func ExistsVault(vaultName string) (bool, error) {
228267 return false , nil
229268}
230269
231- func ExistsItemInVault (vault string , itemName string ) (bool , error ) {
270+ func ( op * OPV1Client ) ExistsItemInVault (vault string , itemName string ) (bool , error ) {
232271 itemsBytes , err := execOP ("list" , "items" , "--vault" , vault )
233272 if err != nil {
234273 return false , fmt .Errorf ("could not list items in vault %s: %s" , vault , err )
0 commit comments