@@ -3,14 +3,13 @@ package cohere
33import (
44 "context"
55
6+ "encoding/json"
67 "github.com/1Password/shell-plugins/sdk"
78 "github.com/1Password/shell-plugins/sdk/importer"
89 "github.com/1Password/shell-plugins/sdk/provision"
910 "github.com/1Password/shell-plugins/sdk/schema"
1011 "github.com/1Password/shell-plugins/sdk/schema/credname"
1112 "github.com/1Password/shell-plugins/sdk/schema/fieldname"
12- "fmt"
13- "encoding/json"
1413)
1514
1615func Credentials () schema.CredentialType {
@@ -20,39 +19,31 @@ func Credentials() schema.CredentialType {
2019 ManagementURL : sdk .URL ("https://dashboard.cohere.ai/" ),
2120 Fields : []schema.CredentialField {
2221 {
23- Name : fieldname .Authtoken ,
22+ Name : fieldname .JWT ,
2423 MarkdownDescription : "JWT used to authenticate to Cohere stored in disk." ,
2524 Secret : true ,
2625 },
2726 {
2827 Name : fieldname .Email ,
2928 MarkdownDescription : "Email used to authenticate to Cohere." ,
30- Optional : true ,
29+ Optional : true ,
3130 },
3231 {
3332 Name : fieldname .URL ,
3433 MarkdownDescription : "URL of the operator server" ,
35- Optional : true ,
34+ Optional : true ,
3635 },
37-
3836 },
3937 DefaultProvisioner : provision .TempFile (
40- mysqlConfig ,
41- provision .Filename ("config" ),
42- provision .AtFixedPath ("~/.command/" )),
38+ cohereJSON ,
39+ provision .AtFixedPath ("~/.command/config" )),
4340 Importer : importer .TryAll (
4441 TryCohereConfigFile (),
4542 )}
4643}
4744
48-
49-
50- // TODO: Check if the platform stores the Credentials in a local config file, and if so,
51- // implement the function below to add support for importing it.
52-
53-
5445func TryCohereConfigFile () sdk.Importer {
55- return importer .TryFile ("~/command/config" , func (ctx context.Context , contents importer.FileContents , in sdk.ImportInput , out * sdk.ImportAttempt ) {
46+ return importer .TryFile ("~/. command/config" , func (ctx context.Context , contents importer.FileContents , in sdk.ImportInput , out * sdk.ImportAttempt ) {
5647 var config Config
5748 if err := contents .ToJSON (& config ); err != nil {
5849 out .AddError (err )
@@ -65,50 +56,49 @@ func TryCohereConfigFile() sdk.Importer {
6556
6657 out .AddCandidate (sdk.ImportCandidate {
6758 Fields : map [sdk.FieldName ]string {
68- fieldname .URL : config .CurrentURL ,
69- fieldname .Authtoken : config .Contexts [config .CurrentURL ].JWT ,
59+ fieldname .URL : config .CurrentURL ,
60+ fieldname .JWT : config .Contexts [config .CurrentURL ].JWT ,
7061 fieldname .Email : config .Contexts [config .CurrentURL ].Email ,
71-
7262 },
7363 })
7464 })
7565}
76- func mysqlConfig (in sdk.ProvisionInput ) ([]byte , error ) {
77- var currentURL , jwt , email string
78-
79-
80- if value , ok := in .ItemFields [fieldname .URL ]; ok {
81- currentURL = value
82- }
8366
84- if value , ok := in .ItemFields [fieldname .Authtoken ]; ok {
85- jwt = value
86- }
87-
88- if value , ok := in .ItemFields [fieldname .Email ]; ok {
89- email = value
90- }
91-
92- data := Config {
93- CurrentURL : currentURL ,
94- Contexts : map [string ]APISettings {
95- currentURL : {
96- JWT : jwt ,
97- Email : email ,
67+ func cohereJSON (in sdk.ProvisionInput ) ([]byte , error ) {
68+ var currentURL , jwt , email string
69+
70+ if value , ok := in .ItemFields [fieldname .JWT ]; ok {
71+ jwt = value
72+ } else {
73+ return nil , nil
74+ }
75+
76+ if value , ok := in .ItemFields [fieldname .URL ]; ok {
77+ currentURL = value
78+ }
79+
80+ if value , ok := in .ItemFields [fieldname .Email ]; ok {
81+ email = value
82+ }
83+
84+ data := Config {
85+ CurrentURL : currentURL ,
86+ Contexts : map [string ]APISettings {
87+ currentURL : {
88+ JWT : jwt ,
89+ Email : email ,
90+ },
9891 },
99- },
100-
101- }
92+ }
10293
103- jsonData , err := json .MarshalIndent (data , "" , " \t " )
104- if err != nil {
105- fmt . Println ( "Error:" , err )
106-
107- }
108- return []byte (jsonData ), nil
94+ jsonData , err := json .Marshal (data )
95+ if err != nil {
96+ return nil , err
97+
98+ }
99+ return []byte (jsonData ), nil
109100}
110101
111- // TODO: Implement the config file schema
112102type Config struct {
113103 CurrentURL string `json:"CurrentURL"`
114104 Contexts map [string ]APISettings `json:"Contexts"`
@@ -118,4 +108,3 @@ type APISettings struct {
118108 JWT string `json:"JWT"`
119109 Email string `json:"Email"`
120110}
121-
0 commit comments