Skip to content

Commit

Permalink
Merge pull request #2074 from authzed/register-common-flags
Browse files Browse the repository at this point in the history
Register common flags with helper
  • Loading branch information
tstirrat15 authored Sep 23, 2024
2 parents 09492d7 + aef7490 commit c0ba134
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
3 changes: 3 additions & 0 deletions pkg/cmd/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/authzed/spicedb/pkg/cmd/datastore"
"github.com/authzed/spicedb/pkg/cmd/server"
"github.com/authzed/spicedb/pkg/cmd/termination"
"github.com/authzed/spicedb/pkg/cmd/util"
dspkg "github.com/authzed/spicedb/pkg/datastore"
)

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

repairCmd := NewRepairDatastoreCommand(programName, cfg)
if err := datastore.RegisterDatastoreFlagsWithPrefix(repairCmd.Flags(), "", cfg); err != nil {
return nil, err
}
util.RegisterCommonFlags(repairCmd)
datastoreCmd.AddCommand(repairCmd)

headCmd := NewHeadCommand(programName)
Expand Down
8 changes: 2 additions & 6 deletions pkg/cmd/devtools.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/jzelinskie/cobrautil/v2"
"github.com/jzelinskie/cobrautil/v2/cobragrpc"
"github.com/jzelinskie/cobrautil/v2/cobrahttp"
"github.com/jzelinskie/cobrautil/v2/cobraotel"
"github.com/jzelinskie/stringz"
"github.com/spf13/cobra"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
Expand All @@ -29,7 +28,7 @@ import (
v0svc "github.com/authzed/spicedb/internal/services/v0"
"github.com/authzed/spicedb/pkg/cmd/server"
"github.com/authzed/spicedb/pkg/cmd/termination"
"github.com/authzed/spicedb/pkg/runtime"
"github.com/authzed/spicedb/pkg/cmd/util"
)

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

otel := cobraotel.New(cmd.Use)
otel.RegisterFlags(cmd.Flags())
termination.RegisterFlags(cmd.Flags())
runtime.RegisterFlags(cmd.Flags())
util.RegisterCommonFlags(cmd)
}

func NewDevtoolsCommand(programName string) *cobra.Command {
Expand Down
9 changes: 3 additions & 6 deletions pkg/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/fatih/color"
"github.com/jzelinskie/cobrautil/v2"
"github.com/jzelinskie/cobrautil/v2/cobraotel"
"github.com/spf13/cobra"

crdbmigrations "github.com/authzed/spicedb/internal/datastore/crdb/migrations"
Expand All @@ -17,9 +16,9 @@ import (
log "github.com/authzed/spicedb/internal/logging"
"github.com/authzed/spicedb/pkg/cmd/server"
"github.com/authzed/spicedb/pkg/cmd/termination"
"github.com/authzed/spicedb/pkg/cmd/util"
"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/migrate"
"github.com/authzed/spicedb/pkg/runtime"
)

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

otel := cobraotel.New("spicedb")
otel.RegisterFlags(cmd.Flags())
termination.RegisterFlags(cmd.Flags())
runtime.RegisterFlags(cmd.Flags())
util.RegisterCommonFlags(cmd)
}

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

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

func NewHeadCommand(programName string) *cobra.Command {
Expand Down
6 changes: 1 addition & 5 deletions pkg/cmd/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package cmd
import (
"context"

"github.com/jzelinskie/cobrautil/v2/cobraotel"
"github.com/spf13/cobra"

"github.com/authzed/spicedb/pkg/cmd/server"
"github.com/authzed/spicedb/pkg/cmd/termination"
"github.com/authzed/spicedb/pkg/cmd/testserver"
"github.com/authzed/spicedb/pkg/cmd/util"
"github.com/authzed/spicedb/pkg/runtime"
)

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

func NewTestingCommand(programName string, config *testserver.Config) *cobra.Command {
Expand Down
14 changes: 14 additions & 0 deletions pkg/cmd/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"time"

"github.com/jzelinskie/cobrautil/v2/cobraotel"
"github.com/jzelinskie/stringz"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
Expand All @@ -31,6 +32,8 @@ import (

"github.com/authzed/spicedb/internal/grpchelpers"
log "github.com/authzed/spicedb/internal/logging"
"github.com/authzed/spicedb/pkg/cmd/termination"
"github.com/authzed/spicedb/pkg/runtime"
"github.com/authzed/spicedb/pkg/x509util"
)

Expand Down Expand Up @@ -448,3 +451,14 @@ func (d *disabledHTTPServer) ListenAndServe() error {
}

func (d *disabledHTTPServer) Close() {}

// Registers flags that are common to many commands.
// NOTE: these used to be registered in the root command
// so that they were shared across all commands, but this
// made it difficult to organize the flags, so we lifted them here.
func RegisterCommonFlags(cmd *cobra.Command) {
otel := cobraotel.New("spicedb")
otel.RegisterFlags(cmd.Flags())
termination.RegisterFlags(cmd.Flags())
runtime.RegisterFlags(cmd.Flags())
}

0 comments on commit c0ba134

Please sign in to comment.