Skip to content

Commit 333532c

Browse files
committed
Merge pull request #2074 from authzed/register-common-flags
Register common flags with helper
1 parent d0e5fca commit 333532c

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

pkg/cmd/datastore.go

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/authzed/spicedb/pkg/cmd/datastore"
1313
"github.com/authzed/spicedb/pkg/cmd/server"
1414
"github.com/authzed/spicedb/pkg/cmd/termination"
15+
"github.com/authzed/spicedb/pkg/cmd/util"
1516
dspkg "github.com/authzed/spicedb/pkg/datastore"
1617
)
1718

@@ -35,12 +36,14 @@ func NewDatastoreCommand(programName string) (*cobra.Command, error) {
3536
if err := datastore.RegisterDatastoreFlagsWithPrefix(gcCmd.Flags(), "", cfg); err != nil {
3637
return nil, err
3738
}
39+
util.RegisterCommonFlags(gcCmd)
3840
datastoreCmd.AddCommand(gcCmd)
3941

4042
repairCmd := NewRepairDatastoreCommand(programName, cfg)
4143
if err := datastore.RegisterDatastoreFlagsWithPrefix(repairCmd.Flags(), "", cfg); err != nil {
4244
return nil, err
4345
}
46+
util.RegisterCommonFlags(repairCmd)
4447
datastoreCmd.AddCommand(repairCmd)
4548

4649
headCmd := NewHeadCommand(programName)

pkg/cmd/devtools.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/jzelinskie/cobrautil/v2"
1818
"github.com/jzelinskie/cobrautil/v2/cobragrpc"
1919
"github.com/jzelinskie/cobrautil/v2/cobrahttp"
20-
"github.com/jzelinskie/cobrautil/v2/cobraotel"
2120
"github.com/jzelinskie/stringz"
2221
"github.com/spf13/cobra"
2322
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
@@ -29,7 +28,7 @@ import (
2928
v0svc "github.com/authzed/spicedb/internal/services/v0"
3029
"github.com/authzed/spicedb/pkg/cmd/server"
3130
"github.com/authzed/spicedb/pkg/cmd/termination"
32-
"github.com/authzed/spicedb/pkg/runtime"
31+
"github.com/authzed/spicedb/pkg/cmd/util"
3332
)
3433

3534
func RegisterDevtoolsFlags(cmd *cobra.Command) {
@@ -45,10 +44,7 @@ func RegisterDevtoolsFlags(cmd *cobra.Command) {
4544
cmd.Flags().String("s3-endpoint", "", "s3 endpoint for s3 share store")
4645
cmd.Flags().String("s3-region", "auto", "s3 region for s3 share store")
4746

48-
otel := cobraotel.New(cmd.Use)
49-
otel.RegisterFlags(cmd.Flags())
50-
termination.RegisterFlags(cmd.Flags())
51-
runtime.RegisterFlags(cmd.Flags())
47+
util.RegisterCommonFlags(cmd)
5248
}
5349

5450
func NewDevtoolsCommand(programName string) *cobra.Command {

pkg/cmd/migrate.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/fatih/color"
99
"github.com/jzelinskie/cobrautil/v2"
10-
"github.com/jzelinskie/cobrautil/v2/cobraotel"
1110
"github.com/spf13/cobra"
1211

1312
crdbmigrations "github.com/authzed/spicedb/internal/datastore/crdb/migrations"
@@ -17,9 +16,9 @@ import (
1716
log "github.com/authzed/spicedb/internal/logging"
1817
"github.com/authzed/spicedb/pkg/cmd/server"
1918
"github.com/authzed/spicedb/pkg/cmd/termination"
19+
"github.com/authzed/spicedb/pkg/cmd/util"
2020
"github.com/authzed/spicedb/pkg/datastore"
2121
"github.com/authzed/spicedb/pkg/migrate"
22-
"github.com/authzed/spicedb/pkg/runtime"
2322
)
2423

2524
func RegisterMigrateFlags(cmd *cobra.Command) {
@@ -32,10 +31,7 @@ func RegisterMigrateFlags(cmd *cobra.Command) {
3231
cmd.Flags().Uint64("migration-backfill-batch-size", 1000, "number of items to migrate per iteration of a datastore backfill")
3332
cmd.Flags().Duration("migration-timeout", 1*time.Hour, "defines a timeout for the execution of the migration, set to 1 hour by default")
3433

35-
otel := cobraotel.New("spicedb")
36-
otel.RegisterFlags(cmd.Flags())
37-
termination.RegisterFlags(cmd.Flags())
38-
runtime.RegisterFlags(cmd.Flags())
34+
util.RegisterCommonFlags(cmd)
3935
}
4036

4137
func NewMigrateCommand(programName string) *cobra.Command {
@@ -149,6 +145,7 @@ func runMigration[D migrate.Driver[C, T], C any, T any](
149145

150146
func RegisterHeadFlags(cmd *cobra.Command) {
151147
cmd.Flags().String("datastore-engine", "postgres", fmt.Sprintf(`type of datastore to initialize (%s)`, datastore.EngineOptions()))
148+
util.RegisterCommonFlags(cmd)
152149
}
153150

154151
func NewHeadCommand(programName string) *cobra.Command {

pkg/cmd/testing.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ package cmd
33
import (
44
"context"
55

6-
"github.com/jzelinskie/cobrautil/v2/cobraotel"
76
"github.com/spf13/cobra"
87

98
"github.com/authzed/spicedb/pkg/cmd/server"
109
"github.com/authzed/spicedb/pkg/cmd/termination"
1110
"github.com/authzed/spicedb/pkg/cmd/testserver"
1211
"github.com/authzed/spicedb/pkg/cmd/util"
13-
"github.com/authzed/spicedb/pkg/runtime"
1412
)
1513

1614
func RegisterTestingFlags(cmd *cobra.Command, config *testserver.Config) {
@@ -32,9 +30,7 @@ func RegisterTestingFlags(cmd *cobra.Command, config *testserver.Config) {
3230
cmd.Flags().Uint32Var(&config.MaxLookupResourcesLimit, "max-lookup-resources-limit", 1000, "maximum number of resources that can be looked up in a single request")
3331
cmd.Flags().Uint32Var(&config.MaxBulkExportRelationshipsLimit, "max-bulk-export-relationships-limit", 10_000, "maximum number of relationships that can be exported in a single request")
3432
cmd.Flags().BoolVar(&config.EnableExperimentalLookupResources, "enable-experimental-lookup-resources", false, "enables the experimental version of the lookup resources API")
35-
otel := cobraotel.New("spicedb")
36-
otel.RegisterFlags(cmd.Flags())
37-
runtime.RegisterFlags(cmd.Flags())
33+
util.RegisterCommonFlags(cmd)
3834
}
3935

4036
func NewTestingCommand(programName string, config *testserver.Config) *cobra.Command {

pkg/cmd/util/util.go

+14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http"
1313
"time"
1414

15+
"github.com/jzelinskie/cobrautil/v2/cobraotel"
1516
"github.com/jzelinskie/stringz"
1617
"github.com/rs/zerolog"
1718
"github.com/spf13/cobra"
@@ -31,6 +32,8 @@ import (
3132

3233
"github.com/authzed/spicedb/internal/grpchelpers"
3334
log "github.com/authzed/spicedb/internal/logging"
35+
"github.com/authzed/spicedb/pkg/cmd/termination"
36+
"github.com/authzed/spicedb/pkg/runtime"
3437
"github.com/authzed/spicedb/pkg/x509util"
3538
)
3639

@@ -448,3 +451,14 @@ func (d *disabledHTTPServer) ListenAndServe() error {
448451
}
449452

450453
func (d *disabledHTTPServer) Close() {}
454+
455+
// Registers flags that are common to many commands.
456+
// NOTE: these used to be registered in the root command
457+
// so that they were shared across all commands, but this
458+
// made it difficult to organize the flags, so we lifted them here.
459+
func RegisterCommonFlags(cmd *cobra.Command) {
460+
otel := cobraotel.New("spicedb")
461+
otel.RegisterFlags(cmd.Flags())
462+
termination.RegisterFlags(cmd.Flags())
463+
runtime.RegisterFlags(cmd.Flags())
464+
}

0 commit comments

Comments
 (0)