/
config.go
59 lines (51 loc) · 1.7 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
package cmd
import (
"fmt"
"github.com/NICEXAI/lazy-go/internal/config"
"github.com/NICEXAI/lazy-go/internal/errorx"
"github.com/fatih/color"
"github.com/spf13/cobra"
"runtime"
)
var (
inputFilePath string
outputFilePath string
moduleName string
fieldTagName string
watchMode bool
)
var configCmd = &cobra.Command{
Use: "config",
Short: "config management",
Long: "Use the command to convert arbitrary formats to Go Struct (including json, toml, yaml, etc.)",
Run: func(cmd *cobra.Command, args []string) {
if inputFilePath == "" || outputFilePath == "" {
if err := cmd.Help(); err != nil {
color.Red(err.Error())
}
return
}
if err := config.Convert(inputFilePath, outputFilePath, moduleName, fieldTagName); err != nil {
color.Red(err.Error())
return
}
color.Green("file convert success")
if watchMode {
fsTask, err := config.Watch(inputFilePath, outputFilePath, moduleName, fieldTagName)
if err != nil {
color.Red("%v: %s", errorx.SDKConfigGenerateFailed, err.Error())
return
}
color.Blue("file listening...")
fsTask.Wait()
}
},
Version: fmt.Sprintf("%s %s/%s", version, runtime.GOOS, runtime.GOARCH),
}
func init() {
configCmd.Flags().StringVarP(&inputFilePath, "input", "i", "", "source file path")
configCmd.Flags().StringVarP(&outputFilePath, "output", "o", "", "target file path")
configCmd.Flags().StringVarP(&moduleName, "module", "m", "", "module name of the target file, default: target file name")
configCmd.Flags().StringVarP(&fieldTagName, "tag", "t", "json", "set filed tag name, default: json")
configCmd.Flags().BoolVarP(&watchMode, "watch", "w", false, "listening file changes and auto convert content to Go Struct")
}