关于go:Go-Cobra快速上手教程持续更新中

29次阅读

共计 2922 个字符,预计需要花费 8 分钟才能阅读完成。

环境变量设置

export GOBIN=$GOPATH/bin
export PATH=$GOBIN:$PATH
export GOPROXY=https://goproxy.cn/,direct

装置依赖

go get -u github.com/spf13/[email protected]
go get -u github.com/spf13/[email protected]

Cobra 我的项目流程

参考

Create rootCmd
Create your main.go
Create additional commands
Returning and handling errors

# 为了疾速实现我的项目 & 上班,倡议应用 cobra-cli 来实现以上步骤
# 上面的演示也是用 cobra-cli 来实现 

应用 Cobra CLI 初始化我的项目

参考

mkdir myapp && cd myapp && go mod init myapp
cobra-cli init

myapp/ $ tree                
.
├── cmd
│   └── root.go
├── go.mod
├── go.sum
├── LICENSE
└── main.go

myapp/ $ go run main.go         
A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.
// // cmd/root.go
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>

*/
package cmd

import (
    "os"

    "github.com/spf13/cobra"
)



// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
    Use:   "myapp",
    Short: "A brief description of your application",
    Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
    // Uncomment the following line if your bare application
    // has an action associated with it:
    // Run: func(cmd *cobra.Command, args []string) {},}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {err := rootCmd.Execute()
    if err != nil {os.Exit(1)
    }
}

func init() {
    // Here you will define your flags and configuration settings.
    // Cobra supports persistent flags, which, if defined here,
    // will be global for your application.

    // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "","config file (default is $HOME/.myapp.yaml)")

    // Cobra also supports local flags, which will only run
    // when this action is called directly.
    rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

我的项目增加 command

$ cobra-cli add config                                                                              [10:38:42]
config created at /home/project/myapp

$ ls cmd 
config.go  root.go
// cmd/config.go
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>

*/
package cmd

import (
    "fmt"

    "github.com/spf13/cobra"
)

// configCmd represents the config command
var configCmd = &cobra.Command{
    Use:   "config",
    Short: "A brief description of your command",
    Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
    Run: func(cmd *cobra.Command, args []string) {fmt.Println("config called")
    },
}

func init() {rootCmd.AddCommand(configCmd)

    // Here you will define your flags and configuration settings.

    // Cobra supports Persistent Flags which will work for this command
    // and all subcommands, e.g.:
    // configCmd.PersistentFlags().String("foo", "","A help for foo")

    // Cobra supports local flags which will only run when this command
    // is called directly, e.g.:
    // configCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

正文完
 0