Skip to content

Commit

Permalink
Renaming to make new sub command code structure work better
Browse files Browse the repository at this point in the history
  • Loading branch information
fgrosse committed Feb 10, 2023
1 parent ad1db64 commit 0842cf1
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 57 deletions.
6 changes: 3 additions & 3 deletions cmd/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
)

type Command struct {
type command struct {
BaseCommand
*cobra.Command
logger *log.Logger
Expand All @@ -18,8 +18,8 @@ type BaseCommand interface {
SaveConfiguration() error
}

func NewCommand(base BaseCommand, logger *log.Logger) *cobra.Command {
cmd := &Command{
func Command(base BaseCommand, logger *log.Logger) *cobra.Command {
cmd := &command{
BaseCommand: base,
logger: logger,
Command: &cobra.Command{
Expand Down
4 changes: 2 additions & 2 deletions cmd/config/config_add_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/spf13/viper"
)

func (cmd *Command) ConfigAddContextCmd() *cobra.Command {
func (cmd *command) ConfigAddContextCmd() *cobra.Command {
addContextCmd := &cobra.Command{
Use: "add <CONTEXT_NAME>",
Args: cobra.ExactArgs(1),
Expand All @@ -23,7 +23,7 @@ func (cmd *Command) ConfigAddContextCmd() *cobra.Command {
return addContextCmd
}

func (cmd *Command) addContext(name string, brokers []string) error {
func (cmd *command) addContext(name string, brokers []string) error {
conf := cmd.Configuration()
err := conf.AddContext(name, brokers...)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/config/config_delete_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/spf13/cobra"
)

func (cmd *Command) ConfigDeleteContextCmd() *cobra.Command {
func (cmd *command) ConfigDeleteContextCmd() *cobra.Command {
return &cobra.Command{
Use: "delete <CONTEXT_NAME>",
Args: cobra.ExactArgs(1),
Expand All @@ -16,7 +16,7 @@ func (cmd *Command) ConfigDeleteContextCmd() *cobra.Command {
}
}

func (cmd *Command) deleteContext(name string) error {
func (cmd *command) deleteContext(name string) error {
conf := cmd.Configuration()
err := conf.DeleteContext(name)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/config/config_print.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/spf13/viper"
)

func (cmd *Command) ConfigPrintCmd() *cobra.Command {
func (cmd *command) ConfigPrintCmd() *cobra.Command {
printCmd := &cobra.Command{
Use: "print",
Args: cobra.NoArgs,
Expand Down
4 changes: 2 additions & 2 deletions cmd/config/config_rename_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/spf13/cobra"
)

func (cmd *Command) ConfigRenameContextCmd() *cobra.Command {
func (cmd *command) ConfigRenameContextCmd() *cobra.Command {
return &cobra.Command{
Use: "rename <OLD_CONTEXT_NAME> <NEW_CONTEXT_NAME>",
Args: cobra.ExactArgs(2),
Expand All @@ -17,7 +17,7 @@ func (cmd *Command) ConfigRenameContextCmd() *cobra.Command {
}
}

func (cmd *Command) renameContext(oldName, newName string) error {
func (cmd *command) renameContext(oldName, newName string) error {
conf := cmd.Configuration()
err := conf.RenameContext(oldName, newName)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/context/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/spf13/cobra"
)

type Command struct {
type command struct {
BaseCommand
*cobra.Command
}
Expand All @@ -15,7 +15,7 @@ type BaseCommand interface {
SaveConfiguration() error
}

func NewCommand(base BaseCommand) *cobra.Command {
cmd := &Command{BaseCommand: base}
func Command(base BaseCommand) *cobra.Command {
cmd := &command{BaseCommand: base}
return cmd.ContextCmd()
}
6 changes: 3 additions & 3 deletions cmd/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ type Context struct {
Brokers []string `table:"-"`
}

func (cmd *Command) ContextCmd() *cobra.Command {
func (cmd *command) ContextCmd() *cobra.Command {
contextCmd := &cobra.Command{
Use: "context [CONTEXT_NAME]",
Args: cobra.MaximumNArgs(1),
Short: "Switch between different configuration contexts (e.g. prod, staging, local)",
Short: "Switch between different configuration contexts",
Long: `Switch between different configurations contexts (e.g. prod, staging, local).
The kafkactl context feature is conceptionally similar to how kubectl manages cluster configuration.
Expand Down Expand Up @@ -59,7 +59,7 @@ add or remove new configuration contexts using the 'kafkactl config' sub command
return contextCmd
}

func (cmd *Command) runContextCmd(contextName, encoding string) error {
func (cmd *command) runContextCmd(contextName, encoding string) error {
conf := cmd.Configuration()
if len(conf.Contexts) == 0 {
return fmt.Errorf("this command requires at least one configured context. Please use kafkactl config --help to get started")
Expand Down
8 changes: 4 additions & 4 deletions cmd/get/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/spf13/cobra"
)

type Command struct {
type command struct {
BaseCommand
*cobra.Command
logger *log.Logger
Expand All @@ -22,14 +22,14 @@ type BaseCommand interface {
ConnectAdmin() (sarama.ClusterAdmin, error)
}

func NewCommand(base BaseCommand, logger, debug *log.Logger) *cobra.Command {
cmd := &Command{
func Command(base BaseCommand, logger, debug *log.Logger) *cobra.Command {
cmd := &command{
BaseCommand: base,
logger: logger,
debug: debug,
Command: &cobra.Command{
Use: "get",
Short: "Display one or many resources in the Kafka cluster",
Short: "Display resources in the Kafka cluster",
Example: `
# List all topics in a Kafka cluster
kafkactl get topics
Expand Down
6 changes: 3 additions & 3 deletions cmd/get/get_brokers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"github.com/spf13/viper"
)

// Broker contains information displayed by "get brokers".
// Broker contains information displayed by "kafkactl get brokers".
type Broker struct {
ID int32
Addr string
Rack string `json:",omitempty"`
}

func (cmd *Command) GetBrokersCmd() *cobra.Command {
func (cmd *command) GetBrokersCmd() *cobra.Command {
return &cobra.Command{
Use: "brokers",
Args: cobra.ExactArgs(0),
Expand All @@ -27,7 +27,7 @@ func (cmd *Command) GetBrokersCmd() *cobra.Command {
}
}

func (cmd *Command) getBrokers(encoding string) error {
func (cmd *command) getBrokers(encoding string) error {
conf := cmd.SaramaConfig()
client, err := cmd.ConnectClient(conf)
if err != nil {
Expand Down
23 changes: 12 additions & 11 deletions cmd/get/get_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ import (
"github.com/spf13/viper"
)

func (cmd *Command) GetConfigCmd() *cobra.Command {
// Config contains information displayed by "kafkactl get config".
type Config struct {
Name string `table:"NAME"`
Value string `table:"VALUE"`
Default bool `table:"DEFAULT"`
ReadOnly bool `table:"READ_ONLY"`
Sensitive bool `table:"-"`
Source string `table:"-"`
}

func (cmd *command) GetConfigCmd() *cobra.Command {
getConfigCmd := &cobra.Command{
Use: "config",
Short: "Get Kafka broker or topic configuration",
Expand Down Expand Up @@ -49,7 +59,7 @@ kafkactl get config auto.create.topics.enable
return getConfigCmd
}

func (cmd *Command) getConfig(encoding, topic string, args []string) error {
func (cmd *command) getConfig(encoding, topic string, args []string) error {
filter := map[string]bool{}
for _, arg := range args {
filter[arg] = true
Expand Down Expand Up @@ -91,15 +101,6 @@ func (cmd *Command) getConfig(encoding, topic string, args []string) error {
return err
}

type Config struct {
Name string `table:"NAME"`
Value string `table:"VALUE"`
Default bool `table:"DEFAULT"`
Sensitive bool `table:"-"`
ReadOnly bool `table:"READ_ONLY"`
Source string `table:"-"`
}

output := make([]Config, 0, len(resp))
for _, conf := range resp {
if _, ok := filter[conf.Name]; len(filter) > 0 && !ok {
Expand Down
14 changes: 7 additions & 7 deletions cmd/get/get_consumer_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/spf13/viper"
)

// ConsumerGroup contains information displayed by "get consumer".
// ConsumerGroup contains information displayed by "kafkactl get consumer".
type ConsumerGroup struct {
GroupID string
Protocol string `table:"-"`
Expand All @@ -26,7 +26,7 @@ type ConsumerGroup struct {
OffsetsSummary string `json:"-" yaml:"-" table:"OFFSETS"`
}

// GroupMember contains information displayed by "get group".
// GroupMember contains information displayed by "kafkactl get consumer".
type GroupMember struct {
ID string
ClientID string
Expand All @@ -35,7 +35,7 @@ type GroupMember struct {
UserData json.RawMessage
}

// GroupOffset contains information displayed by "get group".
// GroupOffset contains information displayed by "kafkactl get consumer".
type GroupOffset struct {
Topic string
Partition int32
Expand All @@ -44,7 +44,7 @@ type GroupOffset struct {
PendingMessages int64
}

func (cmd *Command) GetConsumerGroupsCmd() *cobra.Command {
func (cmd *command) GetConsumerGroupsCmd() *cobra.Command {
return &cobra.Command{
Use: "consumers [GROUP_NAME]",
Args: cobra.MaximumNArgs(1),
Expand All @@ -61,15 +61,15 @@ func (cmd *Command) GetConsumerGroupsCmd() *cobra.Command {
}
}

func (cmd *Command) getConsumerGroups(name, encoding string) error {
func (cmd *command) getConsumerGroups(name, encoding string) error {
if name == "" {
return cmd.listGroups(encoding)
}

return cmd.getConsumerGroup(name, encoding)
}

func (cmd *Command) listGroups(encoding string) error {
func (cmd *command) listGroups(encoding string) error {
admin, err := cmd.ConnectAdmin()
if err != nil {
return err
Expand All @@ -92,7 +92,7 @@ func (cmd *Command) listGroups(encoding string) error {
return cli.Print(encoding, groups)
}

func (cmd *Command) getConsumerGroup(groupID, encoding string) error {
func (cmd *command) getConsumerGroup(groupID, encoding string) error {
conf := cmd.SaramaConfig()
client, err := cmd.ConnectClient(conf)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions cmd/get/get_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ import (
"github.com/spf13/viper"
)

func (cmd *Command) GetMessageCmd() *cobra.Command {
func (cmd *command) GetMessageCmd() *cobra.Command {
getMessageCmd := &cobra.Command{
Use: "message --topic=foo --offset=123",
Args: cobra.NoArgs,
Short: "Consume messages from a Kafka cluster",
Example: `
# Print message with offset 81041238 from topic my-fancy-topic
Command get message --topic=my-fancy-topic --offset=81041238
kafkactl get message --topic=my-fancy-topic --offset=81041238
# Read offsets from std in and print all corresponding messages
kubectl logs -l app=my-app | jq 'select(…) | .offset' | Command get message --offset=- --topic=my-fancy-topic
kubectl logs -l app=my-app | jq 'select(…) | .offset' | kafkactl get message --offset=- --topic=my-fancy-topic
`,
RunE: func(_ *cobra.Command, args []string) error {
ctx := cli.Context()
Expand All @@ -50,7 +50,7 @@ kubectl logs -l app=my-app | jq 'select(…) | .offset' | Command get message --
return getMessageCmd
}

func (cmd *Command) getMessage(ctx context.Context, offset, topic string, partition int32, encoding string) error {
func (cmd *command) getMessage(ctx context.Context, offset, topic string, partition int32, encoding string) error {
if encoding != "json" && encoding != "raw" {
return errors.New("only JSON and raw output are supported by this sub command")
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func (cmd *Command) getMessage(ctx context.Context, offset, topic string, partit
return printMessage(offset)
}

func (cmd *Command) fetchMessageForOffset(topic string, partition int32, offset int64) (*sarama.ConsumerMessage, error) {
func (cmd *command) fetchMessageForOffset(topic string, partition int32, offset int64) (*sarama.ConsumerMessage, error) {
conf := cmd.SaramaConfig()
conf.Metadata.Full = false // we are only interested in very specific topics
conf.Producer.Return.Successes = true
Expand Down
16 changes: 8 additions & 8 deletions cmd/get/get_topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type PartitionMetadata struct {
OfflineReplicas []int32
}

func (cmd *Command) GetTopicsCmd() *cobra.Command {
func (cmd *command) GetTopicsCmd() *cobra.Command {
getTopicsCmd := &cobra.Command{
Use: "topics [TOPIC_NAME]",
Aliases: []string{"topic"},
Expand All @@ -58,7 +58,7 @@ func (cmd *Command) GetTopicsCmd() *cobra.Command {
return getTopicsCmd
}

func (cmd *Command) getTopics(showAll bool, regex, encoding string, args []string) error {
func (cmd *command) getTopics(showAll bool, regex, encoding string, args []string) error {
var rexp *regexp.Regexp
if regex != "" {
var err error
Expand Down Expand Up @@ -95,7 +95,7 @@ func (cmd *Command) getTopics(showAll bool, regex, encoding string, args []strin
// empty set for all topics or one or more names to get information on specific
// topics. Pass regexEnabled=true to parse the first topicsArgs element as a
// regex. If showAll=true internal kafka topics will be displayed.
func (cmd *Command) fetchTopics(client sarama.Client, admin sarama.ClusterAdmin, topicsArgs []string, showAll bool, regex *regexp.Regexp) ([]Topic, error) {
func (cmd *command) fetchTopics(client sarama.Client, admin sarama.ClusterAdmin, topicsArgs []string, showAll bool, regex *regexp.Regexp) ([]Topic, error) {
topicMeta, err := cmd.fetchTopicMetaData(client, topicsArgs)
if err != nil {
return nil, err
Expand Down Expand Up @@ -147,7 +147,7 @@ func (cmd *Command) fetchTopics(client sarama.Client, admin sarama.ClusterAdmin,
return topics, nil
}

func (*Command) fetchTopicMetaData(client sarama.Client, topics []string) (map[string]*sarama.TopicMetadata, error) {
func (*command) fetchTopicMetaData(client sarama.Client, topics []string) (map[string]*sarama.TopicMetadata, error) {
b, err := client.Controller()
if err != nil {
return nil, fmt.Errorf("failed to get cluster controller broker: %w", err)
Expand All @@ -172,7 +172,7 @@ func isIgnoredTopic(name string) bool {
return strings.HasPrefix(name, "__")
}

func (cmd *Command) fetchTopicPartitions(client sarama.Client, topicName string, details sarama.TopicDetail, meta *sarama.TopicMetadata) []PartitionMetadata {
func (cmd *command) fetchTopicPartitions(client sarama.Client, topicName string, details sarama.TopicDetail, meta *sarama.TopicMetadata) []PartitionMetadata {
result := make([]PartitionMetadata, details.NumPartitions)
for i, p := range meta.Partitions {
offset, err := client.GetOffset(topicName, p.ID, sarama.OffsetNewest)
Expand All @@ -198,7 +198,7 @@ func (cmd *Command) fetchTopicPartitions(client sarama.Client, topicName string,
return result
}

func (cmd *Command) assignTopicConsumers(admin sarama.ClusterAdmin, topics []Topic) error {
func (cmd *command) assignTopicConsumers(admin sarama.ClusterAdmin, topics []Topic) error {
topicConsumers, err := cmd.fetchTopicConsumers(admin, topics)
if err != nil {
return err
Expand All @@ -217,7 +217,7 @@ func (cmd *Command) assignTopicConsumers(admin sarama.ClusterAdmin, topics []Top
return nil
}

func (*Command) fetchTopicConsumers(admin sarama.ClusterAdmin, topics []Topic) (map[string][]string, error) {
func (*command) fetchTopicConsumers(admin sarama.ClusterAdmin, topics []Topic) (map[string][]string, error) {
consumerGroups, err := admin.ListConsumerGroups()
if err != nil {
return nil, fmt.Errorf("failed to list consumer groups: %w", err)
Expand Down Expand Up @@ -255,7 +255,7 @@ func (*Command) fetchTopicConsumers(admin sarama.ClusterAdmin, topics []Topic) (
return topicConsumers, nil
}

func (*Command) getTopicRetention(details sarama.TopicDetail) string {
func (*command) getTopicRetention(details sarama.TopicDetail) string {
r := details.ConfigEntries["retention.ms"]
if r == nil {
return ""
Expand Down
Loading

0 comments on commit 0842cf1

Please sign in to comment.