获取 npm 命令,我们使用 Golang 标准库自带的 exec.LookPath 来查找。如果查找到了,就接着使用 exec.Command 来运行 npm run build,并且将输出 cmd.CombinedOutput,输出到控制台中;如果查找不到,就打印出错误信息。

    1. package main
    2. import "gobrief/cmd"
    3. func main() {
    4. cmd.Execute()
    5. }
    1. package cmd
    2. import (
    3. "fmt"
    4. "github.com/spf13/cobra"
    5. "os"
    6. )
    7. // rootCmd represents the base command when called without any subcommands
    8. var rootCmd = &cobra.Command{
    9. Use: "go-brief",
    10. Short: "go-brief [command]",
    11. Long: "Please go-brief run",
    12. // Uncomment the following line if your bare application
    13. // has an action associated with it:
    14. // Run: func(cmd *cobra.Command, args []string) { },
    15. Run: func(cmd *cobra.Command, args []string) {
    16. fmt.Println("this is test run function")
    17. if len(args) == 0 {
    18. _ = cmd.Help()
    19. return
    20. }
    21. },
    22. }
    23. // Execute adds all child commands to the root command and sets flags appropriately.
    24. // This is called by main.main(). It only needs to happen once to the rootCmd.
    25. func Execute() {
    26. if err := rootCmd.Execute(); err != nil {
    27. fmt.Println(err)
    28. os.Exit(1)
    29. }
    30. }
    1. // 打印前端的命令
    2. var buildFrontendCommand = &cobra.Command{
    3. Use: "frontend",
    4. Short: "使用npm编译前端",
    5. RunE: func(c *cobra.Command, args []string) error {
    6. // 获取path路径下的npm命令
    7. path, err := exec.LookPath("npm")
    8. if err != nil {
    9. log.Fatalln("请安装npm在你的PATH路径下")
    10. }
    11. // 执行npm run build
    12. cmd := exec.Command(path, "run", "build")
    13. // 将输出保存在out中
    14. out, err := cmd.CombinedOutput()
    15. if err != nil {
    16. fmt.Println("============= 前端编译失败 ============")
    17. fmt.Println(string(out))
    18. fmt.Println("============= 前端编译失败 ============")
    19. return err
    20. }
    21. // 打印输出
    22. fmt.Print(string(out))
    23. fmt.Println("============= 前端编译成功 ============")
    24. return nil
    25. },
    26. }
    1. package console
    2. import (
    3. "github.com/gohade/hade/app/console/command/demo"
    4. "github.com/gohade/hade/framework"
    5. "github.com/gohade/hade/framework/cobra"
    6. "github.com/gohade/hade/framework/command"
    7. )
    8. // RunCommand 初始化根Command并运行
    9. func RunCommand(container framework.Container) error {
    10. // 根Command
    11. var rootCmd = &cobra.Command{
    12. // 定义根命令的关键字
    13. Use: "hade",
    14. // 简短介绍
    15. Short: "hade 命令",
    16. // 根命令的详细介绍
    17. Long: "hade 框架提供的命令行工具,使用这个命令行工具能很方便执行框架自带命令,也能很方便编写业务命令",
    18. // 根命令的执行函数
    19. RunE: func(cmd *cobra.Command, args []string) error {
    20. cmd.InitDefaultHelpFlag()
    21. return cmd.Help()
    22. },
    23. // 不需要出现cobra默认的completion子命令
    24. CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
    25. }
    26. // 为根Command设置服务容器
    27. rootCmd.SetContainer(container)
    28. // 绑定框架的命令
    29. command.AddKernelCommands(rootCmd)
    30. // 绑定业务的命令
    31. AddAppCommand(rootCmd)
    32. // 执行RootCommand
    33. return rootCmd.Execute()
    34. }
    35. // 绑定业务的命令
    36. func AddAppCommand(rootCmd *cobra.Command) {
    37. rootCmd.AddCommand(demo.FooCommand)
    38. // 每秒调用一次Foo命令
    39. //rootCmd.AddCronCommand("* * * * * *", demo.FooCommand)
    40. // 启动一个分布式任务调度,调度的服务名称为init_func_for_test,每个节点每5s调用一次Foo命令,抢占到了调度任务的节点将抢占锁持续挂载2s才释放
    41. //rootCmd.AddDistributedCronCommand("foo_func_for_test", "*/5 * * * * *", demo.FooCommand, 2*time.Second)
    42. }