-
Notifications
You must be signed in to change notification settings - Fork 0
fix(config-validate): emit structured output for -o json/yaml on success #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+87
−1
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,8 @@ type Options struct { | |
| Client func() (*http.Client, error) | ||
| Config func() (config.Config, error) | ||
|
|
||
| File string | ||
| File string | ||
| Output string | ||
| } | ||
|
|
||
| func NewCmdValidate(f *cmd.Factory) *cobra.Command { | ||
|
|
@@ -44,6 +45,7 @@ func NewCmdValidate(f *cmd.Factory) *cobra.Command { | |
| if opts.File == "" { | ||
| return &cmdutil.FlagError{Err: fmt.Errorf("required flag \"file\" not set")} | ||
| } | ||
| opts.Output, _ = c.Flags().GetString("output") | ||
| return validateRun(opts) | ||
| }, | ||
| } | ||
|
|
@@ -53,6 +55,14 @@ func NewCmdValidate(f *cmd.Factory) *cobra.Command { | |
| return c | ||
| } | ||
|
|
||
| // validateResult is emitted on the success path when -o json or -o yaml is | ||
| // requested. Keeping it small and stable means downstream automation can pipe | ||
| // `a7 config validate -o json | jq '.valid'` without parsing English. | ||
| type validateResult struct { | ||
| Valid bool `json:"valid" yaml:"valid"` | ||
| Message string `json:"message,omitempty" yaml:"message,omitempty"` | ||
| } | ||
|
|
||
| func validateRun(opts *Options) error { | ||
| data, err := readFile(opts.File) | ||
| if err != nil { | ||
|
|
@@ -76,6 +86,13 @@ func validateRun(opts *Options) error { | |
| return fmt.Errorf("config validation failed:\n- %s", strings.Join(errs, "\n- ")) | ||
| } | ||
|
|
||
| switch opts.Output { | ||
| case "json", "yaml": | ||
| return cmdutil.NewExporter(opts.Output, opts.IO.Out).Write(validateResult{ | ||
| Valid: true, | ||
| Message: "Config is valid", | ||
| }) | ||
| } | ||
|
Comment on lines
+89
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return an error for unsupported non-empty output values (Lines 89-95). Right now unknown values (e.g. Proposed fix switch opts.Output {
case "json", "yaml":
return cmdutil.NewExporter(opts.Output, opts.IO.Out).Write(validateResult{
Valid: true,
Message: "Config is valid",
})
+ case "", "table":
+ fmt.Fprintln(opts.IO.Out, "Config is valid")
+ return nil
+ default:
+ return fmt.Errorf("unsupported output format: %s", opts.Output)
}
- fmt.Fprintln(opts.IO.Out, "Config is valid")
- return nil🤖 Prompt for AI Agents |
||
| fmt.Fprintln(opts.IO.Out, "Config is valid") | ||
| return nil | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: api7/a7
Length of output: 197
🏁 Script executed:
Repository: api7/a7
Length of output: 9674
🏁 Script executed:
Repository: api7/a7
Length of output: 8978
🏁 Script executed:
Repository: api7/a7
Length of output: 9085
🏁 Script executed:
Repository: api7/a7
Length of output: 13353
🏁 Script executed:
Repository: api7/a7
Length of output: 15736
🏁 Script executed:
Repository: api7/a7
Length of output: 6821
🏁 Script executed:
Repository: api7/a7
Length of output: 1446
🏁 Script executed:
Repository: api7/a7
Length of output: 15128
Handle
--outputflag error and reject unsupported output values inconfig validateopts.Output, _ = c.Flags().GetString("output")discards the returned error; return it instead to follow the “never suppress errors” rule.opts.Outputis non-empty but notjson/yaml, the command silently falls back to printing the human message ("Config is valid"), instead of erroring (in contrast to other commands that route throughcmdutil.NewExporter, which returnsunsupported output format).Proposed fix
🤖 Prompt for AI Agents