-
Notifications
You must be signed in to change notification settings - Fork 0
/
levels.go
79 lines (65 loc) · 1.8 KB
/
levels.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
package egcmd
import (
"fmt"
)
// A Level provides a context for examples to be found,
// whether this is at the top-level of your app or at a command.
type Level struct {
name string
examples []*Example
}
// Example adds a new example to the command.
func (l *Level) Example(args, decription string) (example *Example) {
example = &Example{
Arguments: args,
Description: decription,
}
l.examples = append(l.examples, example)
return
}
// Envs adds a new top-level example that includes env details.
func (l *Level) Envs(env, args, decription string) (example *Example) {
example = &Example{
EnvVars: env,
Arguments: args,
Description: decription,
}
l.examples = append(l.examples, example)
return
}
// App provides a container for top-level and command examples to be instantiated
type App struct {
Level
commands map[string]*Command
}
// Command adds a new top-level command.
func (a *App) Command(name string) (command *Command) {
command, ok := a.commands[name]
if !ok {
command = &Command{Level: Level{name: name}}
a.commands[name] = command
}
return
}
// Find returns the examples that belong to a particular command
// or to the application top level when empty string provided
func (a *App) Find(command string) (examples ExamplesFound) {
if command == "" {
examples = ExamplesFound{Context: a.name, Examples: a.examples}
return
}
examples = ExamplesFound{Context: fmt.Sprintf("%s %s", a.name, command)}
if value, found := a.commands[command]; found {
examples.Examples = value.examples
}
return
}
// ExamplesFound provides a list of examples along with the context that the belong to
type ExamplesFound struct {
Context string
Examples []*Example
}
// Command provides a container for top-level and command examples to be instantiated
type Command struct {
Level
}