forked from deviceinsight/kafkactl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request deviceinsight#7 from sladkoff/feature/alter-topics
Add `alter topic` command
- Loading branch information
Showing
7 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package alter | ||
|
||
import ( | ||
"github.com/deviceinsight/kafkactl/cmd/validation" | ||
"github.com/deviceinsight/kafkactl/operations" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var flags operations.AlterTopicFlags | ||
|
||
var cmdAlterTopic = &cobra.Command{ | ||
Use: "topic", | ||
Short: "alter a topic", | ||
Args: cobra.MinimumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
(&operations.TopicOperation{}).AlterTopic(args[0], flags) | ||
}, | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
return validation.ValidateAtLeastOneRequiredFlag(cmd) | ||
}, | ||
} | ||
|
||
func init() { | ||
cmdAlterTopic.Flags().Int32VarP(&flags.Partitions, "partitions", "p", flags.Partitions, "number of partitions") | ||
cmdAlterTopic.Flags().StringArrayVarP(&flags.Configs, "config", "c", flags.Configs, "configs in format `key=value`") | ||
cmdAlterTopic.Flags().BoolVarP(&flags.ValidateOnly, "validate-only", "v", false, "validate only") | ||
|
||
validation.MarkFlagAtLeastOneRequired(cmdAlterTopic.Flags(), "partitions") | ||
validation.MarkFlagAtLeastOneRequired(cmdAlterTopic.Flags(), "config") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package alter | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var CmdAlter = &cobra.Command{ | ||
Use: "alter", | ||
Short: "alter topics", | ||
} | ||
|
||
func init() { | ||
CmdAlter.AddCommand(cmdAlterTopic) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package validation | ||
|
||
import ( | ||
"errors" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"strings" | ||
) | ||
|
||
const ( | ||
BashCompAtLeastOneRequiredFlag = "cobra_annotation_bash_completion_at_least_one_required_flag" | ||
) | ||
|
||
func MarkFlagAtLeastOneRequired(flags *pflag.FlagSet, name string) error { | ||
return flags.SetAnnotation(name, BashCompAtLeastOneRequiredFlag, []string{"true"}) | ||
} | ||
|
||
func ValidateAtLeastOneRequiredFlag(cmd *cobra.Command) error { | ||
requiredError := true | ||
atLeastRequiredFlags := []string{} | ||
|
||
cmd.Flags().VisitAll(func(flag *pflag.Flag) { | ||
atLeastOneRequiredAnnotation := flag.Annotations[BashCompAtLeastOneRequiredFlag] | ||
if len(atLeastOneRequiredAnnotation) == 0 { | ||
return | ||
} | ||
|
||
flagRequired := atLeastOneRequiredAnnotation[0] == "true" | ||
|
||
if flagRequired { | ||
atLeastRequiredFlags = append(atLeastRequiredFlags, flag.Name) | ||
} | ||
|
||
if flag.Changed { | ||
requiredError = false | ||
} | ||
}) | ||
|
||
if requiredError { | ||
return errors.New("At least one of the following flags must be set: " + strings.Join(atLeastRequiredFlags[:], ", ")) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters