<menu id="guoca"></menu>
<nav id="guoca"></nav><xmp id="guoca">
  • <xmp id="guoca">
  • <nav id="guoca"><code id="guoca"></code></nav>
  • <nav id="guoca"><code id="guoca"></code></nav>

    構建Go命令行程序工具鏈

    VSole2022-08-01 10:11:53

    話說不久前,需要輸出一個按發布時間排序的酷Go推薦的歷史文章列表,類似于這樣:

    于是乎,想著從GoCN上一篇一篇copy文章的標題吧,5篇下來,手眼已經不協調了,此時此刻才想起自己貌似是個碼農,此情此景那必須coding一段,讓代碼來輸出這個列表。

    思路很簡單,看一下GoCN文章列表的API,再看下鑒權方式(看header是通過cookie),然后就可以coding了,通過API拉取文章數據,提取需要的字段再組裝成MD輸出就完事了!

    當準備一個main.go搞定的時候,本著對coding的敬畏,簡單的思考了一下程序的可讀性、拓展性,決定還是構建一個比較完整的命令行程序,如下:

    主角登場

    那么要實現上圖的命令行提示,程序參數解析等,不借助于第三方包當然也能實現。但,前人種好的大樹為哈不乘涼呢,在業界對于命令行工具的開發已經有比較成熟的實現,下面清楚本文的主角:

    • cobra:應用命令行框架
    • pflag:命令行參數解析
    • viper:配置文件解析

    下面結合實際項目來看如何使用這三位大哥來構建Go命令行程序。

    先簡單看下項目結構:

    .
    ├── cmd
    │   ├── pull.go
    │   └── root.go
    ├── config.yaml
    ├── file
    │   └── cool-go.md
    ├── go.mod
    ├── go.sum
    ├── gocn-cli
    └── main.go
    

    然后引入上面所推薦的三個庫:

    go get -u github.com/spf13/cobra@latest
    go get -u github.com/spf13/viper
    go get -u github.com/spf13/pflag
    

    Coding

    package cmd
    import (
        "fmt"
        "github.com/spf13/cobra"
        "github.com/spf13/viper"
        "os"
    )
    var (
        cfgFile string
    )
    func init() {
        cobra.OnInitialize(initConfig)
        
        // 解析配置文件參數
        rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./config.yaml)")
    }
    // initConfig viper加載配置文件
    func initConfig() {
        if cfgFile != "" {
            viper.SetConfigFile(cfgFile)
        } else {
            viper.AddConfigPath(".")
            viper.SetConfigName("config")
        }
        
        if err := viper.ReadInConfig(); err != nil {
            fmt.Println("Can't read config file:", err)
            os.Exit(1)
        }
    }
    // 創建全局rootCmd
    var rootCmd = &cobra.Command{
        Use:   "gocn-cli",
        Short: "gocn-cli is a command line tool for gocn.com",
        Long:  "gocn-cli is a command line tool for gocn.com",
        Run: func(cmd *cobra.Command, args []string) {
            
        },
    }
    // Execute 執行命令 main調用
    func Execute() {
        if err := rootCmd.Execute(); err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
    }
    package cmd
    import (
     "github.com/spf13/cobra"
     "github.com/spf13/viper"
     "os"
    )
    var (
     moduleName string
    )
    func init() {
        // 給rootCmd增加新的命令pullCmd
     rootCmd.AddCommand(pullCmd)
     pullCmd.Flags().StringVarP(&moduleName, "module", "m", "", "module name")
    }
    var pullCmd = &cobra.Command{
     Use:   "pull gocn data",
     Short: "pull gocn data",
     Run: func(cmd *cobra.Command, args []string) {
      pullHandler()
     },
    }
    // pullHandler pullCmd 對應的處理函數
    func pullHandler() {
     if moduleName == "" {
      fmt.Println("module name is required")
      os.Exit(1)
     }
     switch moduleName {
     case "cool-go":
      pullCoolGo()
     default:
      fmt.Println("module name is invalid")
      os.Exit(1)
     }
    }
    func pullCoolGo() {
     // TODO Write Your Code
    }
    

    結語

    以上簡單介紹了如何使用cobra、viper、pflag,當然它們的用法還有很多,這里只做個簡單的入門。

    可見,通過簡單的代碼就能構建出比較好的命令行程序。

    命令行
    本作品采用《CC 協議》,轉載必須注明作者和本文鏈接
    因此當不方便進行直接傳輸時,同時目標主機是能有網絡連接的,那么此時就可以通過本地下載這種方法來達到文件傳輸的目的。此篇文章,我會對互聯網上針對win與linux大部分的下載指令來做一個匯總,可能會有疏漏,但匯總的都是經常會用的。
    命令行版 HTTP 工具集,自動化檢查網站狀態。
    0x01 前言滲透測試中無論是打點,還是后滲透,多多少少都會用到命令行下的一些技巧以達到快速拿到目標主機權限和繞過一些安全防護功能等。
    話說不久前,需要輸出一個按發布時間排序的酷Go推薦的歷史文章列表,類似于這樣:于是乎,想著從GoCN上一篇一篇copy文章的標題吧,5篇下來,手眼已經不協調了,此時此刻才想起自己貌似是個碼農,此情此景那必須coding一段,讓代碼來輸出這個列表。
    Links2Links2自稱是古老鏈接的圖形版本。就像 Links2 一樣,ELinks 也可以顯示表格和框架,支持使用鼠標跟蹤超鏈接,但它缺乏對Javascript的支持。
    的,可以在 mac、linux、windows 上使用。快捷鍵打開快速命令窗口,方便操作。zsh-syntax-highlighting地址:https://github.com/zsh-users/zsh-syntax-highlighting一句話點評:語法高亮為終端增添色彩。
    VSole
    網絡安全專家
      亚洲 欧美 自拍 唯美 另类