dongxin1999 2018-04-23 06:52
浏览 39
已采纳

共享来自单独命令/流程的属性

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?

}
  • 写回答

2条回答 默认 最新

  • douwei3172 2018-04-28 05:19
    关注

    Share information between commands

    Like was said in comments, if you need to share data across commands, you'll need to persist it. The structure you use is no relevant, but for simplicity and because of JSON is the current most extended language for data exchange we'll use it.


    Where should I create it (path)?

    My recomendation is to use the home of the user. Many applications save its configuration here. This will allow a solution multi-environment easily. Say that your configuration file will be named myApp.

    func configPath() string {
        cfgFile := ".myApp"
        usr, _ := user.Current()
        return path.Join(usr.HomeDir, cfgFile)
    }
    


    When to clean it ?

    That obviously will depend on your requirements. But, if you always need to run pre and run in that order, I bet that you can clean it inmediately after run execution, when it won't be longer needed.


    How to store it ?

    That's easy. If what you need to save is your config struct you can do this:

    func saveConfig(c config) {
        jsonC, _ := json.Marshal(c)
        ioutil.WriteFile(configPath(), jsonC, os.ModeAppend)
    }
    


    And to read it ?

    func readConfig() config {
        data, _ := ioutil.ReadFile(configPath())
        var cfg config
        json.Unmarshal(data, &cfg)
        return cfg
    }
    


    Flow

    // pre command
    // persist to file the data you need
    saveConfig(config{ 
        id:   "id",
        path: "path",
        wd:   "wd",
    }) 
    

    // run command
    // retrieve the previously stored information
    cfg := readConfig()
    
    // from now you can use the data generated by `pre`
    

    DISCLAIMER: I've removed all error handling for short.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题