/
config.go
69 lines (59 loc) · 1.72 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
File: config.go
Author: YJ
Email: yj1516268@outlook.com
Created Time: 2023-04-23 15:09:18
Description: 执行子命令 'config'
*/
package cmd
import (
"github.com/gookit/color"
"github.com/spf13/cobra"
"github.com/yhyj/eniac/cli"
"github.com/yhyj/eniac/general"
)
// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
Short: "Operate configuration file",
Long: `Manipulate the program's configuration files, including generating and printing.`,
Run: func(cmd *cobra.Command, args []string) {
// 获取配置文件路径
cfgFile, _ := cmd.Flags().GetString("config")
// 解析参数
createFlag, _ := cmd.Flags().GetBool("create")
forceFlag, _ := cmd.Flags().GetBool("force")
printFlag, _ := cmd.Flags().GetBool("print")
var (
noticeSlogan []string // 提示标语
)
// 检查参数
if !createFlag && !printFlag {
cmd.Help()
noticeSlogan = append(noticeSlogan, "Please refer to the above help information")
createFlag, printFlag = false, false
}
// 创建配置文件流程
if createFlag {
cli.CreateConfigFile(cfgFile, forceFlag)
}
// 打印配置文件流程
if printFlag {
cli.PrintConfigFile(cfgFile)
}
// 输出标语
if len(noticeSlogan) > 0 {
color.Println()
for _, slogan := range noticeSlogan {
color.Notice.Tips(general.PrimaryText(slogan))
}
}
},
}
func init() {
configCmd.Flags().BoolP("create", "", false, "Create a default configuration file")
configCmd.Flags().BoolP("force", "", false, "Overwrite existing configuration files")
configCmd.Flags().BoolP("print", "", false, "Print configuration file content")
configCmd.Flags().BoolP("help", "h", false, "help for config command")
rootCmd.AddCommand(configCmd)
}