Skip to content

Commit 826f7aa

Browse files
ralphbeanclaude
andcommitted
Add AddFlags() and Validate() methods to outputOptions
Add architecture methods to outputOptions to follow the established pattern used by other option sets in the codebase (branchOptions, verifierOptions, etc.). - Add AddFlags() method to initialize format and register the --format flag - Add Validate() method to verify format is either 'text' or 'json' - Add test coverage for Validate() method with valid and invalid inputs - Import cobra package for AddFlags() implementation This prepares outputOptions to be properly integrated into embedding option sets following the command architecture pattern. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> Signed-off-by: Ralph Bean <[email protected]>
1 parent 403b1fa commit 826f7aa

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

internal/cmd/output.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"fmt"
99
"io"
1010
"os"
11+
12+
"github.com/spf13/cobra"
1113
)
1214

1315
const (
@@ -20,6 +22,20 @@ type outputOptions struct {
2022
format string
2123
}
2224

25+
// AddFlags adds output-related flags to the command
26+
func (oo *outputOptions) AddFlags(cmd *cobra.Command) {
27+
oo.format = OutputFormatText
28+
cmd.PersistentFlags().StringVar(&oo.format, "format", OutputFormatText, "Output format: 'text' (default) or 'json'")
29+
}
30+
31+
// Validate checks that the output format is valid
32+
func (oo *outputOptions) Validate() error {
33+
if oo.format != OutputFormatText && oo.format != OutputFormatJSON {
34+
return fmt.Errorf("output format must be 'text' or 'json', got: %s", oo.format)
35+
}
36+
return nil
37+
}
38+
2339
// getWriter returns the writer to use for output (currently always os.Stdout)
2440
func (oo *outputOptions) getWriter() io.Writer {
2541
return os.Stdout

internal/cmd/verifycommit_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,37 @@ func TestOutputOptions_OutputFormatIsJSON(t *testing.T) {
136136
})
137137
}
138138
}
139+
140+
func TestOutputOptions_Validate(t *testing.T) {
141+
tests := []struct {
142+
name string
143+
format string
144+
wantErr bool
145+
}{
146+
{
147+
name: "valid text format",
148+
format: OutputFormatText,
149+
wantErr: false,
150+
},
151+
{
152+
name: "valid JSON format",
153+
format: OutputFormatJSON,
154+
wantErr: false,
155+
},
156+
{
157+
name: "invalid format",
158+
format: "invalid",
159+
wantErr: true,
160+
},
161+
}
162+
163+
for _, tt := range tests {
164+
t.Run(tt.name, func(t *testing.T) {
165+
opts := outputOptions{format: tt.format}
166+
err := opts.Validate()
167+
if (err != nil) != tt.wantErr {
168+
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
169+
}
170+
})
171+
}
172+
}

0 commit comments

Comments
 (0)