-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrhombifer.go
120 lines (102 loc) · 2.84 KB
/
rhombifer.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
// A flexible and simple unopinionated library for cli tools
package rhombifer
import (
"fmt"
"sync"
"github.com/racg0092/rhombifer/pkg/models"
"github.com/racg0092/rhombifer/pkg/parsing"
)
var root *Command
var once sync.Once
// Takes in a pointer to `cmd` and sets it as the root command. The **root** command is only set once
// for the application runtime. It means that `root` will be set only the first time this funtion is call.
// Use it if you need to define the `root` command before initialization
func SetRoot(cmd *Command) {
if cmd != nil {
cmd.Root = true
if cmd.Subs == nil {
cmd.Subs = make(map[string]*Command)
}
if cmd.Flags == nil {
cmd.Flags = make([]*models.Flag, 0)
}
// for thread safety
once.Do(func() {
root = cmd
})
}
}
// Returns root command. If root has not been initialized it creates a new empty [Command]
// and returns the pointer
func Root() *Command {
if root == nil {
c := Command{}
SetRoot(&c)
}
return root
}
// Runs the root command if it has been set and no help command is set as default run
func runRoot(args ...string) error {
root := Root()
if root.Run == nil && len(args) > 0 {
return ErroNoRootRunFunc
} else if root.Run == nil {
return nil
}
if len(args) > 0 {
foundFlags, err := parsing.FlagsLookup(root.Flags, args...)
if err != nil && err != parsing.ErrFlagsNilOrEmpty {
return err
}
ff = &foundFlags
}
return nil
}
//todo: this function will probably need to be refactor for better usability
// Executes command passed in. It expects [root] to be set
func ExecCommand(cmd string, args ...string) error {
root := Root()
if root == nil {
return fmt.Errorf("Expected root command to be set found %v", root)
}
if len(args) == 0 && cmd == "" || (cmd == "" && IsFirstArgFlag(args[0])) {
return runRoot(args...)
}
subcommand, found := root.Subs[cmd]
if !found {
return fmt.Errorf("Command %s was not found", cmd)
}
childcommand, args, err := DigThroughSubCommand(subcommand.Subs, args)
if err != nil && err != ErrNoSubCommands {
return err
}
if childcommand != nil {
subcommand = childcommand
}
if subcommand.Run == nil {
return fmt.Errorf("Sub command %s, does not have a valid function (Run)", subcommand.Name)
}
if len(subcommand.requiredFlags) > 0 {
if len(args) == 0 {
return fmt.Errorf("This command (%s) requires flags. Please check the commands docs", subcommand.Name)
}
valid := subcommand.ValidateRequiredFlags(args)
if !valid {
return fmt.Errorf("Command [%s] requires the expected flags but found [%v]", subcommand.Name, args)
}
}
if len(args) > 0 {
rawsOnly, err := parsing.IsRawValuesOnly(args...)
if err != nil {
return err
}
if !rawsOnly {
foundFlags, err := parsing.FlagsLookup(subcommand.Flags, args...)
if err != nil {
return err
}
ff = &foundFlags
}
}
return subcommand.Run(args...)
}