从单独的命令/进程共享属性 [英] Share properties from separate commands/process

查看:191
本文介绍了从单独的命令/进程共享属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用眼镜蛇命令行和我使用几个命令和子命令提供命令行工具'two two 独立命令
首先是 prerequisit e至其他



例如第一个命令是通过创建临时文件夹并验证某个文件来优先选择环境



第二个命令应该从第一个命令获得一些属性

,用户应该像执行它一样


准备

btr运行


执行 run命令时,它应该从准备命令结果

  // rootCmd表示在没有任何子命令的情况下调用base命令
var rootCmd =& cobra.Command {
使用:btr,
Short:管道处理,
}


var prepare = & cobra.Command {
使用:pre,
Short:准备环境,
运行:func(cmd * cobra.Command,args [] string){

//创建临时文件夹并验证一些配置文件
tmpFolderPath,parsedFile:= exe.PreProcess ()
},
}


var initProcess =& cobra.Command {
使用:run,
Short: run process,
运行:func(cmd * cobra.Command,args [] string){

//这里我需要这两个属性

run (tmpFolderPath,ParsedFile)
},
}

func init(){

rootCmd.AddCommand(prepare,initProcess)


更新

好吧,下面的答案确实有帮助。我需要在 local&云 env),我如何做到这一点,如果我从shell脚本运行命令行命令,调用1命令,然后调用第二个需要从第一个命令获得某种状态的命令,我需要E2E 解决方案代码在我的上下文中的实例



update 2



理解我需要配置文件(json),


我应该在哪里创建它(路径)?

何时清洁它?



在使用1file的情况下我应该如何验证以存储与特定过程相关的数据,并在需要时使用其他过程数据(guid)? $ b

可以说我配置如下

  type config struct {

路径字符串,
wd字符串,
id字符串,// guid?

$ b $ <$ code>


解决方案

在命令之间



Like在评论中说过,如果你需要跨命令共享数据,你需要坚持下去。您使用的结构并不相关,但为了简单起见,由于JSON是当前最为广泛的数据交换语言,因此我们将使用它。






我应该在哪里创建它(路径)?



我的建议是使用用户的家。许多应用程序在此保存其配置。这将允许轻松解决多环境问题。假设您的配置文件将被命名为myApp 。

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




什么时候清理它?



这显然取决于您的要求。但是,如果你总是需要按顺序运行 pre run ,我敢打赌你可以立即清理它 run 执行,当它不再需要时。




如何存储它?



这很简单。如果你需要保存的是你的 config 结构,你可以这样做:

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




并阅读它? / h3>

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




流量



  // pre命令
//持续存档你需要的数据
saveConfig(config {
id:id,
path:path,
wd :wd,
))

  //运行命令
//检索之前存储的信息
cfg:= readConfig()

//从现在您可以使用`pre`


$ b生成的数据$ b


免责声明:我删除了所有错误处理。


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?

}

解决方案

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.

这篇关于从单独的命令/进程共享属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆