Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Todoist CLI #340

Merged
merged 3 commits into from
Aug 15, 2023
Merged
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
76 changes: 76 additions & 0 deletions plugins/todoist/api_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package todoist

import (
"context"
"encoding/json"

"github.com/1Password/shell-plugins/sdk"
"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 APIToken() schema.CredentialType {
return schema.CredentialType{
Name: credname.APIToken,
DocsURL: sdk.URL("https://todoist.com/help/articles/8048880904476"),
ManagementURL: sdk.URL("https://todoist.com/app/settings/integrations/developer"),
Fields: []schema.CredentialField{
{
Name: fieldname.Token,
MarkdownDescription: "API Token used to authenticate to Todoist.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 40,
Charset: schema.Charset{
Lowercase: true,
Digits: true,
},
},
},
},
DefaultProvisioner: provision.TempFile(
todoistConfig,
provision.AtFixedPath("~/.config/todoist/config.json"),
),
Importer: importer.TryAll(
TryTodoistConfigFile(),
)}
}

func TryTodoistConfigFile() sdk.Importer {
return importer.TryFile("~/.config/todoist/config.json", 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.Token == "" {
return
}

out.AddCandidate(sdk.ImportCandidate{
Fields: map[sdk.FieldName]string{
fieldname.Token: config.Token,
},
})
})
}

type Config struct {
Token string `json:"token"`
}

func todoistConfig(in sdk.ProvisionInput) ([]byte, error) {
config := Config{
Token: in.ItemFields[fieldname.Token],
}
contents, err := json.Marshal(&config)
if err != nil {
return nil, err
}
return []byte(contents), nil
}
45 changes: 45 additions & 0 deletions plugins/todoist/api_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package todoist

import (
"testing"

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

func TestAPITokenProvisioner(t *testing.T) {
plugintest.TestProvisioner(t, APIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{
"config file": {
ItemFields: map[sdk.FieldName]string{
fieldname.Token: "dbq9y65uguqrk4ognfhdiwcc0zx34z20pexample",
},
CommandLine: []string{"todoist"},
ExpectedOutput: sdk.ProvisionOutput{
CommandLine: []string{"todoist"},
Files: map[string]sdk.OutputFile{
"~/.config/todoist/config.json": {
Contents: []byte(plugintest.LoadFixture(t, "config.json")),
},
},
},
},
})
}

func TestAPITokenImporter(t *testing.T) {
plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{
"config file": {
Files: map[string]string{
"~/.config/todoist/config.json": plugintest.LoadFixture(t, "config.json"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "dbq9y65uguqrk4ognfhdiwcc0zx34z20pexample",
},
},
},
},
})
}
22 changes: 22 additions & 0 deletions plugins/todoist/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package todoist

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

func New() schema.Plugin {
return schema.Plugin{
Name: "todoist",
Platform: schema.PlatformInfo{
Name: "Todoist",
Homepage: sdk.URL("https://todoist.com"),
},
Credentials: []schema.CredentialType{
APIToken(),
},
Executables: []schema.Executable{
TodoistCLI(),
},
}
}
1 change: 1 addition & 0 deletions plugins/todoist/test-fixtures/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"token":"dbq9y65uguqrk4ognfhdiwcc0zx34z20pexample"}
24 changes: 24 additions & 0 deletions plugins/todoist/todoist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package todoist

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 TodoistCLI() schema.Executable {
return schema.Executable{
Name: "Todoist CLI",
Runs: []string{"todoist"},
DocsURL: sdk.URL("https://github.com/sachaos/todoist"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
),
Uses: []schema.CredentialUsage{
{
Name: credname.APIToken,
},
},
}
}