\u90a3\u4e48\u5982\u4f55\u5b9e\u73b0\u5462\uff1f<\/strong><\/div>\n\u5728\u8fd9\u8fb9\uff0c\u6211\u4eec\u7528\u4e86\u4e00\u4e2a\u53ebcobra\u7684\u6846\u67b6\uff0c\u8fd9\u4e2a\u6846\u67b6\u88ab\u5e7f\u6cdb\u8fd0\u7528\u5230\u5f88\u591a\u5f00\u6e90\u7684\u4ea7\u54c1\u5f53\u4e2d\uff0c\u6bd4\u5982docker-compose, kubectl\u7b49\u3002<\/p>\n
\u9996\u5148\uff0c\u6211\u4eec\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u73af\u5883\uff1a<\/p>\n
go get -u github.com\/spf13\/cobra@latest\r\ngo install github.com\/spf13\/cobra-cli@latest<\/pre>\n\u5728\u6267\u884c\u5b8c\u4e0a\u9762\u4e24\u6761\u547d\u4ee4\u540e\u6211\u4eec\u5c31\u5177\u5907\u6700\u57fa\u672c\u7684\u5f00\u53d1\u6761\u4ef6\u4e86\uff0c\u63a5\u4e0b\u6765\u5f00\u59cb\u6211\u4eec\u7684\u5f00\u53d1\u5427\uff01<\/p>\n
\u4f7f\u7528Cobra\u521d\u59cb\u5316\u6211\u4eec\u7684\u9879\u76ee<\/p>\n
cobra-cli init<\/pre>\n\u6267\u884c\u5b8c\u4e4b\u540e\uff0c\u6211\u4eec\u4f1a\u5728\u672c\u5730\u76ee\u5f55\u770b\u5230\u8fd9\u6837\u7684\u7ed3\u6784<\/p>\n
\u251c\u2500\u2500 main.go\r\n\u251c\u2500\u2500 cmd\r\n\u2502 \u2514\u2500\u2500 root.go<\/pre>\nmain.go\u5c31\u662f\u6211\u4eec\u7684\u4e3b\u5165\u53e3\u4e86\uff0croot\u662f\u6211\u4eec\u547d\u4ee4\u7684\u6839\u547d\u4ee4<\/p>\n
main.go<\/p>\n
\/\/ \u53ea\u662f\u505a\u4e86\u4e00\u4e2a\u6267\u884c\u7684\u64cd\u4f5c\r\nfunc main() {\r\n cmd.Execute()\r\n}<\/pre>\nRoot.go \u5b9a\u4e49\u4e86\u6839\u547d\u4ee4\uff0c\u8fd8\u6709\u4e00\u4e9b\u521d\u59cb\u5316\u7684\u64cd\u4f5c<\/p>\n
var rootCmd = &cobra.Command{\r\n Use: \"gtools\", \/\/ \u8fd9\u662f\u4f60\u7684\u547d\u4ee4\u7684\u540d\u5b57\r\n Short: \"A brief description of your application\",\r\n Long: `A longer description that spans multiple lines and likely contains\r\nexamples and usage of using your application. For example:\r\nCobra is a CLI library for Go that empowers applications.\r\nThis application is a tool to generate the needed files\r\nto quickly create a Cobra application.`,\r\n \/\/ Uncomment the following line if your bare application\r\n \/\/ has an action associated with it:\r\n \/\/ Run: func(cmd *cobra.Command, args []string) { },\r\n}\r\n\/\/ Execute adds all child commands to the root command and sets flags appropriately.\r\n\/\/ This is called by main.main(). It only needs to happen once to the rootCmd.\r\nfunc Execute() {\r\n err := rootCmd.Execute()\r\n if err != nil {\r\n os.Exit(1)\r\n }\r\n}\r\nfunc init() {\r\n \/\/ Here you will define your flags and configuration settings.\r\n \/\/ Cobra supports persistent flags, which, if defined here,\r\n \/\/ will be global for your application.\r\n \/\/ rootCmd.PersistentFlags().StringVar(&cfgFile, \"config\", \"\", \"config file (default is $HOME\/.main.yaml)\")\r\n \/\/ Cobra also supports local flags, which will only run\r\n \/\/ when this action is called directly.\r\n rootCmd.Flags().BoolP(\"toggle\", \"t\", false, \"Help message for toggle\")\r\n}<\/pre>\n\u52a0\u5165\u6211\u4eec\u7684\u5b50\u547d\u4ee4<\/p>\n
\u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u52a0\u5165\u4e00\u4e2a\u5b50\u547d\u4ee4\uff0c\u5982autoSelector, \u53ea\u9700\u6267\u884c\u4e00\u4e0b\u547d\u4ee4\u5373\u53ef\uff1a<\/p>\n
cobra-cli add autoSelector<\/pre>\n\u5bf9\u5e94\u7684\u4e00\u4e2a\u53ebautoSelector.go\u7684\u6587\u4ef6\u5c31\u4f1a\u51fa\u73b0\u5728cmd\u76ee\u5f55\u5e95\u4e0b\uff0c\u5e76\u4e14\u5df2\u7ecf\u4e3a\u4f60\u51c6\u5907\u4e86\u57fa\u672c\u7684\u547d\u4ee4\u884c\u6846\u67b6<\/p>\n
\/\/ autoSelectorCmd represents the autoSelector command\r\nvar autoSelectorCmd = &cobra.Command{\r\n Use: \"autoSelector\", \/\/ \u540d\u5b57\r\n Aliases: []string{\"as\"}, \/\/ \u547d\u4ee4\u884c\u7684\u7b80\u5199\r\n Short: \"randomly select string from a list\", \/\/\u7b80\u5355\u7684\u63cf\u8ff0\r\n Long: `randomly select string from a list`, \/\/\u8be6\u7ec6\u63cf\u8ff0\r\n Run: func(cmd *cobra.Command, args []string) {\r\n \/\/ \u5728\u8fd9\u91cc\u52a0\u5165\/\u8c03\u7528\u4f60\u7684\u4e3b\u8981\u903b\u8f91\r\n }\r\n}\r\nfunc init() {\r\n \/\/ \u6ce8\u518c\u5230\u6839\u547d\u4ee4\u4e0b\r\n rootCmd.AddCommand(autoSelectorCmd)\r\n \/\/ Here you will define your flags and configuration settings.\r\n \/\/ Cobra supports Persistent Flags which will work for this command\r\n \/\/ and all subcommands, e.g.:\r\n \/\/ autoSelectorCmd.PersistentFlags().String(\"foo\", \"\", \"A help for foo\")\r\n \/\/ Cobra supports local flags which will only run when this command\r\n \/\/ is called directly, e.g.:\r\n \/\/ autoSelectorCmd.Flags().BoolP(\"toggle\", \"t\", false, \"Help message for toggle\")\r\n}<\/pre>\n\u5b9e\u73b0\u6211\u4eec\u7684\u529f\u80fd<\/p>\n
\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2apkg\u5305\u6765\u5b58\u653e\u6211\u4eec\u7684\u5177\u4f53\u5b9e\u73b0\u903b\u8f91\uff0c\u5728cmd\u4e2d\u53ea\u9700\u8981\u505a\u7b80\u5355\u7684\u8c03\u7528\u5373\u53ef<\/p>\n
import (\r\n \"math\/rand\"\r\n \"time\"\r\n)\r\n\/\/ \u7b80\u5355\u5b9e\u73b0\u903b\u8f91\r\nfunc AutoSelect(inputs []string) (selected string, err error) {\r\n source := rand.NewSource(time.Now().UnixNano())\r\n r := rand.New(source)\r\n randomIndex := r.Intn(len(inputs))\r\n selected = inputs[randomIndex]\r\n return selected, nil\r\n}<\/pre>\n\u6b64\u65f6\u6211\u4eec\u7684\u4ee3\u7801\u5de5\u5177\u5c31\u57fa\u672c\u5b9e\u73b0\u5b8c\u6210\u4e86\uff0c\u53ea\u9700\u8981\u7f16\u8bd1\u4e00\u4e0b\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002\u7f16\u8bd1\u8fd0\u884c<\/p>\n
go build -o gtools<\/pre>\n\u4f60\u5c31\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u53ebgtools\u7684\u4e8c\u8fdb\u5236\u5305\uff0c\u76f4\u63a5\u8fd0\u884c\u5c31\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u5f00\u5934\u7684\u6548\u679c\u5566~<\/p>\n
\u4ee3\u7801\u4ed3\u5e93\uff1a github.com\/819110812\/G\u2026<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5728\u73b0\u5b9e\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5927\u5bb6\u4f1a\u53d1\u73b0\u5f88\u591a\u5f00\u6e90\u7684\u6846\u67b6\u90fd\u4f1a\u6709\u7740\u81ea\u5df1\u7684\u4e00\u4e2aCLI\u5de5\u5177\u5e93\u6765\u5e2e\u52a9\u5f00\u53d1\u8005\u4eec\u901a\u8fc7\u547d\u4ee4\u884c\u7684\u65b9\u5f0f\u5feb\u901f\u7684 […]<\/p>\n","protected":false},"author":668,"featured_media":230488,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-262888","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/262888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=262888"}],"version-history":[{"count":2,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/262888\/revisions"}],"predecessor-version":[{"id":262890,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/262888\/revisions\/262890"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/230488"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=262888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=262888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=262888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}