-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (63 loc) · 1.5 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
package main
import (
"flag"
"fmt"
)
var (
argConfigPath string
argVerbose int
)
func init() {
flag.StringVar(&argConfigPath, "c", ".", "Path to the configuration file")
flag.IntVar(&argVerbose, "v", 0, "Enable verbose output")
}
type Command string
const (
CommandHelp Command = "help"
CommandInit Command = "init"
CommandVersion Command = "version"
CommandUnknown Command = "unknown"
)
func parseCommand(args []string) Command {
if len(args) == 0 {
return CommandHelp
}
switch Command(args[0]) {
case CommandHelp, CommandInit, CommandVersion:
return Command(args[0])
default:
return CommandUnknown
}
}
func main() {
flag.Usage = func() {} // disable default usage message
flag.Parse() // parse arguments
args := flag.Args() // get arguments
logger := GetLogger() // set verbosity level
logger.setVerbosity(argVerbose)
// initialize config parser
configParser, err := NewConfigParser(argConfigPath)
if err != nil {
logger.logError(err.Error())
return
}
// run command looking at the first argument
switch parseCommand(args) {
case CommandHelp:
fmt.Println(configParser.generateHelpOutput())
case CommandInit:
if err := configParser.init(); err != nil {
fmt.Println(err)
} else {
fmt.Println("Initialized new DOSH configuration.")
}
case CommandVersion:
fmt.Println(getVersion())
default:
if err := configParser.runTask(args); err != nil {
fmt.Println(err)
fmt.Println("Run 'dosh help' for usage information.")
}
}
configParser.close()
}