下面由golang教學專欄跟大家介紹一個golang常見函式庫cobra,希望對需要的朋友有幫助!
cobra 是 go 語言的一個函式庫,可以用來寫命令列工具。通常我們可以看到git pull
、docker container start
、apt install
等等這樣指令,都可以很容易用corba來實現,另外,go 語言是很容易編譯成一個二進位文件,本文將實作一個簡單的命令列工具。
具體寫一個範例, 設計一個指令叫blog
, 有四個子指令
blog new [post-name] :创建一篇新的blog blog list :列出当前有哪些文章 blog delete [post-name]: 删除某一篇文章 blog edit [post-name]:编辑某一篇文章
計畫有以下幾個步驟
$ go mod init github.com/shalk/blog go: creating new go.mod: module github.com/shalk/blog
說到命令列,可能會想到bash的getopt 或java 的jcommand,可以解析各種風格的命令列,但通常這些命令列都有固定的寫法,這個寫法一般還記不住要找一個模板參考以下。 cobra除了可以解析命令列之外,還提供了命令列,可以產生模板。先安裝這個指令列, 並且把函式庫的依賴加到go.mod裡
$ go get -u github.com/spf13/cobra/cobra
cobra會安裝到$GOPATH\bin
目錄下,注意環境變數中把它加入PATH中
$ cobra init --pkg-name github.com/shalk/blog -a shalk -l mit Your Cobra applicaton is ready at D:\code\github.com\shalk\blog
目錄結構如下:
./cmd ./cmd/root.go ./go.mod ./go.sum ./LICENSE ./main.go
編譯一下
go build -o blog .
執行一下
$blog -h 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.
命令列就創建好了,看上去一行程式碼也不用寫,由於隨著了解的深入,後面需要調整產生的程式碼,還是要了解cobra程式碼的套路。
有三個概念,command、flag和args ,例如:
go get -u test.com/a/b
這裡get 就是commond(這裡比較特殊), -u 就是flag, test.com/a/b 就是args
那麼命令列就是有三個部分構成,所以需要定義好這個
#還有一個概念是子指令,例如get就是go的子指令,這是一個樹狀結構的關係。
我可以使用go指令,也可以用go get指令
例如: root.go,定義了root指令,另外在init裡面定義了flag,如果它本身有具體執行,就填充Run字段。
// rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "blog", 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() { if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) } } func init() { cobra.OnInitialize(initConfig) // 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/.blog.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") }
如果需要子命令,就需要在init 裡,給rootCmd.AddCommand() 其他的command,其他的子命令通常也會單獨用一個檔案編寫,並且有一個全域變量,讓rootCmd可以add它
D:\code\github.com\shalk\blog>cobra add new new created at D:\code\github.com\shalk\blog D:\code\github.com\shalk\blog>cobra add delete delete created at D:\code\github.com\shalk\blog D:\code\github.com\shalk\blog>cobra add list list created at D:\code\github.com\shalk\blog D:\code\github.com\shalk\blog>cobra add edit edit created at D:\code\github.com\shalk\blog
cmd 目錄下增加了new.go, delete.go,list.go,edit.go
new.go
var newCmd = &cobra.Command{ Use: "new", Short: "create new post", Long: `create new post `, Args: func(cmd *cobra.Command, args []string) error { if len(args) != 1 { return errors.New("requires a color argument") } return nil }, Run: func(cmd *cobra.Command, args []string) { fileName := "posts/" + args[0] err := os.Mkdir("posts", 644) if err != nil { log.Fatal(err) } _, err = os.Stat( fileName) if os.IsNotExist(err) { file, err := os.Create(fileName) if err != nil { log.Fatal(err) } log.Printf("create file %s", fileName) defer file.Close() } else { } }, }
list.go
var listCmd = &cobra.Command{ Use: "list", Short: "list all blog in posts", Long: `list all blog in posts `, Run: func(cmd *cobra.Command, args []string) { _, err := os.Stat("posts") if os.IsNotExist(err) { log.Fatal("posts dir is not exits") } dirs, err := ioutil.ReadDir("posts") if err != nil { log.Fatal("read posts dir fail") } fmt.Println("------------------") for _, dir := range dirs { fmt.Printf(" %s\n", dir.Name() ) } fmt.Println("------------------") fmt.Printf("total: %d blog\n", len(dirs)) }, }
delete.go
var deleteCmd = &cobra.Command{ Use: "delete", Short: "delete a post", Long: `delete a post`, Args: func(cmd *cobra.Command, args []string) error { if len(args) != 1 { return errors.New("requires a color argument") } if strings.Contains(args[0],"/") || strings.Contains(args[0],"..") { return errors.New("posts name should not contain / or .. ") } return nil }, Run: func(cmd *cobra.Command, args []string) { fileName := "./posts/" + args[0] stat, err := os.Stat(fileName) if os.IsNotExist(err) { log.Fatalf("post %s is not exist", fileName) } if stat.IsDir() { log.Fatalf("%s is dir ,can not be deleted", fileName) } err = os.Remove(fileName) if err != nil { log.Fatalf("delete %s fail, err %v", fileName, err) } else { log.Printf("delete post %s success", fileName) } }, }
edit.go 這個有一點麻煩,因為如果呼叫一個程式例如vim 開啟文件,而golang程式本身要退出,需要detach。暫時放一下(TODO)
我是window測試,linux 更簡單一點
PS D:\code\github.com\shalk\blog> go build -o blog.exe . PS D:\code\github.com\shalk\blog> .\blog.exe list ------------------ ------------------ total: 0 blog PS D:\code\github.com\shalk\blog> .\blog.exe new blog1.md 2020/07/26 22:37:15 create file posts/blog1.md PS D:\code\github.com\shalk\blog> .\blog.exe new blog2.md 2020/07/26 22:37:18 create file posts/blog2.md PS D:\code\github.com\shalk\blog> .\blog.exe new blog3.md 2020/07/26 22:37:20 create file posts/blog3.md PS D:\code\github.com\shalk\blog> .\blog list ------------------ blog1.md blog2.md blog3.md ------------------ total: 3 blog PS D:\code\github.com\shalk\blog> .\blog delete blog1.md 2020/07/26 22:37:37 delete post ./posts/blog1.md success PS D:\code\github.com\shalk\blog> .\blog list ------------------ blog2.md blog3.md ------------------ total: 2 blog PS D:\code\github.com\shalk\blog> ls .\posts\ 目录: D:\code\github.com\shalk\blog\posts Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2020/7/26 22:37 0 blog2.md -a---- 2020/7/26 22:37 0 blog3.md PS D:\code\github.com\shalk\blog>
cobra 是一個高效率的命令列解析庫,利用cobra的鷹架,可以快速的實作一個命令列工具。如果需要更細緻的控制,可以參考cobra的官方文件。
以上是一個golang常見庫cobra的詳細內容。更多資訊請關注PHP中文網其他相關文章!