github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/cmd/spicedb/main.go (about) 1 package main 2 3 import ( 4 "errors" 5 "fmt" 6 "os" 7 8 mcobra "github.com/muesli/mango-cobra" 9 "github.com/muesli/roff" 10 "github.com/rs/zerolog" 11 "github.com/sercand/kuberesolver/v5" 12 "github.com/spf13/cobra" 13 "google.golang.org/grpc/balancer" 14 _ "google.golang.org/grpc/xds" 15 16 log "github.com/authzed/spicedb/internal/logging" 17 "github.com/authzed/spicedb/pkg/cmd" 18 cmdutil "github.com/authzed/spicedb/pkg/cmd/server" 19 "github.com/authzed/spicedb/pkg/cmd/testserver" 20 _ "github.com/authzed/spicedb/pkg/runtime" 21 "github.com/authzed/spicedb/pkg/spiceerrors" 22 ) 23 24 var errParsing = errors.New("parsing error") 25 26 func main() { 27 // Enable Kubernetes gRPC resolver 28 kuberesolver.RegisterInCluster() 29 30 // Enable consistent hashring gRPC load balancer 31 balancer.Register(cmdutil.ConsistentHashringBuilder) 32 33 log.SetGlobalLogger(zerolog.New(os.Stderr).Level(zerolog.InfoLevel)) 34 35 // Create a root command 36 rootCmd := cmd.NewRootCommand("spicedb") 37 rootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { 38 cmd.Println(err) 39 cmd.Println(cmd.UsageString()) 40 return errParsing 41 }) 42 if err := cmd.RegisterRootFlags(rootCmd); err != nil { 43 log.Fatal().Err(err).Msg("failed to register root flags") 44 } 45 46 // Add a version command 47 versionCmd := cmd.NewVersionCommand(rootCmd.Use) 48 cmd.RegisterVersionFlags(versionCmd) 49 rootCmd.AddCommand(versionCmd) 50 51 // Add datastore commands 52 datastoreCmd, err := cmd.NewDatastoreCommand(rootCmd.Use) 53 if err != nil { 54 log.Fatal().Err(err).Msg("failed to register datastore command") 55 } 56 57 cmd.RegisterDatastoreRootFlags(datastoreCmd) 58 rootCmd.AddCommand(datastoreCmd) 59 60 // Add deprecated head command 61 headCmd := cmd.NewHeadCommand(rootCmd.Use) 62 cmd.RegisterHeadFlags(headCmd) 63 headCmd.Hidden = true 64 headCmd.RunE = cmd.DeprecatedRunE(headCmd.RunE, "spicedb datastore head") 65 rootCmd.AddCommand(headCmd) 66 67 // Add deprecated migrate command 68 migrateCmd := cmd.NewMigrateCommand(rootCmd.Use) 69 migrateCmd.Hidden = true 70 migrateCmd.RunE = cmd.DeprecatedRunE(migrateCmd.RunE, "spicedb datastore migrate") 71 cmd.RegisterMigrateFlags(migrateCmd) 72 rootCmd.AddCommand(migrateCmd) 73 74 // Add server commands 75 serverConfig := cmdutil.NewConfigWithOptionsAndDefaults() 76 serveCmd := cmd.NewServeCommand(rootCmd.Use, serverConfig) 77 if err := cmd.RegisterServeFlags(serveCmd, serverConfig); err != nil { 78 log.Fatal().Err(err).Msg("failed to register server flags") 79 } 80 rootCmd.AddCommand(serveCmd) 81 82 devtoolsCmd := cmd.NewDevtoolsCommand(rootCmd.Use) 83 cmd.RegisterDevtoolsFlags(devtoolsCmd) 84 rootCmd.AddCommand(devtoolsCmd) 85 86 lspConfig := new(cmd.LSPConfig) 87 lspCmd := cmd.NewLSPCommand(rootCmd.Use, lspConfig) 88 if err := cmd.RegisterLSPFlags(lspCmd, lspConfig); err != nil { 89 log.Fatal().Err(err).Msg("failed to register lsp flags") 90 } 91 rootCmd.AddCommand(lspCmd) 92 93 var testServerConfig testserver.Config 94 testingCmd := cmd.NewTestingCommand(rootCmd.Use, &testServerConfig) 95 cmd.RegisterTestingFlags(testingCmd, &testServerConfig) 96 rootCmd.AddCommand(testingCmd) 97 98 rootCmd.AddCommand(&cobra.Command{ 99 Use: "man", 100 Short: "Generate the SpiceDB manpage", 101 SilenceUsage: true, 102 DisableFlagsInUseLine: true, 103 Hidden: true, 104 Args: cobra.NoArgs, 105 RunE: func(cmd *cobra.Command, args []string) error { 106 manPage, err := mcobra.NewManPage(1, cmd.Root()) 107 if err != nil { 108 return err 109 } 110 111 _, err = fmt.Fprint(os.Stdout, manPage.Build(roff.NewDocument())) 112 return err 113 }, 114 }) 115 116 if err := rootCmd.Execute(); err != nil { 117 if !errors.Is(err, errParsing) { 118 log.Err(err).Msg("terminated with errors") 119 } 120 var termErr spiceerrors.TerminationError 121 if errors.As(err, &termErr) { 122 os.Exit(termErr.ExitCode()) 123 } 124 os.Exit(1) 125 } 126 }