-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathapi_token.go
More file actions
84 lines (73 loc) · 2.06 KB
/
api_token.go
File metadata and controls
84 lines (73 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package ohdear
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://ohdear.app/docs/api/introduction#get-your-api-token"),
ManagementURL: sdk.URL("https://ohdear.app/user/api-tokens"),
Fields: []schema.CredentialField{
{
Name: fieldname.Token,
MarkdownDescription: "Token used to authenticate to Oh Dear.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 40,
Charset: schema.Charset{
Uppercase: true,
Lowercase: true,
Digits: true,
},
},
},
},
DefaultProvisioner: provision.TempFile(configFile,
provision.Filename("config.json"),
provision.SetPathAsEnvVar("OHDEAR_CONFIG_FILE_PATH"),
),
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
TryOhdearConfigFile(),
)}
}
var defaultEnvVarMapping = map[string]sdk.FieldName{
"OHDEAR_API_TOKEN": fieldname.Token,
}
func configFile(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
}
func TryOhdearConfigFile() sdk.Importer {
return importer.TryFile("~/.ohdear/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"`
}