Skip to content

Commit 503729f

Browse files
ralphbeanclaude
andcommitted
Simplify OutputFormat from int to string type
Change OutputFormat from an int-based custom type with String(), Set(), and Type() methods to simple string constants. This eliminates unnecessary type conversion logic between int and string. - Remove OutputFormat custom type and its methods - Use string constants: OutputFormatText = "text", OutputFormatJSON = "json" - Update format field in outputOptions from OutputFormat to string - Change flag registration from Var() to StringVar() in audit.go and verifycommit.go - Update tests to use string type instead of OutputFormat type - Remove obsolete tests for OutputFormat.String() and OutputFormat.Set() This addresses code review feedback requesting a simpler string-based approach instead of the int-based enum 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 019882f commit 503729f

File tree

4 files changed

+6
-107
lines changed

4 files changed

+6
-107
lines changed

internal/cmd/audit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (ao *auditOpts) AddFlags(cmd *cobra.Command) {
114114
ao.auditMode = AuditModeBasic
115115
cmd.PersistentFlags().Var(&ao.auditMode, "audit-mode", "'basic' for limited details (default), 'full' for all details")
116116
ao.format = OutputFormatText
117-
cmd.PersistentFlags().Var(&ao.format, "format", "Output format: 'text' (default) or 'json'")
117+
cmd.PersistentFlags().StringVar(&ao.format, "format", OutputFormatText, "Output format: 'text' (default) or 'json'")
118118
}
119119

120120
func addAudit(parentCmd *cobra.Command) {

internal/cmd/output.go

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,19 @@ package cmd
55

66
import (
77
"encoding/json"
8-
"errors"
98
"fmt"
109
"io"
1110
"os"
1211
)
1312

14-
type OutputFormat int
15-
1613
const (
17-
OutputFormatText OutputFormat = 1
18-
OutputFormatJSON OutputFormat = 2
14+
OutputFormatText = "text"
15+
OutputFormatJSON = "json"
1916
)
2017

21-
// String is used both by fmt.Print and by Cobra in help text
22-
func (e *OutputFormat) String() string {
23-
switch *e {
24-
case OutputFormatText:
25-
return "text"
26-
case OutputFormatJSON:
27-
return "json"
28-
}
29-
return "error"
30-
}
31-
32-
// Set must have pointer receiver so it doesn't change the value of a copy
33-
func (e *OutputFormat) Set(v string) error {
34-
switch v {
35-
case "text":
36-
*e = OutputFormatText
37-
return nil
38-
case "json":
39-
*e = OutputFormatJSON
40-
return nil
41-
default:
42-
return errors.New(`must be one of "text" or "json"`)
43-
}
44-
}
45-
46-
// Type is only used in help text
47-
func (e *OutputFormat) Type() string {
48-
return "OutputFormat"
49-
}
50-
5118
// outputOptions provides common output formatting options
5219
type outputOptions struct {
53-
format OutputFormat
20+
format string
5421
writer io.Writer
5522
}
5623

internal/cmd/verifycommit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (vco *verifyCommitOptions) AddFlags(cmd *cobra.Command) {
5656
&vco.tag, "tag", "", "The tag within the repository",
5757
)
5858
vco.format = OutputFormatText
59-
cmd.PersistentFlags().Var(&vco.format, "format", "Output format: 'text' (default) or 'json'")
59+
cmd.PersistentFlags().StringVar(&vco.format, "format", OutputFormatText, "Output format: 'text' (default) or 'json'")
6060
}
6161

6262
//nolint:dupl

internal/cmd/verifycommit_test.go

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestOutputOptions_WriteJSON(t *testing.T) {
126126
func TestOutputOptions_IsJSON(t *testing.T) {
127127
tests := []struct {
128128
name string
129-
format OutputFormat
129+
format string
130130
want bool
131131
}{
132132
{
@@ -150,71 +150,3 @@ func TestOutputOptions_IsJSON(t *testing.T) {
150150
})
151151
}
152152
}
153-
154-
func TestOutputFormat_String(t *testing.T) {
155-
tests := []struct {
156-
name string
157-
format OutputFormat
158-
want string
159-
}{
160-
{
161-
name: "text format",
162-
format: OutputFormatText,
163-
want: "text",
164-
},
165-
{
166-
name: "JSON format",
167-
format: OutputFormatJSON,
168-
want: "json",
169-
},
170-
}
171-
172-
for _, tt := range tests {
173-
t.Run(tt.name, func(t *testing.T) {
174-
if got := tt.format.String(); got != tt.want {
175-
t.Errorf("String() = %v, want %v", got, tt.want)
176-
}
177-
})
178-
}
179-
}
180-
181-
func TestOutputFormat_Set(t *testing.T) {
182-
tests := []struct {
183-
name string
184-
value string
185-
want OutputFormat
186-
wantErr bool
187-
}{
188-
{
189-
name: "set to text",
190-
value: "text",
191-
want: OutputFormatText,
192-
wantErr: false,
193-
},
194-
{
195-
name: "set to json",
196-
value: "json",
197-
want: OutputFormatJSON,
198-
wantErr: false,
199-
},
200-
{
201-
name: "invalid value",
202-
value: "invalid",
203-
wantErr: true,
204-
},
205-
}
206-
207-
for _, tt := range tests {
208-
t.Run(tt.name, func(t *testing.T) {
209-
var format OutputFormat
210-
err := format.Set(tt.value)
211-
if (err != nil) != tt.wantErr {
212-
t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr)
213-
return
214-
}
215-
if !tt.wantErr && format != tt.want {
216-
t.Errorf("Set() got = %v, want %v", format, tt.want)
217-
}
218-
})
219-
}
220-
}

0 commit comments

Comments
 (0)