go 使用cobra构建cli

Last updated on November 22, 2024 pm

🧙 Questions

使用cobra可以快速构建跨平台命令

☄️ Ideas

安装cobra命令
go install github.com/spf13/cobra-cli@latest

创建项目

# 初始化自己的项目
go mod init github.com/ispong/ispong-cli

# 初始化cobra项目
cobra-cli init

初始化命令

# 初始化一个命令
# cobra-cli add xxxx
cobra-cli add serve

# 初始化一个config命令
cobra-cli add config
# 二阶命令,依赖于上一个config命令,configCmd
cobra-cli add create -p 'configCmd'

启动项目

go run main.go serve

创建登录命令

用户输入一个值,存到配置文件中

# 使用viper管理配置文件
go get github.com/spf13/viper
go get github.com/mitchellh/go-homedir
cobra-cli add login
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"fmt"
	"github.com/mitchellh/go-homedir"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"os"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
	Use:   "ispong-cli",
	Short: "ispong个人工具集",
	Long:  `ispong个人工具集`,
	// 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)
	}
}

var (
	cfgFile string
)

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/.i-cli/i-config.yml)")

	// 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")
}

func initConfig() {

	// 获取home目录
	home, err := homedir.Dir()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// 初始化配置文件信息
	viper.SetConfigFile(home + "/.i-cli/i-config.yml")

	// 判断配置文件是否存在
	if err := viper.ReadInConfig(); err != nil {

		// 判断文件夹是否存在,不存在则新建
		_, err := os.Stat(home + "/.i-cli")
		if os.IsNotExist(err) {
			err := os.Mkdir(home+"/.i-cli", 0755)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
		}

		// 判断文件是否存在,不存在则新建
		_, err = os.Stat(home + "/.i-cli/i-config.yml")
		if os.IsNotExist(err) {
			// 初始化配置
			viper.SetDefault("account", "ispong")
			// 持久化配置
			err = viper.SafeWriteConfigAs(home + "/.i-cli/i-config.yml")
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
		}
	}
}

打包

# mac
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o i main.go

# windows
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o i.exe main.go

# linux
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o i main.go

安装命令

sudo cp /Users/ispong/isxcode/ispong-cli/i /usr/local/bin

go 使用cobra构建cli
https://ispong.isxcode.com/kubernetes/go/go 使用cobra构建cli/
Author
ispong
Posted on
December 7, 2023
Licensed under