-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
84 lines (68 loc) · 1.49 KB
/
cli.go
File metadata and controls
84 lines (68 loc) · 1.49 KB
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
package cli
import (
"log"
"sync"
"github.com/fatih/color"
)
type Cli struct {
Name string
Description string
Usage string
Commands []*Command
Debug bool
}
type Root struct {
rootCommand *rootCommand
Commands map[string]*Command
Debug bool
InfoLogger *log.Logger
ErrorLogger *log.Logger
}
var Debug bool = true
func New(v Cli) *Root {
Debug = v.Debug
if Debug {
greenText := color.New(color.FgHiGreen).SprintFunc()
infoLogger.Println("loading cli")
defer infoLogger.Println(greenText("cli loaded and ready"))
}
rootCommand := &rootCommand{
Name: v.Name,
Usage: v.Usage,
Description: v.Description,
}
if err := validateCommand(*rootCommand); err != nil {
warnLogger.Println(err)
}
newRoot := &Root{
rootCommand: rootCommand,
Commands: make(map[string]*Command),
}
newRoot.loadCommands(v.Commands)
if err := newRoot.rootCommand.generateRootHelp(newRoot.Commands); err != nil {
warnLogger.Println(err)
}
return newRoot
}
func (root *Root) loadCommands(newCommands []*Command) error {
var wg sync.WaitGroup
for _, v := range newCommands {
wg.Add(1)
current := v
go func() {
err := root.NewCommand(current)
if err != nil {
warnLogger.Println(err)
}
if Debug {
infoLogger.Println("loading command: " + current.Name)
for _, o := range current.Options {
infoLogger.Println("loading option: " + o.Name + " for " + current.Name)
}
}
wg.Done()
}()
}
wg.Wait()
return nil
}