Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions plugins/cohere/co.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cohere

Check failure on line 1 in plugins/cohere/co.go

View workflow job for this annotation

GitHub Actions / Lint

: # github.com/1Password/shell-plugins/plugins/cohere [github.com/1Password/shell-plugins/plugins/cohere.test]

import (
"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/needsauth"
"github.com/1Password/shell-plugins/sdk/schema"
"github.com/1Password/shell-plugins/sdk/schema/credname"
)

func CohereCLI() schema.Executable {
return schema.Executable{
Name: "Cohere CLI",
Runs: []string{"co"},
DocsURL: sdk.URL("https://docs.cohere.com/reference/command"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
Comment on lines +16 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there may be other commands, such as co auth login, that do not require authentication - could you have a look at what all of those may be?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not yet resolved

needsauth.NotWhenContainsArgs("config"),
needsauth.NotWhenContainsArgs("ping"),
),
Uses: []schema.CredentialUsage{
{
Name: credname.Credentials,
},
},
}
}
110 changes: 110 additions & 0 deletions plugins/cohere/credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package cohere

import (
"context"
"encoding/json"

"github.com/1Password/shell-plugins/sdk"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's sort imports on this file to make the linter pass.

"github.com/1Password/shell-plugins/sdk/importer"
"github.com/1Password/shell-plugins/sdk/provision"
"github.com/1Password/shell-plugins/sdk/schema"
"github.com/1Password/shell-plugins/sdk/schema/credname"
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
)

func Credentials() schema.CredentialType {
return schema.CredentialType{
Name: credname.Credentials,
DocsURL: sdk.URL("https://docs.cohere.com/reference/config"),
ManagementURL: sdk.URL("https://dashboard.cohere.ai/"),
Fields: []schema.CredentialField{
{
Name: fieldname.JWT,

Check failure on line 22 in plugins/cohere/credentials.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: fieldname.JWT

Check failure on line 22 in plugins/cohere/credentials.go

View workflow job for this annotation

GitHub Actions / Test

undefined: fieldname.JWT
MarkdownDescription: "JWT used to authenticate to Cohere stored in disk.",
Secret: true,
},
{
Name: fieldname.Email,
MarkdownDescription: "Email used to authenticate to Cohere.",
Optional: true,
},
{
Name: fieldname.URL,
MarkdownDescription: "URL of the operator server",
Optional: true,
},
},
DefaultProvisioner: provision.TempFile(
cohereJSON,
provision.AtFixedPath("~/.command/config")),
Importer: importer.TryAll(
TryCohereConfigFile(),
)}
}

func TryCohereConfigFile() sdk.Importer {
return importer.TryFile("~/.command/config", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
var config Config
if err := contents.ToJSON(&config); err != nil {
out.AddError(err)
return
}

if config.Contexts[config.CurrentURL].JWT == "" {
return
}

out.AddCandidate(sdk.ImportCandidate{
Fields: map[sdk.FieldName]string{
fieldname.URL: config.CurrentURL,
fieldname.JWT: config.Contexts[config.CurrentURL].JWT,

Check failure on line 60 in plugins/cohere/credentials.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: fieldname.JWT

Check failure on line 60 in plugins/cohere/credentials.go

View workflow job for this annotation

GitHub Actions / Test

undefined: fieldname.JWT
fieldname.Email: config.Contexts[config.CurrentURL].Email,
},
})
})
}

func cohereJSON(in sdk.ProvisionInput) ([]byte, error) {
var currentURL, jwt, email string

if value, ok := in.ItemFields[fieldname.JWT]; ok {

Check failure on line 70 in plugins/cohere/credentials.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: fieldname.JWT

Check failure on line 70 in plugins/cohere/credentials.go

View workflow job for this annotation

GitHub Actions / Test

undefined: fieldname.JWT
jwt = value
} else {
return nil, nil
}

if value, ok := in.ItemFields[fieldname.URL]; ok {
currentURL = value
}

if value, ok := in.ItemFields[fieldname.Email]; ok {
email = value
}

data := Config{
CurrentURL: currentURL,
Contexts: map[string]APISettings{
currentURL: {
JWT: jwt,
Email: email,
},
},
}

jsonData, err := json.Marshal(data)
if err != nil {
return nil, err

}
return []byte(jsonData), nil
}

type Config struct {
CurrentURL string `json:"CurrentURL"`
Contexts map[string]APISettings `json:"Contexts"`
}

type APISettings struct {
JWT string `json:"JWT"`
Email string `json:"Email"`
}
48 changes: 48 additions & 0 deletions plugins/cohere/credentials_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cohere

import (
"testing"

"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/plugintest"
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
)

func TestCredentialsProvisioner(t *testing.T) {
plugintest.TestProvisioner(t, Credentials().DefaultProvisioner, map[string]plugintest.ProvisionCase{
"temp file": {
ItemFields: map[sdk.FieldName]string{
fieldname.JWT: "PLEzI1N--EXAMPLE",

Check failure on line 15 in plugins/cohere/credentials_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: fieldname.JWT

Check failure on line 15 in plugins/cohere/credentials_test.go

View workflow job for this annotation

GitHub Actions / Test

undefined: fieldname.JWT
fieldname.URL: "https://api.os.cohere.ai",
fieldname.Email: "[email protected]",
},
ExpectedOutput: sdk.ProvisionOutput{
Files: map[string]sdk.OutputFile{
"~/.command/config": {
Contents: []byte(plugintest.LoadFixture(t, "config")),
},
},
},
},
})
}

func TestCredentialsImporter(t *testing.T) {
plugintest.TestImporter(t, Credentials().Importer, map[string]plugintest.ImportCase{

"config file": {
Files: map[string]string{
"~/.command/config": plugintest.LoadFixture(t, "config"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.JWT: "PLEzI1N--EXAMPLE",

Check failure on line 40 in plugins/cohere/credentials_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: fieldname.JWT (typecheck)

Check failure on line 40 in plugins/cohere/credentials_test.go

View workflow job for this annotation

GitHub Actions / Test

undefined: fieldname.JWT
fieldname.URL: "https://api.os.cohere.ai",
fieldname.Email: "[email protected]",
},
},
},
},
})
}
22 changes: 22 additions & 0 deletions plugins/cohere/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cohere

import (
"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/schema"
)

func New() schema.Plugin {
return schema.Plugin{
Name: "cohere",
Platform: schema.PlatformInfo{
Name: "Cohere",
Homepage: sdk.URL("https://cohere.com"),
},
Credentials: []schema.CredentialType{
Credentials(),
},
Executables: []schema.Executable{
CohereCLI(),
},
}
}
1 change: 1 addition & 0 deletions plugins/cohere/test-fixtures/config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"CurrentURL":"https://api.os.cohere.ai","Contexts":{"https://api.os.cohere.ai":{"JWT":"PLEzI1N--EXAMPLE","Email":"[email protected]"}}}
Loading