Skip to content

Commit

Permalink
Merge pull request #109 from 1Password/digitalocean_config_file_importer
Browse files Browse the repository at this point in the history
DigitalOcean config file importer for MacOS
  • Loading branch information
hculea committed Jan 24, 2023
2 parents 832c910 + d6803d6 commit 5dae3fe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
33 changes: 32 additions & 1 deletion plugins/digitalocean/personal_access_token.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package digitalocean

import (
"context"

"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/importer"
"github.com/1Password/shell-plugins/sdk/provision"
Expand Down Expand Up @@ -32,6 +34,35 @@ func PersonalAccessToken() schema.CredentialType {
DefaultProvisioner: provision.EnvVars(map[string]sdk.FieldName{
"DIGITALOCEAN_ACCESS_TOKEN": fieldname.Token,
}),
Importer: importer.TryAllEnvVars(fieldname.Token, "DIGITALOCEAN_ACCESS_TOKEN"),
Importer: importer.TryAll(
importer.TryAllEnvVars(fieldname.Token, "DIGITALOCEAN_ACCESS_TOKEN"),
importer.MacOnly(
TryDigitalOceanConfigFile("~/Library/Application Support/doctl/config.yaml"),
),
),
}
}

func TryDigitalOceanConfigFile(path string) sdk.Importer {
return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
var config Config
if err := contents.ToYAML(&config); err != nil {
out.AddError(err)
return
}

if config.AccessToken == "" {
return
}

out.AddCandidate(sdk.ImportCandidate{
Fields: map[sdk.FieldName]string{
fieldname.Token: config.AccessToken,
},
})
})
}

type Config struct {
AccessToken string `yaml:"access-token"`
}
13 changes: 13 additions & 0 deletions plugins/digitalocean/personal_access_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ func TestPersonalAccessTokenImporter(t *testing.T) {
},
},
},
"config file macos": {
OS: "darwin",
Files: map[string]string{
"~/Library/Application Support/doctl/config.yaml": plugintest.LoadFixture(t, "config.yaml"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "dop_v1_tr33mpd5m8q9t3ncisqbceydi8dd2n60pl1yiycg97z25fkqffp8j6ycjexample",
},
},
},
},
})
}

Expand Down
4 changes: 4 additions & 0 deletions plugins/digitalocean/test-fixtures/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
access-token: dop_v1_tr33mpd5m8q9t3ncisqbceydi8dd2n60pl1yiycg97z25fkqffp8j6ycjexample
config: ${HOME}/Library/Application Support/doctl/config.yaml
context: default
output: text

0 comments on commit 5dae3fe

Please sign in to comment.