Skip to content

Commit

Permalink
Add built-in support for XDG_CONFIG_HOME imports
Browse files Browse the repository at this point in the history
  • Loading branch information
florisvdg committed Nov 8, 2022
1 parent 0b3aa82 commit d44a749
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 20 deletions.
6 changes: 3 additions & 3 deletions plugins/circleci/personal_api_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func PersonalAPIToken() schema.CredentialType {
importer.TryEnvVarPair(map[string]string{
fieldname.Token: "CIRCLECI_CLI_TOKEN",
}),
TryCircleCIConfigFile(),
TryCircleCIConfigFile("~/.circleci/cli.yml"),
),
}
}
Expand All @@ -46,8 +46,8 @@ type Config struct {
Token string `yaml:"token"`
}

func TryCircleCIConfigFile() sdk.Importer {
return importer.TryFile("~/.circleci/cli.yml", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportOutput) {
func TryCircleCIConfigFile(path string) sdk.Importer {
return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportOutput) {
var config Config
if err := contents.ToYAML(&config); err != nil {
out.AddError(err)
Expand Down
6 changes: 3 additions & 3 deletions plugins/gitlab/personal_access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func PersonalAccessToken() schema.CredentialType {
Provisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
TryGlabConfigFile(),
TryGlabConfigFile("$XDG_CONFIG_HOME/glab-cli/config.yml"),
),
}
}
Expand All @@ -54,7 +54,7 @@ var defaultEnvVarMapping = map[string]string{
fieldname.APIHost: "GITLAB_API_HOST",
}

func TryGlabConfigFile() sdk.Importer {
// TODO: Try importing token from ~/.config/glab-cli/config.yml
func TryGlabConfigFile(path string) sdk.Importer {
// TODO: Try importing token from $XDG_CONFIG_HOME/glab-cli/config.yml
return importer.NoOp()
}
7 changes: 3 additions & 4 deletions plugins/heroku/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ func APIKey() schema.CredentialType {
}),
Importer: importer.TryAll(
importer.TryAllEnvVars(fieldname.APIKey, "HEROKU_API_KEY"),
TryNetrcFile(),
TryNetrcFile("~/.netrc"),
),
}
}

// TryNetrcFile tries to find Heroku API keys in the ~/.netrc file
func TryNetrcFile() sdk.Importer {
return importer.TryFile("~/.netrc", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportOutput) {
func TryNetrcFile(path string) sdk.Importer {
return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportOutput) {
// TODO: Iterate over 'machine' entries to look for 'api.heroku.com' or 'git.heroku.com'
// Example contents:
//
Expand Down
4 changes: 2 additions & 2 deletions plugins/okta/api_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func APIToken() schema.CredentialType {
Provisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
TryOktaConfigFile(),
TryOktaConfigFile("~/.okta/okta.yaml"),
),
}
}
Expand All @@ -48,7 +48,7 @@ var defaultEnvVarMapping = map[string]string{

const FieldNameOrgURL = "Org URL"

func TryOktaConfigFile() sdk.Importer {
func TryOktaConfigFile(path string) sdk.Importer {
// TODO: Try importing from ~/.okta/okta.yaml
return importer.NoOp()
}
6 changes: 3 additions & 3 deletions plugins/snyk/api_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ func APIToken() schema.CredentialType {
Provisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
TrySnykConfigFile(),
TrySnykConfigFile("$XDG_CONFIG_HOME/configstore/snyk.json"),
)}
}

var defaultEnvVarMapping = map[string]string{
fieldname.Token: "SNYK_TOKEN",
}

func TrySnykConfigFile() sdk.Importer {
// TODO: Import `api` field from ~/.config/configstore/snyk.json
func TrySnykConfigFile(path string) sdk.Importer {
// TODO: Import `api` field from $XDG_CONFIG_HOME/configstore/snyk.json
return importer.NoOp()
}
4 changes: 2 additions & 2 deletions plugins/vault/auth_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func AuthToken() schema.CredentialType {
Provisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
TryVaultTokenFile(),
TryVaultTokenFile("~/.vault-token"),
),
}
}
Expand All @@ -45,7 +45,7 @@ var defaultEnvVarMapping = map[string]string{
fieldname.Namespace: "VAULT_NAMESPACE",
}

func TryVaultTokenFile() sdk.Importer {
func TryVaultTokenFile(path string) sdk.Importer {
// TODO: Try importing from ~/.vault-token file
return importer.NoOp()
}
3 changes: 2 additions & 1 deletion sdk/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ type ImportSource struct {
}

type ImportInput struct {
HomeDir string
HomeDir string
XDGConfigHome string
}

type ImportOutput struct {
Expand Down
6 changes: 4 additions & 2 deletions sdk/importer/file_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (

func TryFile(path string, result func(ctx context.Context, contents FileContents, in sdk.ImportInput, out *sdk.ImportOutput)) sdk.Importer {
return func(ctx context.Context, in sdk.ImportInput, out *sdk.ImportOutput) {
if strings.HasPrefix(path, "~/") {
path = filepath.Join(in.HomeDir, strings.TrimPrefix(path, "~/"))
if strings.HasPrefix(path, "~") {
path = filepath.Join(in.HomeDir, strings.TrimPrefix(path, "~"))
} else if strings.HasPrefix(path, "$XDG_CONFIG_HOME") {
path = filepath.Join(in.XDGConfigHome, strings.TrimPrefix(path, "$XDG_CONFIG_HOME"))
}

contents, err := os.ReadFile(path)
Expand Down

0 comments on commit d44a749

Please sign in to comment.