Cobra CLI has support for PostRun to invoke after execution of a command.
https://github.com/spf13/cobra#prerun-and-postrun-hooks
How do I pass on the command status to PostRun call? I have requirement of posting the command status after its execution to a server
Cleaner approach would be to leverage the Annotations provided in cobra command
Really liked the approach @Bracken has taken here, Although there are some tweaks that will make it work
----- Old Answer -----
If I understand it right, There is some status that needs to be passed from cmd.Execute to PostRun.
You can use
ExecuteContext(ctx context.Context)
method instead ofExecute()
and set whatever key-value needs to be set in context.Same values can be Retrieved inside PostRun using
cmd.Context()