Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
executable file
·102 lines (80 loc) · 1.55 KB
/
Copy pathmain.go
File metadata and controls
executable file
·102 lines (80 loc) · 1.55 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"errors"
"os"
"github.com/dominic-wassef/ghostly"
"github.com/fatih/color"
)
const version = "1.0.0"
var gho ghostly.Ghostly
func main() {
var message string
arg1, arg2, arg3, err := validateInput()
if err != nil {
exitGracefully(err)
}
setup(arg1, arg2)
switch arg1 {
case "help":
showHelp()
case "new":
if arg2 == "" {
exitGracefully(errors.New("new requires an application name"))
}
doNew(arg2)
case "version":
color.Yellow("Application version: " + version)
case "migrate":
if arg2 == "" {
arg2 = "up"
}
err = doMigrate(arg2, arg3)
if err != nil {
exitGracefully(err)
}
message = "Migrations complete!"
case "make":
if arg2 == "" {
exitGracefully(errors.New("make requires a subcommand: (migration|model|handler)"))
}
err = doMake(arg2, arg3)
if err != nil {
exitGracefully(err)
}
default:
showHelp()
}
exitGracefully(nil, message)
}
func validateInput() (string, string, string, error) {
var arg1, arg2, arg3 string
if len(os.Args) > 1 {
arg1 = os.Args[1]
if len(os.Args) >= 3 {
arg2 = os.Args[2]
}
if len(os.Args) >= 4 {
arg3 = os.Args[3]
}
} else {
color.Red("Error: command required")
showHelp()
return "", "", "", errors.New("command required")
}
return arg1, arg2, arg3, nil
}
func exitGracefully(err error, msg ...string) {
message := ""
if len(msg) > 0 {
message = msg[0]
}
if err != nil {
color.Red("Error: %v\n", err)
}
if len(message) > 0 {
color.Yellow(message)
} else {
color.Green("Finished!")
}
os.Exit(0)
}