Skip to content

Create separate subcmds for get #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
256 changes: 173 additions & 83 deletions cmd/topicctl/subcmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package subcmd

import (
"context"
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -17,15 +16,10 @@ var getCmd = &cobra.Command{
Long: strings.Join(
[]string{
"Get instances of a particular type.",
"Supported types currently include: balance, brokers, config, groups, lags, members, partitions, offsets, and topics.",
"",
"See the tool README for a detailed description of each one.",
},
"\n",
),
Args: cobra.MinimumNArgs(1),
PreRunE: getPreRun,
RunE: getRun,
PersistentPreRunE: getPreRun,
}

type getCmdConfig struct {
Expand All @@ -38,109 +32,205 @@ type getCmdConfig struct {
var getConfig getCmdConfig

func init() {
getCmd.Flags().BoolVar(
getCmd.PersistentFlags().BoolVar(
Copy link
Contributor Author

@petedannemann petedannemann Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PersistentFlags allow us to pass these flags through from the parent command (get) to all the child commands

&getConfig.full,
"full",
false,
"Show more full information for resources",
)
getCmd.Flags().BoolVar(
getCmd.PersistentFlags().BoolVar(
&getConfig.sortValues,
"sort-values",
false,
"Sort by value instead of name; only applies for lags at the moment",
)

addSharedFlags(getCmd, &getConfig.shared)
getCmd.AddCommand(
balanceCmd(),
brokersCmd(),
configCmd(),
groupsCmd(),
lagsCmd(),
membersCmd(),
partitionsCmd(),
offsetsCmd(),
topicsCmd(),
)
RootCmd.AddCommand(getCmd)
}

func getPreRun(cmd *cobra.Command, args []string) error {
return getConfig.shared.validate()
}

func getRun(cmd *cobra.Command, args []string) error {
func getCliRunnerAndCtx() (
context.Context,
*cli.CLIRunner,
error,
) {
ctx := context.Background()
sess := session.Must(session.NewSession())

adminClient, err := getConfig.shared.getAdminClient(ctx, sess, true)
if err != nil {
return err
return nil, nil, err
}
defer adminClient.Close()

cliRunner := cli.NewCLIRunner(adminClient, log.Infof, !noSpinner)
return ctx, cliRunner, nil
}

func balanceCmd() *cobra.Command {
return &cobra.Command{
Use: "balance [optional topic]",
Short: "Number of replicas per broker position for topic or cluster as a whole",
Long: strings.Join([]string{
"Displays the number of replicas per broker position.",
"Accepts an optional argument of a topic, which will just scope this to that topic. If topic is omitted, the balance displayed will be for the entire cluster.",
},
"\n",
),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cliRunner, err := getCliRunnerAndCtx()
if err != nil {
return err
}

var topicName string
if len(args) == 1 {
topicName = args[0]
}
return cliRunner.GetBrokerBalance(ctx, topicName)
},
PreRunE: getPreRun,
}
}

resource := args[0]

switch resource {
case "balance":
var topicName string

if len(args) == 2 {
topicName = args[1]
} else if len(args) > 2 {
return fmt.Errorf("Can provide at most one positional argument with brokers")
}

return cliRunner.GetBrokerBalance(ctx, topicName)
case "brokers":
if len(args) > 1 {
return fmt.Errorf("Can only provide one positional argument with brokers")
}

return cliRunner.GetBrokers(ctx, getConfig.full)
case "config":
if len(args) != 2 {
return fmt.Errorf("Must provide broker ID or topic name as second positional argument")
}

return cliRunner.GetConfig(ctx, args[1])
case "groups":
if len(args) > 1 {
return fmt.Errorf("Can only provide one positional argument with groups")
}

return cliRunner.GetGroups(ctx)
case "lags":
if len(args) != 3 {
return fmt.Errorf("Must provide topic and groupID as additional positional arguments")
}

return cliRunner.GetMemberLags(
ctx,
args[1],
args[2],
getConfig.full,
getConfig.sortValues,
)
case "members":
if len(args) != 2 {
return fmt.Errorf("Must provide group ID as second positional argument")
}

return cliRunner.GetGroupMembers(ctx, args[1], getConfig.full)
case "partitions":
if len(args) != 2 {
return fmt.Errorf("Must provide topic as second positional argument")
}
topicName := args[1]

return cliRunner.GetPartitions(ctx, topicName)
case "offsets":
if len(args) != 2 {
return fmt.Errorf("Must provide topic as second positional argument")
}
topicName := args[1]

return cliRunner.GetOffsets(ctx, topicName)
case "topics":
if len(args) > 1 {
return fmt.Errorf("Can only provide one positional argument with args")
}

return cliRunner.GetTopics(ctx, getConfig.full)
default:
return fmt.Errorf("Unrecognized resource type: %s", resource)
func brokersCmd() *cobra.Command {
return &cobra.Command{
Use: "brokers",
Short: "Displays descriptions of each broker in the cluster.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cliRunner, err := getCliRunnerAndCtx()
if err != nil {
return err
}
return cliRunner.GetBrokers(ctx, getConfig.full)
},
}
}

func configCmd() *cobra.Command {
return &cobra.Command{
Use: "config [broker or topic]",
Short: "Displays configuration for the provider broker or topic.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cliRunner, err := getCliRunnerAndCtx()
if err != nil {
return err
}

return cliRunner.GetConfig(ctx, args[0])
},
}
}

func groupsCmd() *cobra.Command {
return &cobra.Command{
Use: "groups",
Short: "Displays consumer group informatin for the cluster.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cliRunner, err := getCliRunnerAndCtx()
if err != nil {
return err
}
return cliRunner.GetGroups(ctx)
},
}
}

func lagsCmd() *cobra.Command {
return &cobra.Command{
Use: "lags [topic] [group]",
Short: "Displays consumer group lag for the specified topic and consumer group.",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cliRunner, err := getCliRunnerAndCtx()
if err != nil {
return err
}
return cliRunner.GetMemberLags(
ctx,
args[0],
args[1],
getConfig.full,
getConfig.sortValues,
)
},
}
}

func membersCmd() *cobra.Command {
return &cobra.Command{
Use: "members [group]",
Short: "Details of each member in the specified consumer group.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cliRunner, err := getCliRunnerAndCtx()
if err != nil {
return err
}
return cliRunner.GetGroupMembers(ctx, args[0], getConfig.full)
},
}
}

func partitionsCmd() *cobra.Command {
return &cobra.Command{
Use: "partitions [topic]",
Short: "Displays partition information for the specified topic.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cliRunner, err := getCliRunnerAndCtx()
if err != nil {
return err
}
return cliRunner.GetPartitions(ctx, args[0])
},
}
}

func offsetsCmd() *cobra.Command {
return &cobra.Command{
Use: "offsets [topic]",
Short: "Displays offset information for the specified topic along with start and end times.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cliRunner, err := getCliRunnerAndCtx()
if err != nil {
return err
}
return cliRunner.GetOffsets(ctx, args[0])
},
}
}

func topicsCmd() *cobra.Command {
return &cobra.Command{
Use: "topics",
Short: "Displays information for all topics in the cluster.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cliRunner, err := getCliRunnerAndCtx()
if err != nil {
return err
}
return cliRunner.GetTopics(ctx, getConfig.full)
},
}
}
Loading