Need help, how to invoke command from another command with given args?
I have a command called start, stop and restart command. I would like to call start and stop from command restart.
How to do this?
Hi @jeevatkm! Currently there isn't an easy way to do that, but that is a good feature idea.
A workaround might be to create the commands as standalone structs assigned to variables, initialize the context and call yourCommand.Run() directly. I don't know exactly what that would look like, but it seems like it'd be feasible.
In your specific case, I'd recommend pulling out the functionality of start and stop into separate methods that are called by the start and stop subcommands. Then you could just call start and stop in the restart subcommand.
I agree it'd be good feature to have in cli.
I think I will go with your second thought 'having methods and calling respectively'. Thanks!
I also think this would be a nice feature to have. Invoking the underlying action function directly is a workaround, but what if in the future we have some middleware kind of feature executed before the actions get called or after? I would love those to be triggered.
You can do it like this:
c.App.Run([]string{c.App.Name, "stop", param1, param2})
c.App.Run([]string{c.App.Name, "start", foobar})
Where c is the current context.
@jeevatkm is @iigorr's suggestion acceptable?
@meatballhat actually I haven't tried @iigorr suggestion as of now. I have implemented with methods and calling those methods. It was pushed to prod on end of Jan, 2016.
@jeevatkm Got it! OK with you if we close this out?
Yes, please. thanks.
馃憤
Most helpful comment
You can do it like this:
Where
cis the current context.