Skip to content

Commit b32d7b1

Browse files
committed
Refactor flag checking logic
1 parent 54d29a3 commit b32d7b1

File tree

3 files changed

+54
-39
lines changed

3 files changed

+54
-39
lines changed

cmd/namigo/flag.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/huangsam/namigo/internal/model"
7+
"github.com/urfave/cli/v2"
8+
)
9+
10+
// checkCountFlag checks for valid count flag.
11+
func checkCountFlag(ctx *cli.Context, i int) error {
12+
if i <= 0 {
13+
return fmt.Errorf("count %d is invalid", i)
14+
}
15+
return nil
16+
}
17+
18+
// checkLengthFlag checks for valid length flag.
19+
func checkLengthFlag(ctx *cli.Context, i int) error {
20+
if i <= 0 {
21+
return fmt.Errorf("length %d is invalid", i)
22+
}
23+
return nil
24+
}
25+
26+
// checkFormatFlag checks for valid format flag.
27+
func checkFormatFlag(ctx *cli.Context, s string) error {
28+
if s != model.TextValue && s != model.JSONValue {
29+
return fmt.Errorf("format %s is invalid", s)
30+
}
31+
return nil
32+
}

cmd/namigo/main.go

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66

77
"github.com/huangsam/namigo/cmd/namigo/sub"
8+
"github.com/huangsam/namigo/internal/model"
89
"github.com/urfave/cli/v2"
910
)
1011

@@ -38,26 +39,16 @@ func main() {
3839
Usage: "Target interests",
3940
},
4041
&cli.IntFlag{
41-
Name: "count",
42-
Usage: "Maximum count of names",
43-
Value: 10,
44-
Action: func(ctx *cli.Context, i int) error {
45-
if i <= 0 {
46-
return fmt.Errorf("count %d is invalid", i)
47-
}
48-
return nil
49-
},
42+
Name: "count",
43+
Usage: "Maximum count of names",
44+
Value: 10,
45+
Action: checkCountFlag,
5046
},
5147
&cli.IntFlag{
52-
Name: "length",
53-
Usage: "Maximum length for each name",
54-
Value: 20,
55-
Action: func(ctx *cli.Context, i int) error {
56-
if i <= 0 {
57-
return fmt.Errorf("length %d is invalid", i)
58-
}
59-
return nil
60-
},
48+
Name: "length",
49+
Usage: "Maximum length for each name",
50+
Value: 20,
51+
Action: checkLengthFlag,
6152
},
6253
},
6354
Action: sub.GeneratePromptAction,
@@ -69,26 +60,16 @@ func main() {
6960
Usage: "Search for terms across entities",
7061
Flags: []cli.Flag{
7162
&cli.IntFlag{
72-
Name: "count",
73-
Usage: "Maximum count of results",
74-
Value: 10,
75-
Action: func(ctx *cli.Context, i int) error {
76-
if i <= 0 {
77-
return fmt.Errorf("count %d is invalid", i)
78-
}
79-
return nil
80-
},
63+
Name: "count",
64+
Usage: "Maximum count of results",
65+
Value: 10,
66+
Action: checkCountFlag,
8167
},
8268
&cli.StringFlag{
83-
Name: "format",
84-
Usage: "Output format can be text or json",
85-
Value: "text",
86-
Action: func(ctx *cli.Context, s string) error {
87-
if s != "text" && s != "json" {
88-
return fmt.Errorf("format %s is invalid", s)
89-
}
90-
return nil
91-
},
69+
Name: "format",
70+
Usage: fmt.Sprintf("Output format can be %s or %s", model.TextValue, model.JSONValue),
71+
Value: model.TextValue,
72+
Action: checkFormatFlag,
9273
},
9374
},
9475
Subcommands: []*cli.Command{

internal/model/format.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ package model
33
// OutputFormat represents the output mode.
44
type OutputFormat int
55

6-
// Output formats.
76
const (
87
TextFormat OutputFormat = iota
98
JSONFormat
9+
10+
TextValue = "text"
11+
JSONValue = "json"
1012
)
1113

1214
// GetOutputFormat returns an OutputMode instance.
1315
func GetOutputFormat(format string) OutputFormat {
1416
switch format {
15-
case "text":
17+
case TextValue:
1618
return TextFormat
17-
case "json":
19+
case JSONValue:
1820
return JSONFormat
1921
default:
2022
return TextFormat

0 commit comments

Comments
 (0)