Skip to content

Commit 22a8966

Browse files
committed
chore: introduce flags helper for consistency
1 parent 833d30d commit 22a8966

File tree

14 files changed

+261
-38
lines changed

14 files changed

+261
-38
lines changed

.golangci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ linters:
5050
- $test
5151
allow:
5252
- $gostd
53-
- github.com/golang/mock/gomock
5453
- github.com/openfga/api/proto
5554
- github.com/openfga/cli
5655
- github.com/openfga/go-sdk
5756
- github.com/openfga/openfga
57+
- github.com/spf13/cobra
5858
- github.com/stretchr
5959
- go.uber.org/mock/gomock
6060
funlen:

cmd/model/get.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/openfga/cli/internal/authorizationmodel"
2626
"github.com/openfga/cli/internal/cmdutils"
27+
"github.com/openfga/cli/internal/flags"
2728
"github.com/openfga/cli/internal/output"
2829
)
2930

@@ -77,8 +78,8 @@ func init() {
7778
getCmd.Flags().StringArray("field", []string{"model"}, "Fields to display, choices are: id, created_at and model") //nolint:lll
7879
getCmd.Flags().Var(&getOutputFormat, "format", `Authorization model output format. Can be "fga" or "json"`)
7980

80-
if err := getCmd.MarkFlagRequired("store-id"); err != nil {
81-
fmt.Printf("error setting flag as required - %v: %v\n", "cmd/models/get", err)
81+
if err := flags.SetFlagRequired(getCmd, "store-id", "cmd/models/get", false); err != nil {
82+
fmt.Printf("%v\n", err)
8283
os.Exit(1)
8384
}
8485
}

cmd/model/list.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/openfga/cli/internal/authorizationmodel"
2929
"github.com/openfga/cli/internal/cmdutils"
30+
"github.com/openfga/cli/internal/flags"
3031
"github.com/openfga/cli/internal/output"
3132
)
3233

@@ -112,8 +113,8 @@ func init() {
112113
listCmd.Flags().String("store-id", "", "Store ID")
113114
listCmd.Flags().StringArray("field", []string{"id", "created_at"}, "Fields to display, choices are: id, created_at and model") //nolint:lll
114115

115-
if err := listCmd.MarkFlagRequired("store-id"); err != nil {
116-
fmt.Printf("error setting flag as required - %v: %v\n", "cmd/models/list", err)
116+
if err := flags.SetFlagRequired(listCmd, "store-id", "cmd/models/list", false); err != nil {
117+
fmt.Printf("%v\n", err)
117118
os.Exit(1)
118119
}
119120
}

cmd/model/test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/spf13/cobra"
2525

2626
"github.com/openfga/cli/internal/cmdutils"
27+
"github.com/openfga/cli/internal/flags"
2728
"github.com/openfga/cli/internal/output"
2829
"github.com/openfga/cli/internal/storetest"
2930
)
@@ -99,8 +100,8 @@ func init() {
99100
testCmd.Flags().Bool("verbose", false, "Print verbose JSON output")
100101
testCmd.Flags().Bool("suppress-summary", false, "Suppress the plain text summary output")
101102

102-
if err := testCmd.MarkFlagRequired("tests"); err != nil {
103-
fmt.Printf("error setting flag as required - %v: %v\n", "cmd/models/test", err)
103+
if err := flags.SetFlagRequired(testCmd, "tests", "cmd/models/test", false); err != nil {
104+
fmt.Printf("%v\n", err)
104105
os.Exit(1)
105106
}
106107
}

cmd/model/write.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/openfga/cli/internal/authorizationmodel"
2929
"github.com/openfga/cli/internal/cmdutils"
30+
"github.com/openfga/cli/internal/flags"
3031
"github.com/openfga/cli/internal/output"
3132
"github.com/openfga/cli/internal/utils"
3233
)
@@ -106,8 +107,8 @@ func init() {
106107
writeCmd.Flags().String("file", "", "File Name. The file should have the model in the JSON or DSL format")
107108
writeCmd.Flags().Var(&writeInputFormat, "format", `Authorization model input format. Can be "fga", "json", or "modular"`) //nolint:lll
108109

109-
if err := writeCmd.MarkFlagRequired("store-id"); err != nil {
110-
fmt.Printf("error setting flag as required - %v: %v\n", "cmd/models/write", err)
110+
if err := flags.SetFlagRequired(writeCmd, "store-id", "cmd/model/write", false); err != nil {
111+
fmt.Printf("%v\n", err)
111112
os.Exit(1)
112113
}
113114
}

cmd/query/list-users.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/spf13/cobra"
2828

2929
"github.com/openfga/cli/internal/cmdutils"
30+
"github.com/openfga/cli/internal/flags"
3031
"github.com/openfga/cli/internal/output"
3132
)
3233

@@ -138,18 +139,11 @@ func init() {
138139
listUsersCmd.Flags().String("relation", "", "Relation to evaluate on")
139140
listUsersCmd.Flags().String("user-filter", "", "Filter the responses can be in the formats <type> (to filter objects and typed public bound access) or <type>#<relation> (to filter usersets)") //nolint:lll
140141

141-
if err := listUsersCmd.MarkFlagRequired("object"); err != nil {
142-
fmt.Printf("error setting flag as required - %v: %v\n", "cmd/query/list-users", err)
143-
os.Exit(1)
144-
}
145-
146-
if err := listUsersCmd.MarkFlagRequired("relation"); err != nil {
147-
fmt.Printf("error setting flag as required - %v: %v\n", "cmd/query/list-users", err)
148-
os.Exit(1)
149-
}
150-
151-
if err := listUsersCmd.MarkFlagRequired("user-filter"); err != nil {
152-
fmt.Printf("error setting flag as required - %v: %v\n", "cmd/query/list-users", err)
142+
if err := flags.SetFlagsRequired(
143+
listUsersCmd,
144+
[]string{"object", "relation", "user-filter"},
145+
"cmd/query/list-users", false); err != nil {
146+
fmt.Printf("%v\n", err)
153147
os.Exit(1)
154148
}
155149
}

cmd/query/query.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"os"
2323

2424
"github.com/spf13/cobra"
25+
26+
"github.com/openfga/cli/internal/flags"
2527
)
2628

2729
// QueryCmd represents the query command.
@@ -48,9 +50,8 @@ func init() {
4850
"Consistency preference for the request. Valid options are HIGHER_CONSISTENCY and MINIMIZE_LATENCY.",
4951
)
5052

51-
err := QueryCmd.MarkPersistentFlagRequired("store-id")
52-
if err != nil {
53-
fmt.Print(err)
53+
if err := flags.SetFlagRequired(QueryCmd, "store-id", "cmd/query/query", true); err != nil {
54+
fmt.Printf("%v\n", err)
5455
os.Exit(1)
5556
}
5657
}

cmd/store/delete.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/openfga/cli/internal/cmdutils"
2727
"github.com/openfga/cli/internal/confirmation"
28+
"github.com/openfga/cli/internal/flags"
2829
"github.com/openfga/cli/internal/output"
2930
)
3031

@@ -72,9 +73,8 @@ func init() {
7273
deleteCmd.Flags().String("store-id", "", "Store ID")
7374
deleteCmd.Flags().Bool("force", false, "Force delete without confirmation")
7475

75-
err := deleteCmd.MarkFlagRequired("store-id")
76-
if err != nil {
77-
fmt.Print(err)
76+
if err := flags.SetFlagRequired(deleteCmd, "store-id", "cmd/store/delete", false); err != nil {
77+
fmt.Printf("%v\n", err)
7878
os.Exit(1)
7979
}
8080
}

cmd/store/export.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/openfga/cli/internal/cmdutils"
3131
"github.com/openfga/cli/internal/confirmation"
3232
"github.com/openfga/cli/internal/fga"
33+
"github.com/openfga/cli/internal/flags"
3334
"github.com/openfga/cli/internal/output"
3435
"github.com/openfga/cli/internal/storetest"
3536
"github.com/openfga/cli/internal/tuple"
@@ -193,9 +194,8 @@ func init() {
193194
exportCmd.Flags().String("model-id", "", "Authorization Model ID")
194195
exportCmd.Flags().Uint("max-tuples", defaultMaxTupleCount, "max number of tuples to return in the output")
195196

196-
err := exportCmd.MarkFlagRequired("store-id")
197-
if err != nil {
198-
fmt.Print(err)
197+
if err := flags.SetFlagRequired(exportCmd, "store-id", "cmd/store/export", false); err != nil {
198+
fmt.Printf("%v\n", err)
199199
os.Exit(1)
200200
}
201201
}

cmd/store/get.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/openfga/cli/internal/cmdutils"
2828
"github.com/openfga/cli/internal/fga"
29+
"github.com/openfga/cli/internal/flags"
2930
"github.com/openfga/cli/internal/output"
3031
)
3132

@@ -65,9 +66,8 @@ var getCmd = &cobra.Command{
6566
func init() {
6667
getCmd.Flags().String("store-id", "", "Store ID")
6768

68-
err := getCmd.MarkFlagRequired("store-id")
69-
if err != nil {
70-
fmt.Print(err)
69+
if err := flags.SetFlagRequired(getCmd, "store-id", "cmd/store/get", false); err != nil {
70+
fmt.Printf("%v\n", err)
7171
os.Exit(1)
7272
}
7373
}

0 commit comments

Comments
 (0)