Skip to content

Commit

Permalink
pkg/cmd: root programName and share ExampleServe
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Dec 9, 2021
1 parent 1845227 commit e4f83e1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cmd/spicedb/main.go
Expand Up @@ -36,7 +36,7 @@ func main() {
))

// Create a root command
rootCmd := root.NewCommand()
rootCmd := root.NewCommand("spicedb")
root.RegisterFlags(rootCmd)

// Add a version command
Expand Down
22 changes: 22 additions & 0 deletions pkg/cmd/defaults.go
@@ -1,14 +1,34 @@
package cmd

import (
"fmt"
"net/http"
"net/http/pprof"

"github.com/fatih/color"
"github.com/jzelinskie/cobrautil"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
)

// ServeExample creates an example usage string with the provided program name.
func ServeExample(programName string) string {
return fmt.Sprintf(` %[1]s:
%[3]s serve --grpc-preshared-key "somerandomkeyhere"
%[2]s:
%[3]s serve --grpc-preshared-key "realkeyhere" --grpc-tls-cert-path path/to/tls/cert --grpc-tls-key-path path/to/tls/key \
--http-tls-cert-path path/to/tls/cert --http-tls-key-path path/to/tls/key \
--datastore-engine postgres --datastore-conn-uri "postgres-connection-string-here"
`,
color.YellowString("No TLS and in-memory"),
color.GreenString("TLS and a real datastore"),
programName,
)
}

// DefaultPreRunE sets up viper, zerolog, and OpenTelemetry flag handling for a
// command.
func DefaultPreRunE(programName string) cobrautil.CobraRunFunc {
return cobrautil.CommandStack(
cobrautil.SyncViperPreRunE(programName),
Expand All @@ -17,6 +37,8 @@ func DefaultPreRunE(programName string) cobrautil.CobraRunFunc {
)
}

// MetricsHandler sets up an HTTP server that handles serving Prometheus
// metrics and pprof endpoints.
func MetricsHandler() http.Handler {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
Expand Down
28 changes: 7 additions & 21 deletions pkg/cmd/root/root.go
@@ -1,36 +1,22 @@
package root

import (
"fmt"

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

cmdutil "github.com/authzed/spicedb/pkg/cmd"
)

func RegisterFlags(cmd *cobra.Command) {
cobrautil.RegisterZeroLogFlags(cmd.PersistentFlags(), "log")
cobrautil.RegisterOpenTelemetryFlags(cmd.PersistentFlags(), "otel", cmd.Use)
}

func NewCommand() *cobra.Command {
func NewCommand(programName string) *cobra.Command {
return &cobra.Command{
Use: "spicedb",
Short: "A modern permissions database",
Long: "A database that stores, computes, and validates application permissions",
Example: fmt.Sprintf(` %s:
spicedb serve --grpc-preshared-key "somerandomkeyhere"
%s:
spicedb serve --grpc-preshared-key "realkeyhere" --grpc-tls-cert-path path/to/tls/cert --grpc-tls-key-path path/to/tls/key \
--http-tls-cert-path path/to/tls/cert --http-tls-key-path path/to/tls/key \
--datastore-engine postgres --datastore-conn-uri "postgres-connection-string-here"
%s:
spicedb serve-testing
`,
color.YellowString("No TLS and in-memory"),
color.GreenString("TLS and a real datastore"),
color.CyanString("In-memory integration test server"),
),
Use: programName,
Short: "A modern permissions database",
Long: "A database that stores, computes, and validates application permissions",
Example: cmdutil.ServeExample(programName),
}
}
11 changes: 1 addition & 10 deletions pkg/cmd/serve/serve.go
Expand Up @@ -2,13 +2,11 @@ package serve

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"

"github.com/fatih/color"
grpcauth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpczerolog "github.com/grpc-ecosystem/go-grpc-middleware/providers/zerolog/v2"
grpclog "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
Expand Down Expand Up @@ -89,14 +87,7 @@ func NewServeCommand(programName string, dsConfig *cmdutil.DatastoreConfig) *cob
Run: func(cmd *cobra.Command, args []string) {
serveRun(cmd, args, dsConfig)
},
Example: fmt.Sprintf(` %s:
spicedb serve --grpc-preshared-key "somerandomkeyhere"
%s:
spicedb serve --grpc-preshared-key "realkeyhere" --grpc-tls-cert-path path/to/tls/cert --grpc-tls-key-path path/to/tls/key \
--http-tls-cert-path path/to/tls/cert --http-tls-key-path path/to/tls/key \
--datastore-engine postgres --datastore-conn-uri "postgres-connection-string-here"
`, color.YellowString("No TLS and in-memory"), color.GreenString("TLS and a real datastore")),
Example: cmdutil.ServeExample(programName),
}
}

Expand Down

0 comments on commit e4f83e1

Please sign in to comment.