Skip to content

Commit

Permalink
add IsBuiltinCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Aug 18, 2021
1 parent 7164715 commit 05384ab
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions cobrautil.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/jzelinskie/stringz"
"github.com/mattn/go-isatty"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
Expand All @@ -26,15 +27,24 @@ import (
"google.golang.org/grpc/keepalive"
)

func IsBuiltinCommand(cmd *cobra.Command) bool {
return stringz.SliceContains([]string{
"help [command]",
"completion [command]",
},
cmd.Use,
)
}

// SyncViperPreRunE returns a Cobra run func that synchronizes Viper environment
// flags prefixed with the provided argument.
//
// Thanks to Carolyn Van Slyck: https://github.com/carolynvs/stingoftheviper
func SyncViperPreRunE(prefix string) func(cmd *cobra.Command, args []string) error {
prefix = strings.ReplaceAll(strings.ToUpper(prefix), "-", "_")
return func(cmd *cobra.Command, args []string) error {
if cmd.Use == "help [command]" {
return nil // No-op the help command
if IsBuiltinCommand(cmd) {
return nil // No-op for builtins
}

v := viper.New()
Expand Down Expand Up @@ -81,8 +91,8 @@ func RegisterZeroLogFlags(flags *pflag.FlagSet) {
//
// This function exits with log.Fatal on failure.
func ZeroLogPreRunE(cmd *cobra.Command, args []string) error {
if cmd.Use == "help [command]" {
return nil // No-op the help command
if IsBuiltinCommand(cmd) {
return nil // No-op for builtins
}

format := MustGetString(cmd, "log-format")
Expand Down Expand Up @@ -129,8 +139,8 @@ func RegisterOpenTelemetryFlags(flags *pflag.FlagSet, serviceName string) {
// corresponding tracing provider. The required flags can be added to a command
// by using RegisterTracingPersistentFlags().
func OpenTelemetryPreRunE(cmd *cobra.Command, args []string) error {
if cmd.Use == "help [command]" {
return nil // No-op the help command
if IsBuiltinCommand(cmd) {
return nil // No-op for builtins
}

provider := strings.ToLower(MustGetString(cmd, "otel-provider"))
Expand Down

0 comments on commit 05384ab

Please sign in to comment.