Im providing command line tool with several command and sub commands, Im using cobra command line and I’ve two separate commands that first is prerequisite to other
e.g. the first command is preferring the environment by creating temp folder and validate some file
The second command should get some properties from the first command
and user should execute it like
btr prepare
btr run
when the run command
is executed it should get some data from the prepare
command outcome
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "btr",
Short: "piping process",
}
var prepare = &cobra.Command{
Use: "pre",
Short: "Prepare The environment" ,
Run: func(cmd *cobra.Command, args []string) {
//This creating the temp folder and validate some configuration file
tmpFolderPath,parsedFile := exe.PreProcess()
},
}
var initProcess = &cobra.Command{
Use: “run”,
Short: “run process”,
Run: func(cmd *cobra.Command, args []string) {
//Here I need those two properties
run(tmpFolderPath, ParsedFile)
},
}
func init() {
rootCmd.AddCommand(prepare,initProcess)
}
UPDATE
Well, the answer below doest really help. I need to share state between two command in local & cloud env) , how I can do it that if I run the command line commands from shell script that call to 1 command and then call to the second which need to get some state from the first command, I need E2E solution with code real example in my context
update 2
let say that I understand that I need config file (json) ,
Where should I create it (path)?
When to clean it ?
in case I use 1file How should I validate to store data which is relevant for specific process and take other process data when needed (guid ) ?
lets say I've config like following
type config struct{
path string,
wd string,
id string,//guid?
}