-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (66 loc) · 1.55 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"regexp"
"github.com/broxio/axapi-cli-go/axapi/v21"
"github.com/broxio/axapi-cli-go/config"
"github.com/mitchellh/cli"
)
const (
CLI_VERSION = "0.0.1"
)
func main() {
AXAPI_VERSIONS := map[string]map[string]cli.CommandFactory{
"v21": v21.Commands,
}
command := os.Args[0]
args := os.Args[1:]
// --version or -v to just show this cli tool version.
for _, arg := range args {
if arg == "-v" || arg == "--version" {
fmt.Println(CLI_VERSION)
os.Exit(0)
}
}
// aXAPI version string must be specified.
api_version := ""
var command_map map[string]cli.CommandFactory
if len(args) > 0 {
for version, cmd_map := range AXAPI_VERSIONS {
if args[0] == version {
api_version = version
command_map = cmd_map
break
}
}
}
if api_version == "" {
fmt.Println("You have to specify api version. try:")
fmt.Printf("$ %v [API_VERSION] --help\n\n", command)
fmt.Println("Available API_VERSIONs are:")
for version, _ := range AXAPI_VERSIONS {
fmt.Printf(" - %v\n", version)
}
os.Exit(0)
}
// --config-json to override default ConfigData
configRe := regexp.MustCompile("--config-json=([^ ]+)")
var configFilePath string
for _, arg := range args {
if group := configRe.FindStringSubmatch(arg); len(group) == 2 {
configFilePath = group[1]
}
}
config.ConfigInit(configFilePath)
// run cli
c := cli.NewCLI(command+" "+api_version, CLI_VERSION)
c.Args = args[1:]
c.Commands = command_map
exitStatus, err := c.Run()
if err != nil {
log.Println(err)
}
os.Exit(exitStatus)
}