共享来自单独 commands/process 的属性

Share properties from separate commands/process

我提供带有多个命令和子命令的命令行工具,我使用 cobra 命令行,我有两个 单独的命令 首先是 prerequisite to other

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

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

并且用户应该像

一样执行它

btr prepare
btr run

当执行 run command 时,它应该从 prepare 命令结果中获取一些数据

// 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)
    
}

更新

好吧,下面的答案确实有帮助。我需要在 local & cloud env) 中的两个命令之间共享状态,如果我 运行 来自 shell 脚本的命令行命令,我该怎么做调用 1 个命令,然后调用第二个命令,它需要从第一个命令获取一些状态,我需要 E2E 解决方案,在我的上下文中使用代码示例

更新 2

假设我知道我需要配置文件 (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 ) ?

假设我的配置如下

type config struct{
    
    path string,
    wd string,
    id string,//guid?
    
}

如果您尝试在执行命令行工具的不同命令时保持状态,您有 2 个选择。

  1. 将状态写入某种文件(在这种情况下将 env 视为一个文件)。
  2. 第一个命令的输出值可以用作另一个命令的输入。

1.

我认为任何参数等都只在 CLI 工具的 运行ning 生命周期内存在是一种很好的做法。相当于编程中可能的最小作用域变量。当您需要无限期地保留状态时,这很有效,但如果在 initProcess 命令完成后不再使用您的状态,那么这可能不是正确的选择。

2.

这有一些优先权。 unix 哲学 (wikipedia) 建议:

Expect the output of every program to become the input to another

所以你可以为第一个 prepare 命令选择一些输出(到标准输出)格式,可以用作第二个 initProcess 命令的输入。然后使用 | 管道将 运行 一个输出到另一个。

示例:

func Run(cmd *cobra.Command, args []string) {
    //This creating the temp folder and validate some configuration file    
    tmpFolderPath,parsedFile := exe.PreProcess()
    fmt.Println(tmpFolderPath)
    fmt.Println(parsedFile)
}

func InitProcess(cmd *cobra.Command, args []string) {

    tmpFolder, parsedFile := args[0], args[1]

}

然后到 运行 程序,并将命令组合在一起:

./program pre | xargs ./program run

在命令之间共享信息

就像评论中所说的那样,如果您需要跨命令共享数据,则需要保留它。您使用的结构无关紧要,但为了简单起见,并且由于 JSON 是当前最广泛的数据交换语言,我们将使用它。


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

我的建议是使用用户的家。许多应用程序将其配置保存在这里。这将允许一个解决方案轻松实现多环境。假设您的配置文件将被命名为 myApp.

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


什么时候清理它?

这显然取决于您的要求。但是,如果你总是需要 运行 prerun 的顺序,我打赌你可以在 run 执行后立即清理它,当它不会更长时需要。


如何存储?

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

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


并阅读它?

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


流量

// 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.