-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
54 lines (44 loc) · 1.2 KB
/
commands.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
package main
import (
"fmt"
"github.com/ryanuber/columnize"
)
// Command is a stucture representing each command
type Command struct {
Action func(args []string, commands []Command) error
Name string
Tokens []string
Help string
}
// Execute will print the command's Help or execute the commands Action
func (command Command) Execute(args []string, commands []Command) error {
if len(args) > 0 && (args[0] == "--help" || args[0] == "-h") {
fmt.Println(command.Help)
} else {
return command.Action(args, commands)
}
return nil
}
// Contains reports if an argument can be resolved as a token
func (command Command) Contains(arg string) bool {
for _, token := range command.Tokens {
if token == arg {
return true
}
}
return false
}
// Help displays all commands that are available
func Help(args []string, commands []Command) error {
lines := []string{
"Name \t Command \t Information",
}
for _, command := range commands {
lines = append(lines, command.Name+" \t "+fmt.Sprint(command.Tokens)+" \t "+command.Help)
}
config := columnize.DefaultConfig()
config.Delim = "\t"
fmt.Println("Usage: redditfs [command] [args]")
fmt.Println(columnize.Format(lines, config))
return nil
}