forked from TrueCloudLab/frostfs-node
[#979] adm/subnet: Inherit pre-run functions in child commands
Cobra commands don't call `PreRun` functions on parent by default. We need to do it in `subnet` command of admin utility in order to inherit viper bindings. Add `inheritPreRun` function which makes sub-commands to call `PreRun` functions before its own `PreRun`. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
6facc26cb9
commit
579c3717a5
1 changed files with 29 additions and 5 deletions
|
@ -42,7 +42,7 @@ func viperBindFlags(cmd *cobra.Command, flags ...string) {
|
||||||
var cmdSubnet = &cobra.Command{
|
var cmdSubnet = &cobra.Command{
|
||||||
Use: "subnet",
|
Use: "subnet",
|
||||||
Short: "NeoFS subnet management.",
|
Short: "NeoFS subnet management.",
|
||||||
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
||||||
viperBindFlags(cmd,
|
viperBindFlags(cmd,
|
||||||
flagSubnetEndpoint,
|
flagSubnetEndpoint,
|
||||||
flagSubnetKey,
|
flagSubnetKey,
|
||||||
|
@ -378,7 +378,7 @@ const (
|
||||||
var cmdSubnetAdmin = &cobra.Command{
|
var cmdSubnetAdmin = &cobra.Command{
|
||||||
Use: "admin",
|
Use: "admin",
|
||||||
Short: "Manage administrators of the NeoFS subnet.",
|
Short: "Manage administrators of the NeoFS subnet.",
|
||||||
PreRun: func(cmd *cobra.Command, _ []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
viperBindFlags(cmd,
|
viperBindFlags(cmd,
|
||||||
flagSubnetAdminSubnet,
|
flagSubnetAdminSubnet,
|
||||||
flagSubnetAdminID,
|
flagSubnetAdminID,
|
||||||
|
@ -646,6 +646,30 @@ var cmdSubnetClientRemove = &cobra.Command{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// returns function which calls PreRun on parent if it exists.
|
||||||
|
func inheritPreRun(preRun func(*cobra.Command, []string)) func(*cobra.Command, []string) {
|
||||||
|
return func(cmd *cobra.Command, args []string) {
|
||||||
|
par := cmd.Parent()
|
||||||
|
if par != nil && par.PreRun != nil {
|
||||||
|
par.PreRun(par, args)
|
||||||
|
}
|
||||||
|
|
||||||
|
if preRun != nil {
|
||||||
|
preRun(cmd, args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// inherits PreRun function of parent command in all sub-commands and
|
||||||
|
// adds them to the parent.
|
||||||
|
func addCommandInheritPreRun(par *cobra.Command, subs ...*cobra.Command) {
|
||||||
|
for _, sub := range subs {
|
||||||
|
sub.PreRun = inheritPreRun(sub.PreRun)
|
||||||
|
}
|
||||||
|
|
||||||
|
par.AddCommand(subs...)
|
||||||
|
}
|
||||||
|
|
||||||
// registers flags and binds sub-commands for subnet commands.
|
// registers flags and binds sub-commands for subnet commands.
|
||||||
func init() {
|
func init() {
|
||||||
// get subnet flags
|
// get subnet flags
|
||||||
|
@ -683,13 +707,13 @@ func init() {
|
||||||
_ = cmdSubnetClient.MarkFlagRequired(flagSubnetClientID)
|
_ = cmdSubnetClient.MarkFlagRequired(flagSubnetClientID)
|
||||||
|
|
||||||
// add all admin managing commands to corresponding command section
|
// add all admin managing commands to corresponding command section
|
||||||
cmdSubnetAdmin.AddCommand(
|
addCommandInheritPreRun(cmdSubnetAdmin,
|
||||||
cmdSubnetAdminAdd,
|
cmdSubnetAdminAdd,
|
||||||
cmdSubnetAdminRemove,
|
cmdSubnetAdminRemove,
|
||||||
)
|
)
|
||||||
|
|
||||||
// add all client managing commands to corresponding command section
|
// add all client managing commands to corresponding command section
|
||||||
cmdSubnetClient.AddCommand(
|
addCommandInheritPreRun(cmdSubnetClient,
|
||||||
cmdSubnetClientAdd,
|
cmdSubnetClientAdd,
|
||||||
cmdSubnetClientRemove,
|
cmdSubnetClientRemove,
|
||||||
)
|
)
|
||||||
|
@ -705,7 +729,7 @@ func init() {
|
||||||
cmdSubnetFlags.Bool(flagSubnetNonNotary, false, "Flag to work in non-notary environment")
|
cmdSubnetFlags.Bool(flagSubnetNonNotary, false, "Flag to work in non-notary environment")
|
||||||
|
|
||||||
// add all subnet commands to corresponding command section
|
// add all subnet commands to corresponding command section
|
||||||
cmdSubnet.AddCommand(
|
addCommandInheritPreRun(cmdSubnet,
|
||||||
cmdSubnetCreate,
|
cmdSubnetCreate,
|
||||||
cmdSubnetRemove,
|
cmdSubnetRemove,
|
||||||
cmdSubnetGet,
|
cmdSubnetGet,
|
||||||
|
|
Loading…
Reference in a new issue