|
| 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.Marshal(&config) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + return []byte(contents), nil |
| 76 | +} |
0 commit comments