Skip to content

Commit c07b3bb

Browse files
committed
Allow setting explicit output format for single keys, but keep defaulting to value for single and json for multiple
1 parent 966b9a7 commit c07b3bb

1 file changed

Lines changed: 22 additions & 24 deletions

File tree

clicommand/secret_get.go

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ Gets a list of secrets from Buildkite and prints them to stdout. Key names are c
3838
insensitive in this command, and secret values are automatically redacted in the build logs
3939
unless the ′skip-redaction′ flag is used.
4040
41-
If any request for a secret fails, the command will return a non-zero exit code and print details of all failed secrets.
41+
If any request for a secret fails, the command will return a non-zero exit code and print
42+
details of all failed secrets.
4243
43-
If only a single key is provided, the secret value will be printed without any formatting.
44-
45-
If multiple keys are provided, the output format will be as defined by the ′format′ flag, which defaults to JSON.
44+
By default, when fetching a single key, the secret value will be printed without any
45+
formatting. When fetching multiple keys, the output will be in JSON format. Output
46+
format can be controlled explicitly with the ′format′ flag.
4647
4748
Examples:
4849
@@ -52,11 +53,13 @@ Examples:
5253
$ buildkite-agent secret get DEPLOY_KEY
5354
"..."
5455
55-
# Format is ignored when only a single key is provided
5656
$ buildkite-agent secret get --format env deploy_key
57-
"..."
57+
DEPLOY_KEY="..."
5858
59-
# JSON is the default format when multiple keys are provided
59+
$ buildkite-agent secret get --format json deploy_key
60+
{"deploy_key": "..."}
61+
62+
# JSON is the default format when multiple keys are provided...
6063
$ buildkite-agent secret get deploy_key github_api_token
6164
{"deploy_key": "...", "github_api_token": "..."}
6265
@@ -72,8 +75,8 @@ Examples:
7275
},
7376
cli.StringFlag{
7477
Name: "format",
75-
Usage: "The output format, either 'json' (default) or 'env'. Ignored when only a single key is provided",
76-
Value: "json",
78+
Usage: "The output format, either 'default', 'json', or 'env'. When 'default', a single secret will print just the value, while multiple secrets will print JSON. When 'json' or 'env', secrets will be printed as key-value pairs in the requested format",
79+
Value: "default",
7780
EnvVar: "BUILDKITE_AGENT_SECRET_GET_FORMAT",
7881
},
7982
cli.BoolFlag{
@@ -91,8 +94,8 @@ Examples:
9194
return errors.New("at least one secret key must be provided")
9295
}
9396

94-
if cfg.Format != "json" && cfg.Format != "env" {
95-
return fmt.Errorf("invalid format %q: must be either 'json' or 'env'", cfg.Format)
97+
if !slices.Contains([]string{"default", "json", "env"}, cfg.Format) {
98+
return fmt.Errorf("invalid format %q: must be one of 'default', 'json', or 'env'", cfg.Format)
9699
}
97100

98101
agentClient := api.NewClient(l, loadAPIClientConfig(cfg, "AgentAccessToken"))
@@ -122,32 +125,27 @@ Examples:
122125
}
123126
}
124127

125-
// If only a single key was requested, print the value without any formatting
126-
if len(secrets) == 1 {
127-
_, _ = fmt.Fprintln(c.App.Writer, secrets[0].Value)
128-
return nil
129-
}
130-
131128
// Otherwise, print in the requested format
132129
secretsMap := make(map[string]string, len(secrets))
133130
for _, secret := range secrets {
134131
secretsMap[secret.Key] = secret.Value
135132
}
136133

137-
switch cfg.Format {
138-
case "json":
134+
switch {
135+
case len(cfg.Keys) == 1 && cfg.Format == "default":
136+
_, _ = fmt.Fprintln(c.App.Writer, secrets[0].Value)
137+
return nil
138+
139+
case cfg.Format == "json" || (cfg.Format == "default" && len(cfg.Keys) > 1):
139140
if err := json.NewEncoder(c.App.Writer).Encode(secretsMap); err != nil {
140141
return fmt.Errorf("failed to write JSON response: %w", err)
141142
}
142143

143-
case "env":
144-
sb := strings.Builder{}
144+
case cfg.Format == "env":
145145
for _, key := range slices.Sorted(maps.Keys(secretsMap)) {
146-
sb.WriteString(fmt.Sprintf("%s=%q\n", strings.ToUpper(key), secretsMap[key]))
146+
fmt.Fprintf(c.App.Writer, "%s=%q\n", strings.ToUpper(key), secretsMap[key])
147147
}
148148

149-
_, _ = fmt.Fprint(c.App.Writer, sb.String())
150-
151149
default:
152150
return fmt.Errorf("unsupported format %q", cfg.Format)
153151
}

0 commit comments

Comments
 (0)