github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/cmd/root.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/jzelinskie/cobrautil/v2"
     7  	"github.com/jzelinskie/cobrautil/v2/cobraotel"
     8  	"github.com/jzelinskie/cobrautil/v2/cobrazerolog"
     9  	"github.com/spf13/cobra"
    10  
    11  	log "github.com/authzed/spicedb/internal/logging"
    12  	"github.com/authzed/spicedb/pkg/cmd/server"
    13  	"github.com/authzed/spicedb/pkg/cmd/termination"
    14  	"github.com/authzed/spicedb/pkg/releases"
    15  	"github.com/authzed/spicedb/pkg/runtime"
    16  )
    17  
    18  func RegisterRootFlags(cmd *cobra.Command) error {
    19  	zl := cobrazerolog.New()
    20  	zl.RegisterFlags(cmd.PersistentFlags())
    21  	if err := zl.RegisterFlagCompletion(cmd); err != nil {
    22  		return fmt.Errorf("failed to register zerolog flag completion: %w", err)
    23  	}
    24  
    25  	ot := cobraotel.New(cmd.Use)
    26  	ot.RegisterFlags(cmd.PersistentFlags())
    27  	if err := ot.RegisterFlagCompletion(cmd); err != nil {
    28  		return fmt.Errorf("failed to register otel flag completion: %w", err)
    29  	}
    30  
    31  	releases.RegisterFlags(cmd.PersistentFlags())
    32  	termination.RegisterFlags(cmd.PersistentFlags())
    33  	runtime.RegisterFlags(cmd.PersistentFlags())
    34  
    35  	return nil
    36  }
    37  
    38  // DeprecatedRunE wraps the RunFunc with a warning log statement.
    39  func DeprecatedRunE(fn cobrautil.CobraRunFunc, newCmd string) cobrautil.CobraRunFunc {
    40  	return func(cmd *cobra.Command, args []string) error {
    41  		log.Warn().Str("newCommand", newCmd).Msg("use of deprecated command")
    42  		return fn(cmd, args)
    43  	}
    44  }
    45  
    46  func NewRootCommand(programName string) *cobra.Command {
    47  	return &cobra.Command{
    48  		Use:           programName,
    49  		Short:         "A modern permissions database",
    50  		Long:          "A database that stores, computes, and validates application permissions",
    51  		Example:       server.ServeExample(programName),
    52  		SilenceErrors: true,
    53  		SilenceUsage:  true,
    54  	}
    55  }