|
| 1 | +package flyctl |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/1Password/shell-plugins/sdk" |
| 7 | + "github.com/1Password/shell-plugins/sdk/importer" |
| 8 | + "github.com/1Password/shell-plugins/sdk/provision" |
| 9 | + "github.com/1Password/shell-plugins/sdk/schema" |
| 10 | + "github.com/1Password/shell-plugins/sdk/schema/credname" |
| 11 | + "github.com/1Password/shell-plugins/sdk/schema/fieldname" |
| 12 | +) |
| 13 | + |
| 14 | +func AccessToken() schema.CredentialType { |
| 15 | + return schema.CredentialType{ |
| 16 | + Name: credname.AccessToken, |
| 17 | + DocsURL: sdk.URL("https://fly.io/docs/flyctl/auth-login/"), |
| 18 | + ManagementURL: sdk.URL("https://fly.io/user/personal_access_tokens"), |
| 19 | + Fields: []schema.CredentialField{ |
| 20 | + { |
| 21 | + Name: fieldname.Token, |
| 22 | + MarkdownDescription: "Token used to authenticate to Fly.io.", |
| 23 | + Secret: true, |
| 24 | + Composition: &schema.ValueComposition{ |
| 25 | + Length: 43, |
| 26 | + Charset: schema.Charset{ |
| 27 | + Uppercase: true, |
| 28 | + Lowercase: true, |
| 29 | + Digits: true, |
| 30 | + Symbols: true, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), |
| 36 | + Importer: importer.TryAll( |
| 37 | + importer.TryEnvVarPair(defaultEnvVarMapping), |
| 38 | + TryFlyctlConfigFile(), |
| 39 | + )} |
| 40 | +} |
| 41 | + |
| 42 | +var defaultEnvVarMapping = map[string]sdk.FieldName{ |
| 43 | + "FLY_ACCESS_TOKEN": fieldname.Token, |
| 44 | +} |
| 45 | + |
| 46 | +func TryFlyctlConfigFile() sdk.Importer { |
| 47 | + return importer.TryFile("~/.fly/config.yml", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { |
| 48 | + var config Config |
| 49 | + if err := contents.ToYAML(&config); err != nil { |
| 50 | + out.AddError(err) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + if config.Token == "" { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + out.AddCandidate(sdk.ImportCandidate{ |
| 59 | + Fields: map[sdk.FieldName]string{ |
| 60 | + fieldname.Token: config.Token, |
| 61 | + }, |
| 62 | + }) |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +type Config struct { |
| 67 | + Token string `yaml:"access_token"` |
| 68 | +} |
0 commit comments