-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from rajapri28613/todoist
Add support for Todoist CLI
- Loading branch information
Showing
5 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"token":"dbq9y65uguqrk4ognfhdiwcc0zx34z20pexample"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
} | ||
} |