gocobrabuild命令行工具为统一编译前后端命令-《GO开发知识笔记》

admin 2025-11-04 00:57:53 编程 来源:ZONE.CI 全球网 0 阅读模式

    获取 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. }
    以太坊cppgolang区别 编程

    以太坊cppgolang区别

    以太坊是一种去中心化的开源平台,它采用智能合约技术,旨在构建和运行不受干扰的分布式应用程序。作为目前最受欢迎的区块链平台之一,以太坊提供了多种编程语言的支持,其
    progolang 编程

    progolang

    Go语言(Golang)是由Google开发的一门静态类型编程语言。作为一名专业的Golang开发者,我深知这门语言的优势和特点。在本文中,我将介绍Golang
    golangn个发送者 编程

    golangn个发送者

    Golang是一种开源的编程语言,由Google团队开发,旨在提高程序的并发性和简化软件开发过程。在Go语言中,有时需要向多个接收者发送信息。本文将介绍如何在G
    golang技能图谱 编程

    golang技能图谱

    从互联网行业的快速发展到人工智能技术的日益成熟,各种编程语言也应运而生。而在这众多的编程语言中,Golang(即Go)作为一门强大且高效的开发语言备受关注。Go
    评论:0   参与:  12