Skip to content

Commit 43459c2

Browse files
committed
Add support for Todoist CLI
1 parent 1e4048c commit 43459c2

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed

plugins/todoist/api_token.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package todoist
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
7+
"github.com/1Password/shell-plugins/sdk"
8+
"github.com/1Password/shell-plugins/sdk/importer"
9+
"github.com/1Password/shell-plugins/sdk/provision"
10+
"github.com/1Password/shell-plugins/sdk/schema"
11+
"github.com/1Password/shell-plugins/sdk/schema/credname"
12+
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
13+
)
14+
15+
func APIToken() schema.CredentialType {
16+
return schema.CredentialType{
17+
Name: credname.APIToken,
18+
DocsURL: sdk.URL("https://todoist.com/help/articles/8048880904476"),
19+
ManagementURL: sdk.URL("https://todoist.com/app/settings/integrations/developer"),
20+
Fields: []schema.CredentialField{
21+
{
22+
Name: fieldname.Token,
23+
MarkdownDescription: "API Token used to authenticate to Todoist.",
24+
Secret: true,
25+
Composition: &schema.ValueComposition{
26+
Length: 40,
27+
Charset: schema.Charset{
28+
Lowercase: true,
29+
Digits: true,
30+
},
31+
},
32+
},
33+
},
34+
DefaultProvisioner: provision.TempFile(
35+
todoistConfig,
36+
provision.AtFixedPath("~/.config/todoist/config.json"),
37+
),
38+
Importer: importer.TryAll(
39+
TryTodoistConfigFile(),
40+
)}
41+
}
42+
43+
func TryTodoistConfigFile() sdk.Importer {
44+
return importer.TryFile("~/.config/todoist/config.json", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
45+
var config Config
46+
if err := contents.ToJSON(&config); err != nil {
47+
out.AddError(err)
48+
return
49+
}
50+
51+
if config.Token == "" {
52+
return
53+
}
54+
55+
out.AddCandidate(sdk.ImportCandidate{
56+
Fields: map[sdk.FieldName]string{
57+
fieldname.Token: config.Token,
58+
},
59+
})
60+
})
61+
}
62+
63+
type Config struct {
64+
Token string `json:"token"`
65+
}
66+
67+
func todoistConfig(in sdk.ProvisionInput) ([]byte, error) {
68+
config := Config{
69+
Token: in.ItemFields[fieldname.Token],
70+
}
71+
contents, err := json.MarshalIndent(&config, "", " ")
72+
if err != nil {
73+
return nil, err
74+
}
75+
return []byte(contents), nil
76+
}

plugins/todoist/api_token_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package todoist
2+
3+
import (
4+
"testing"
5+
6+
"github.com/1Password/shell-plugins/sdk"
7+
"github.com/1Password/shell-plugins/sdk/plugintest"
8+
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
9+
)
10+
11+
func TestAPITokenProvisioner(t *testing.T) {
12+
plugintest.TestProvisioner(t, APIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{
13+
"config file": {
14+
ItemFields: map[sdk.FieldName]string{
15+
fieldname.Token: "dbq9y65uguqrk4ognfhdiwcc0zx34z20pexample",
16+
},
17+
CommandLine: []string{"todoist"},
18+
ExpectedOutput: sdk.ProvisionOutput{
19+
CommandLine: []string{"todoist"},
20+
Files: map[string]sdk.OutputFile{
21+
"~/.config/todoist/config.json": {
22+
Contents: []byte(plugintest.LoadFixture(t, "config.json")),
23+
},
24+
},
25+
},
26+
},
27+
})
28+
}
29+
30+
func TestAPITokenImporter(t *testing.T) {
31+
plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{
32+
"config file": {
33+
Files: map[string]string{
34+
"~/.config/todoist/config.json": plugintest.LoadFixture(t, "config.json"),
35+
},
36+
ExpectedCandidates: []sdk.ImportCandidate{
37+
{
38+
Fields: map[sdk.FieldName]string{
39+
fieldname.Token: "dbq9y65uguqrk4ognfhdiwcc0zx34z20pexample",
40+
},
41+
},
42+
},
43+
},
44+
})
45+
}

plugins/todoist/plugin.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package todoist
2+
3+
import (
4+
"github.com/1Password/shell-plugins/sdk"
5+
"github.com/1Password/shell-plugins/sdk/schema"
6+
)
7+
8+
func New() schema.Plugin {
9+
return schema.Plugin{
10+
Name: "todoist",
11+
Platform: schema.PlatformInfo{
12+
Name: "Todoist",
13+
Homepage: sdk.URL("https://todoist.com"),
14+
},
15+
Credentials: []schema.CredentialType{
16+
APIToken(),
17+
},
18+
Executables: []schema.Executable{
19+
TodoistCLI(),
20+
},
21+
}
22+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"token": "dbq9y65uguqrk4ognfhdiwcc0zx34z20pexample"
3+
}

plugins/todoist/todoist.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package todoist
2+
3+
import (
4+
"github.com/1Password/shell-plugins/sdk"
5+
"github.com/1Password/shell-plugins/sdk/needsauth"
6+
"github.com/1Password/shell-plugins/sdk/schema"
7+
"github.com/1Password/shell-plugins/sdk/schema/credname"
8+
)
9+
10+
func TodoistCLI() schema.Executable {
11+
return schema.Executable{
12+
Name: "Todoist CLI",
13+
Runs: []string{"todoist"},
14+
DocsURL: sdk.URL("https://github.com/sachaos/todoist"),
15+
NeedsAuth: needsauth.IfAll(
16+
needsauth.NotForHelpOrVersion(),
17+
needsauth.NotWithoutArgs(),
18+
),
19+
Uses: []schema.CredentialUsage{
20+
{
21+
Name: credname.APIToken,
22+
},
23+
},
24+
}
25+
}

0 commit comments

Comments
 (0)