157 lines
3.6 KiB
Go
157 lines
3.6 KiB
Go
package cmd
|
||
|
||
import (
|
||
"fmt"
|
||
"github.com/spf13/cobra"
|
||
"github.com/spf13/viper"
|
||
"os"
|
||
)
|
||
|
||
const WddConfigFilePath = "/usr/local/etc/wdd/"
|
||
|
||
var rootCmd = &cobra.Command{
|
||
Use: "wdd",
|
||
Short: "wdd应用程序是wdd封装的NB工具",
|
||
Long: `使用golang的强大特性,加上wdd的高超技术,\n打造一个方便、高效、适用于各种LINUX系统的瑞士军刀工具\n 尽情享用!`,
|
||
Run: func(cmd *cobra.Command, args []string) {
|
||
// Do Stuff Here
|
||
},
|
||
}
|
||
var cfgFile = ""
|
||
var userLicense = ""
|
||
var projectBase = ""
|
||
|
||
func init() {
|
||
|
||
cobra.OnInitialize(initConfig)
|
||
|
||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
|
||
rootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "",
|
||
"base project directory eg. github.com/spf13/")
|
||
rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution")
|
||
rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "Name of license for the project (can provide `licensetext` in config)")
|
||
rootCmd.PersistentFlags().Bool("viper", true, "Use Viper for configuration")
|
||
viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
|
||
viper.BindPFlag("projectbase", rootCmd.PersistentFlags().Lookup("projectbase"))
|
||
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
|
||
viper.SetDefault("author", "Esdeath Wang(zeaslity@gamil.com)")
|
||
viper.SetDefault("license", "apache")
|
||
}
|
||
|
||
func initConfig() {
|
||
// Don't forget to read config either from cfgFile or from home directory!
|
||
if cfgFile != "" {
|
||
// Use config file from the flag.
|
||
viper.SetConfigFile(cfgFile)
|
||
} else {
|
||
|
||
// Search config in home directory with name ".cobra" (without extension).
|
||
viper.AddConfigPath(WddConfigFilePath)
|
||
viper.SetConfigName(".cobra")
|
||
}
|
||
|
||
if err := viper.ReadInConfig(); err != nil {
|
||
fmt.Println("Can't read config:", err)
|
||
os.Exit(1)
|
||
}
|
||
}
|
||
|
||
func Execute() {
|
||
|
||
// 1. base命令
|
||
baseCmd := &cobra.Command{
|
||
Use: "base",
|
||
Short: "服务器基础操作",
|
||
}
|
||
addBaseSubcommands(baseCmd)
|
||
|
||
// 2. zsh命令
|
||
zshCmd := &cobra.Command{
|
||
Use: "zsh",
|
||
Short: "zsh相关的内容",
|
||
}
|
||
addZshSubcommands(zshCmd)
|
||
|
||
// 3. proxy命令
|
||
proxyCmd := &cobra.Command{
|
||
Use: "proxy",
|
||
Short: "主机代理相关的内容",
|
||
}
|
||
addProxySubcommands(proxyCmd)
|
||
|
||
// 4. acme命令
|
||
acmeCmd := &cobra.Command{
|
||
Use: "acme",
|
||
Short: "acme相关的内容",
|
||
}
|
||
addAcmeSubcommands(acmeCmd)
|
||
|
||
// 5. wdd命令
|
||
wddCmd := &cobra.Command{
|
||
Use: "wdd",
|
||
Short: "WDD相关操作",
|
||
}
|
||
addWddSubcommands(wddCmd)
|
||
|
||
// 6. security命令
|
||
securityCmd := &cobra.Command{
|
||
Use: "security",
|
||
Short: "安全相关操作",
|
||
}
|
||
|
||
// 7. info命令
|
||
infoCmd := &cobra.Command{
|
||
Use: "info",
|
||
Short: "打印主机详细信息",
|
||
Run: func(cmd *cobra.Command, args []string) {
|
||
// 实现info逻辑
|
||
},
|
||
}
|
||
|
||
// 8. version命令
|
||
versionCmd := &cobra.Command{
|
||
Use: "version",
|
||
Short: "打印版本信息",
|
||
Run: func(cmd *cobra.Command, args []string) {
|
||
// 实现version逻辑
|
||
},
|
||
}
|
||
|
||
// 9. config命令
|
||
configCmd := &cobra.Command{
|
||
Use: "config",
|
||
Short: "配置文件管理",
|
||
}
|
||
addConfigSubcommands(configCmd)
|
||
|
||
// 10. download命令
|
||
downloadCmd := &cobra.Command{
|
||
Use: "download",
|
||
Short: "文件下载管理",
|
||
}
|
||
addDownloadSubcommands(downloadCmd)
|
||
|
||
// 添加所有根命令
|
||
rootCmd.AddCommand(
|
||
baseCmd,
|
||
zshCmd,
|
||
proxyCmd,
|
||
acmeCmd,
|
||
wddCmd,
|
||
securityCmd,
|
||
infoCmd,
|
||
versionCmd,
|
||
configCmd,
|
||
downloadCmd,
|
||
)
|
||
|
||
if err := rootCmd.Execute(); err != nil {
|
||
fmt.Println(err)
|
||
}
|
||
|
||
if err := rootCmd.Execute(); err != nil {
|
||
fmt.Println(err)
|
||
os.Exit(1)
|
||
}
|
||
}
|