代码之家  ›  专栏  ›  技术社区  ›  user6124024

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

  •  4
  • user6124024  · 技术社区  · 6 年前

    Im提供带有多个命令和子命令的命令行工具,Im使用 cobra 命令行和Ive 2 单独的命令 第一个是 先决条件 e至其他

    e、 g.第一个命令是通过创建临时文件夹和验证一些文件来优化环境

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

    用户应该像这样执行它

    btr准备
    btr运行

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

    使现代化

    好吧,下面的答案真的很有帮助。我需要在中的两个命令之间共享状态 本地(&A);云 env),如果我从shell脚本运行命令行命令,调用1个命令,然后调用需要从第一个命令获取某些状态的第二个命令,那么我需要E2E 在我的上下文中使用代码实例的解决方案

    更新2

    假设我知道我需要配置文件(json),

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

    什么时候清洗?

    如果我使用1文件,我应该如何验证以存储与特定流程相关的数据,并在需要时获取其他流程数据(guid)?

    假设我有如下配置

    type config struct{
        
        path string,
        wd string,
        id string,//guid?
        
    }
    
    2 回复  |  直到 4 年前
        1
  •  2
  •   Ivan Beldad    6 年前

    在命令之间共享信息

    正如评论中所说,如果需要跨命令共享数据,则需要将其持久化。您使用的结构与此无关,但为了简单起见,并且由于JSON是当前数据交换中扩展最广泛的语言,我们将使用它。


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

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

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


    什么时候清洗?

    这显然取决于您的要求。但是,如果你总是需要跑步 pre 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`
    

    免责声明 :简而言之,我已删除所有错误处理。

        2
  •  2
  •   Zak    6 年前

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

    1. 将状态写入某种文件(在本例中考虑env a文件)。
    2. 以可以用作另一个命令输入的方式从第一个命令输出值。

    在1上。

    我认为,对于任何参数等,最好只在CLI工具运行的生命周期内存在。编程中可能的最小范围变量的等价物。当您需要无限期地保持状态时,这种方法效果很好,但如果在 initProcess 命令已完成,那么这可能不是正确的选项。

    在2上。

    这有一定的优先权。unix理念( wikipedia )建议:

    期望每个程序的输出成为另一个程序的输入

    因此,您可以为第一个选择一些输出(到标准输出)格式 prepare 可以用作第二个 初始化进程 命令然后使用 | 管道将一个输出连接到另一个输出。

    示例:

    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