-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (132 loc) · 2.85 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"io/ioutil"
"log"
"os"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
var flags = map[string]Flag{}
func main() {
filename := "cli.yml"
if fileExists("cli.yaml") {
filename = "cli.yaml"
} else if os.Getenv("CLIMOCK_FILE") != "" {
filename = os.Getenv("CLIMOCK_FILE")
} else if os.Getenv("FAUXCLI_FILE") != "" {
filename = os.Getenv("FAUXCLI_FILE")
}
if !fileExists(filename) && os.Getenv("FAUXCLI_INIT") == "1" {
if err := initCLIYAML(filename); err != nil {
log.Fatalf("Could not init fauxcli file %s: %s", filename, err)
}
} else if !fileExists(filename) {
log.Fatalf("%s not found", filename)
}
cli, err := loadCLIYAML(filename)
if err != nil {
log.Fatal(err)
}
err = processCommands(cli).Execute()
if err != nil {
log.Fatal(err)
}
}
func processCommands(command Command) *cobra.Command {
cobraCommand := setCommand(command)
if len(command.Commands) > 0 {
for _, c := range command.Commands {
err := validateCommand(c)
if err != nil {
log.Fatalln(err.Error())
}
cobraCommand.AddCommand(processCommands(c))
}
}
if len(command.Flags) > 0 {
for _, f := range command.Flags {
err := validateFlag(f, flags)
if err != nil {
log.Fatalln(err.Error())
}
flagSet := cobraCommand.Flags()
if f.Global {
flagSet = cobraCommand.PersistentFlags()
}
switch f.Type {
case "string":
setStringFlag(f, flagSet)
case "bool":
setBoolFlag(f, flagSet)
case "int":
setIntFlag(f, flagSet)
case "float":
setFloatFlag(f, flagSet)
default:
setStringFlag(f, flagSet)
}
}
}
return cobraCommand
}
func fileExists(filename string) bool {
_, err := os.Stat(filename)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return true
}
func initCLIYAML(filename string) error {
root := Command{
Name: "mycliapp",
Help: "does something cool",
Aliases: []string{"myapp", "app"},
Output: "Hello, World!\n",
Flags: []Flag{
Flag{
Name: "debug",
Short: "d",
Help: "enables debugging",
Default: false,
Global: true,
Type: "bool",
},
},
Commands: []Command{
Command{
Name: "subcommand1",
Help: "a subcommand",
Output: "{{ if .Flags.upper.Bool -}}\nHELLO FROM SC1!\n{{ else -}}\nHello from SC1!\n{{ end -}}",
Flags: []Flag{
Flag{
Name: "upper",
Short: "u",
Help: "converts output to uppercase",
Type: "bool",
},
},
},
},
}
data, err := yaml.Marshal(&root)
if err != nil {
return err
}
err = ioutil.WriteFile(filename, data, 0644)
return err
}
func loadCLIYAML(filename string) (Command, error) {
command := Command{}
contents, err := ioutil.ReadFile(filename)
if err != nil {
return command, err
}
err = yaml.Unmarshal(contents, &command)
if err != nil {
return command, err
}
return command, nil
}