go - 从单独的命令/进程共享属性

标签 go command-line command-line-arguments viper-go go-cobra

我提供带有多个命令和子命令的命令行工具,我使用 cobra命令行,我有两个单独的命令 这首先是先决条件其他

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

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

用户应该像这样执行它

btr prepare
btr run

当执行 run 命令 时,它应该从 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) 中的两个命令之间共享状态,如果我从调用 1 个命令然后调用第二个命令的 shell 脚本运行命令行命令,我该如何做到这一点需要从第一个命令获取一些状态,我需要 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?
    
}

最佳答案

在命令之间共享信息

就像评论中所说的那样,如果您需要跨命令共享数据,则需要保留它。您使用的结构无关紧要,但为了简单起见,并且由于 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.

关于go - 从单独的命令/进程共享属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49974998/

相关文章:

r - 从命令行运行 R 文件时出错

php - WP-CLI.phar "could not open input file"

node.js - 在 Visual Studio Code 中,如何在 launch.json 中传递参数

sql-server - sql statement.exec错误:mssql:'?'附近的语法不正确

networking - net/http GET 请求错误 tls 收到长度为 20527 的超大记录

java - 如何在没有jar文件的情况下在命令行中编译和运行Netbeans java项目?

使用参数运行另一个脚本

python - Bash 将字符串参数传递给 python 脚本

go - 将 Jenkins Freestyle Golang 作业转换为 Jenkinsfile

go - 防止 go build 覆盖 go.mod 中的版本