|
| 1 | +# golang-cobra-cli-sample |
| 2 | + |
| 3 | +This repository is how to use cobra framework to write cli application |
| 4 | + |
| 5 | +## dependency |
| 6 | + |
| 7 | +1. cobra |
| 8 | + |
| 9 | +```shell |
| 10 | +go get github.com/spf13/cobra@latest |
| 11 | +``` |
| 12 | + |
| 13 | +## logic |
| 14 | + |
| 15 | +basic struct |
| 16 | + |
| 17 | +main contain command.Execute method |
| 18 | +```golang |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "log" |
| 23 | + |
| 24 | + "github.com/leetcode-golang-classroom/golang-cobra-cli-sample/internal/command" |
| 25 | +) |
| 26 | + |
| 27 | +func main() { |
| 28 | + err := command.Execute() |
| 29 | + if err != nil { |
| 30 | + log.Fatalf("command.Execute error: %v", err) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +``` |
| 35 | + |
| 36 | +root cmd for handle main Execute |
| 37 | +```golang |
| 38 | +import "github.com/spf13/cobra" |
| 39 | + |
| 40 | +var rootCmd = &cobra.Command{ |
| 41 | + Use: "securerandom", |
| 42 | + Short: "Secure random number generators", |
| 43 | + Long: "an interface to secure random number generators", |
| 44 | +} |
| 45 | + |
| 46 | +func Execute() error { |
| 47 | + return rootCmd.Execute() |
| 48 | +} |
| 49 | + |
| 50 | +``` |
| 51 | +hex.go for generate a random number with hex encoded |
| 52 | +```golang |
| 53 | +package command |
| 54 | + |
| 55 | +import ( |
| 56 | + "crypto/rand" |
| 57 | + "encoding/hex" |
| 58 | + "fmt" |
| 59 | + |
| 60 | + "github.com/spf13/cobra" |
| 61 | +) |
| 62 | + |
| 63 | +// hexCmd |
| 64 | +var hexCmd = &cobra.Command{ |
| 65 | + Use: "hex", |
| 66 | + Short: "Generates Hex numbers", |
| 67 | + Long: `Provided length, it generates hex numbers. |
| 68 | +For example: |
| 69 | +securerandom hex -l 10`, |
| 70 | + Run: func(cmd *cobra.Command, args []string) { |
| 71 | + length, _ := cmd.Flags().GetInt("length") |
| 72 | + hex, err := generateHexString(length) |
| 73 | + if err != nil { |
| 74 | + fmt.Println(err) |
| 75 | + } else { |
| 76 | + fmt.Println(hex) |
| 77 | + } |
| 78 | + }, |
| 79 | +} |
| 80 | +// init for add cmd, and flag setup |
| 81 | +func init() { |
| 82 | + rootCmd.AddCommand(hexCmd) |
| 83 | + hexCmd.Flags().IntP("length", "l", 4, "Length of hex") |
| 84 | +} |
| 85 | + |
| 86 | +func generateHexString(length int) (string, error) { |
| 87 | + // Calculate the nubmer of bytes needed |
| 88 | + byteLength := (length + 1) / 2 |
| 89 | + bytes := make([]byte, byteLength) |
| 90 | + // Generate random byte |
| 91 | + if _, err := rand.Read(bytes); err != nil { |
| 92 | + return "", err |
| 93 | + } |
| 94 | + // Conver to hex and truncate to the desired length |
| 95 | + hexString := hex.EncodeToString(bytes)[:length] |
| 96 | + return hexString, nil |
| 97 | +} |
| 98 | + |
| 99 | +``` |
| 100 | + |
| 101 | +base64.go generate random number with base64 encoded |
| 102 | +```golang |
| 103 | +package command |
| 104 | + |
| 105 | +import ( |
| 106 | + "crypto/rand" |
| 107 | + "encoding/base64" |
| 108 | + "fmt" |
| 109 | + |
| 110 | + "github.com/spf13/cobra" |
| 111 | +) |
| 112 | + |
| 113 | +var base64Cmd = &cobra.Command{ |
| 114 | + Use: "base64", |
| 115 | + Short: "Generate base64 string", |
| 116 | + Long: `Provided length, it generates base64 string. |
| 117 | +For example: |
| 118 | +securerandom base64 -l 10`, |
| 119 | + Run: func(cmd *cobra.Command, args []string) { |
| 120 | + length, _ := cmd.Flags().GetInt("length") |
| 121 | + str, err := generateBase64tring(length) |
| 122 | + if err != nil { |
| 123 | + fmt.Println(err) |
| 124 | + } else { |
| 125 | + fmt.Println(str) |
| 126 | + } |
| 127 | + }, |
| 128 | +} |
| 129 | + |
| 130 | +func generateBase64tring(byteLength int) (string, error) { |
| 131 | + // Generate random bytes |
| 132 | + bytes := make([]byte, byteLength) |
| 133 | + if _, err := rand.Read(bytes); err != nil { |
| 134 | + return "", err |
| 135 | + } |
| 136 | + // Encode the bytes ot a Base64 string |
| 137 | + base64String := base64.StdEncoding.EncodeToString(bytes) |
| 138 | + return base64String, nil |
| 139 | +} |
| 140 | + |
| 141 | +func init() { |
| 142 | + rootCmd.AddCommand(base64Cmd) |
| 143 | + base64Cmd.Flags().IntP("length", "l", 4, "Length of base64") |
| 144 | +} |
| 145 | + |
| 146 | +``` |
| 147 | + |
| 148 | +uuid.go for generate a uuid |
| 149 | +```golang |
| 150 | +package command |
| 151 | + |
| 152 | +import ( |
| 153 | + "fmt" |
| 154 | + |
| 155 | + "github.com/google/uuid" |
| 156 | + "github.com/spf13/cobra" |
| 157 | +) |
| 158 | + |
| 159 | +// uuidcmd represents the uuid command |
| 160 | +var uuidCmd = &cobra.Command{ |
| 161 | + Use: "uuid", |
| 162 | + Short: "Generates UUID", |
| 163 | + Long: `Generates UUID. For example: |
| 164 | +
|
| 165 | +securerandom uuid`, |
| 166 | + Run: func(cmd *cobra.Command, args []string) { |
| 167 | + newUUID := uuid.New() |
| 168 | + fmt.Println(newUUID.String()) |
| 169 | + }, |
| 170 | +} |
| 171 | + |
| 172 | +func init() { |
| 173 | + rootCmd.AddCommand(uuidCmd) |
| 174 | +} |
| 175 | + |
| 176 | +``` |
0 commit comments