-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.go
More file actions
58 lines (49 loc) · 1.11 KB
/
action.go
File metadata and controls
58 lines (49 loc) · 1.11 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
package lithium
import (
"fmt"
"os"
"path/filepath"
)
func NewAction(actimpl Action) {
flags := NewFlags(actimpl.Name())
loadedActions[actimpl.Name()] = loadedAction{action: actimpl, flags: flags}
actimpl.Init(flags)
}
func Usage() {
exec, _ := os.Executable()
exec = filepath.Base(exec)
fmt.Printf("Usage: %s <action>\n", exec)
for _, key := range loadedActions.SortedKeys() {
loadedAction := loadedActions[key]
fmt.Printf(" %s options:\n", loadedAction.action.Name())
loadedAction.flags.Usage()
fmt.Printf(" %s support notes:\n", loadedAction.action.Name())
loadedAction.action.Support()
fmt.Println()
}
fmt.Printf("This help menu can be brought up again by running \"./%s help\"\n\n", exec)
os.Exit(1)
}
func Execute() {
if len(os.Args) < 2 {
fmt.Println("No action specified!")
Usage()
}
action := os.Args[1]
if action == "help" {
Usage()
}
act, ok := loadedActions[action]
if !ok {
fmt.Println("Invalid action specified!")
Usage()
}
act.flags.Parse()
res := act.action.Run()
if res == Action_Failed {
os.Exit(1)
} else if res == Action_Invalid {
Usage()
os.Exit(1)
}
}