60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var (
|
|
cfgFile string
|
|
verbose bool
|
|
)
|
|
|
|
var rootCmd = &cobra.Command{
|
|
Use: "cdglab",
|
|
Short: "cdglab is a tool for gitlab CI-CD.",
|
|
Long: `cdglab is a CLI tool for GitLab CI/CD, used to simplify RESTful API calls.`,
|
|
}
|
|
|
|
func Execute() {
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cdglab.yaml)")
|
|
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
|
|
|
|
// 注册子命令
|
|
rootCmd.AddCommand(rawCmd)
|
|
rootCmd.AddCommand(versionCmd)
|
|
}
|
|
|
|
func initConfig() {
|
|
if cfgFile != "" {
|
|
viper.SetConfigFile(cfgFile)
|
|
} else {
|
|
home, err := os.UserHomeDir()
|
|
if err == nil {
|
|
viper.AddConfigPath(home)
|
|
}
|
|
viper.AddConfigPath(".")
|
|
viper.SetConfigType("yaml")
|
|
viper.SetConfigName(".cdglab")
|
|
}
|
|
|
|
// 环境变量支持
|
|
viper.SetEnvPrefix("CDGLAB")
|
|
viper.AutomaticEnv()
|
|
|
|
// 读取配置文件(忽略不存在的错误)
|
|
_ = viper.ReadInConfig()
|
|
}
|