Files

71 lines
2.5 KiB
Go
Raw Permalink Normal View History

2026-08-17 10:14:44 -07:00
package cli
import (
"github.com/spf13/cobra"
)
// globalFlags holds the resolved values of the root command's persistent
// flags after parsing — read by subcommands when they build a client.
type globalFlags struct {
server string
token string
output string
insecure bool
clientID string
clientSecret string
tokenURL string
}
// NewRootCmd builds the kmsvc root command.
func NewRootCmd() *cobra.Command {
cfg := LoadConfig()
flags := &globalFlags{
server: cfg.Server,
output: cfg.Output,
insecure: cfg.Insecure,
clientID: cfg.ClientID,
tokenURL: cfg.TokenURL,
}
root := &cobra.Command{
Use: "kmsvc",
Short: "Kafka Management Service CLI",
SilenceUsage: true,
SilenceErrors: false,
// Secrets resolved from env/config aren't pre-bound to the flag's
// pflag default (which --help prints verbatim) — apply them here
// instead, only when the user didn't pass the flag explicitly.
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Flags().Changed("token") {
flags.token = cfg.Token
}
if !cmd.Flags().Changed("client-secret") {
flags.clientSecret = cfg.ClientSecret
}
return nil
},
}
root.PersistentFlags().StringVar(&flags.server, "server", flags.server, "kmsvc gRPC server address (env KMSVC_SERVER)")
root.PersistentFlags().StringVar(&flags.token, "token", "", "bearer token (env KMSVC_TOKEN)")
root.PersistentFlags().StringVar(&flags.output, "output", flags.output, "output format: table|json (env KMSVC_OUTPUT)")
root.PersistentFlags().BoolVar(&flags.insecure, "insecure", flags.insecure, "use plaintext gRPC instead of TLS (env KMSVC_INSECURE) — for cluster-internal/dev targets only")
root.PersistentFlags().StringVar(&flags.clientID, "client-id", flags.clientID, "OAuth2 client_credentials client ID, used to fetch a token when --token is unset (env KMSVC_CLIENT_ID)")
root.PersistentFlags().StringVar(&flags.clientSecret, "client-secret", "", "OAuth2 client_credentials client secret (env KMSVC_CLIENT_SECRET)")
root.PersistentFlags().StringVar(&flags.tokenURL, "token-url", flags.tokenURL, "OAuth2 token endpoint used with --client-id/--client-secret (env KMSVC_TOKEN_URL)")
root.AddCommand(
newMessageSendCmd(flags),
newMessageReceiveCmd(flags),
newMessageDeleteCmd(flags),
newMessageChangeVisibilityCmd(flags),
newDLQCmd(flags),
newQueueCmd(flags),
newQueueCreateCmd(flags),
newQueueDeleteCmd(flags),
newVersionCmd(),
)
return root
}