Skip to content

Commit ee2c3fc

Browse files
committed
Recommited with sign
1 parent f3bee75 commit ee2c3fc

File tree

5 files changed

+65
-80
lines changed

5 files changed

+65
-80
lines changed

plugins/cohere/co.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import (
99

1010
func CohereCLI() schema.Executable {
1111
return schema.Executable{
12-
Name: "Cohere CLI",
13-
Runs: []string{"co"},
14-
DocsURL: sdk.URL("https://docs.cohere.com/reference/command"),
12+
Name: "Cohere CLI",
13+
Runs: []string{"co"},
14+
DocsURL: sdk.URL("https://docs.cohere.com/reference/command"),
1515
NeedsAuth: needsauth.IfAll(
1616
needsauth.NotForHelpOrVersion(),
1717
needsauth.NotWithoutArgs(),
18+
needsauth.NotWhenContainsArgs("config"),
19+
needsauth.NotWhenContainsArgs("ping"),
1820
),
1921
Uses: []schema.CredentialUsage{
2022
{

plugins/cohere/credentials.go

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package cohere
33
import (
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

1615
func 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-
5445
func 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
112102
type 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-

plugins/cohere/credentials_test.go

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@ package cohere
22

33
import (
44
"testing"
5-
5+
66
"github.com/1Password/shell-plugins/sdk"
77
"github.com/1Password/shell-plugins/sdk/plugintest"
88
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
99
)
10-
10+
1111
func TestCredentialsProvisioner(t *testing.T) {
1212
plugintest.TestProvisioner(t, Credentials().DefaultProvisioner, map[string]plugintest.ProvisionCase{
13-
"default": {
14-
ItemFields: map[sdk.FieldName]string{ // TODO: Check if this is correct
15-
fieldname.: "G5d4wd0U1OAcox6zrYcOZ96EXAMPLE",
13+
"temp file": {
14+
ItemFields: map[sdk.FieldName]string{
15+
fieldname.JWT: "PLEzI1N--EXAMPLE",
16+
fieldname.URL: "https://api.os.cohere.ai",
17+
fieldname.Email: "[email protected]",
1618
},
1719
ExpectedOutput: sdk.ProvisionOutput{
18-
Environment: map[string]string{
19-
"COHERE": "G5d4wd0U1OAcox6zrYcOZ96EXAMPLE",
20+
Files: map[string]sdk.OutputFile{
21+
"~/.command/config": {
22+
Contents: []byte(plugintest.LoadFixture(t, "config")),
23+
},
2024
},
2125
},
2226
},
@@ -25,31 +29,20 @@ func TestCredentialsProvisioner(t *testing.T) {
2529

2630
func TestCredentialsImporter(t *testing.T) {
2731
plugintest.TestImporter(t, Credentials().Importer, map[string]plugintest.ImportCase{
28-
"environment": {
29-
Environment: map[string]string{ // TODO: Check if this is correct
30-
"COHERE": "G5d4wd0U1OAcox6zrYcOZ96EXAMPLE",
32+
33+
"config file": {
34+
Files: map[string]string{
35+
"~/.command/config": plugintest.LoadFixture(t, "config"),
3136
},
3237
ExpectedCandidates: []sdk.ImportCandidate{
3338
{
3439
Fields: map[sdk.FieldName]string{
35-
fieldname.: "G5d4wd0U1OAcox6zrYcOZ96EXAMPLE",
40+
fieldname.JWT: "PLEzI1N--EXAMPLE",
41+
fieldname.URL: "https://api.os.cohere.ai",
42+
fieldname.Email: "[email protected]",
3643
},
3744
},
3845
},
3946
},
40-
// TODO: If you implemented a config file importer, add a test file example in cohere/test-fixtures
41-
// and fill the necessary details in the test template below.
42-
"config file": {
43-
Files: map[string]string{
44-
// "~/path/to/config.yml": plugintest.LoadFixture(t, "config.yml"),
45-
},
46-
ExpectedCandidates: []sdk.ImportCandidate{
47-
// {
48-
// Fields: map[sdk.FieldName]string{
49-
// fieldname.Token: "G5d4wd0U1OAcox6zrYcOZ96EXAMPLE",
50-
// },
51-
// },
52-
},
53-
},
5447
})
5548
}

plugins/cohere/plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func New() schema.Plugin {
1010
Name: "cohere",
1111
Platform: schema.PlatformInfo{
1212
Name: "Cohere",
13-
Homepage: sdk.URL("https://cohere.com"), // TODO: Check if this is correct
13+
Homepage: sdk.URL("https://cohere.com"),
1414
},
1515
Credentials: []schema.CredentialType{
1616
Credentials(),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"CurrentURL":"https://api.os.cohere.ai","Contexts":{"https://api.os.cohere.ai":{"JWT":"PLEzI1N--EXAMPLE","Email":"[email protected]"}}}

0 commit comments

Comments
 (0)