如何从cobra运行另一个命令



我正在GoLang中构建一个cli应用程序。我使用cobra来实现这一点,我有以下代码:

/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// pullCmd represents the pull command
var pullCmd = &cobra.Command{
Use:   "pull",
Short: "Take pull from repo",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("pull called")
},
}
func init() {
rootCmd.AddCommand(pullCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// pullCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// pullCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

每当我运行pull命令时,我希望pull命令在内部运行git pull命令。我该怎么做?我是GoLang&眼镜蛇图书馆。

感谢

os/exec包可以在这里为您提供帮助。为git pull创建一个command,然后创建Run

相关内容

  • 没有找到相关文章

最新更新