-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
186 lines (140 loc) · 3.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"flag"
"fmt"
"os"
"github.com/Dobefu/csb/cmd/check_health"
"github.com/Dobefu/csb/cmd/cli"
"github.com/Dobefu/csb/cmd/database"
"github.com/Dobefu/csb/cmd/init_env"
"github.com/Dobefu/csb/cmd/logger"
"github.com/Dobefu/csb/cmd/migrate_db"
"github.com/Dobefu/csb/cmd/remote_sync"
"github.com/Dobefu/csb/cmd/server"
)
type subCommand struct {
desc string
}
var (
verbose = flag.Bool("verbose", false, "Enable verbose logging")
quiet = flag.Bool("quiet", false, "Only log warnings and errors")
envPath = flag.String("env-file", ".env", "The location of the .env file. Defaults to .env")
)
func initDB() {
err := database.Connect()
if err != nil {
logger.Fatal("Could not connect to the database: %s", err.Error())
}
err = database.DB.Ping()
if err != nil {
logger.Fatal("Could not connect to the database: %s", err.Error())
}
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
listSubCommands()
}
err := runSubCommand(args)
if err != nil {
logger.Fatal(err.Error())
}
}
func runSubCommand(args []string) error {
flag := flag.NewFlagSet(args[0], flag.ExitOnError)
var err error
switch args[0] {
case "check:health":
registerGlobalFlags(flag)
err = flag.Parse(args[1:])
if err != nil {
break
}
applyGlobalFlags()
err = check_health.Main()
case "migrate:db":
reset := flag.Bool("reset", false, "Migrate from a clean database. Warning: this will delete existing data")
registerGlobalFlags(flag)
err = flag.Parse(args[1:])
if err != nil {
break
}
applyGlobalFlags()
err = migrate_db.Main(*reset)
case "remote:sync":
reset := flag.Bool("reset", false, "Synchronise all data, instead of starting from the last sync token")
registerGlobalFlags(flag)
err = flag.Parse(args[1:])
if err != nil {
break
}
applyGlobalFlags()
err = remote_sync.Sync(*reset)
case "server":
port := flag.Uint("port", 4000, "The port to use for the web server")
registerGlobalFlags(flag)
err = flag.Parse(args[1:])
if err != nil {
break
}
applyGlobalFlags()
err = server.Start(*port)
case "create:content-type":
name := flag.String("name", "", "The name of the content type to create")
machineName := flag.String("machine-name", "", "The machine name of the content type to create")
isDryRun := flag.Bool("dry-run", false, "Log the actions instead of actually running them")
registerGlobalFlags(flag)
err = flag.Parse(args[1:])
if err != nil {
break
}
applyGlobalFlags()
err = cli.CreateContentType(*isDryRun, *name, *machineName)
default:
listSubCommands()
}
return err
}
func registerGlobalFlags(fset *flag.FlagSet) {
flag.VisitAll(func(f *flag.Flag) {
fset.Var(f.Value, f.Name, f.Usage)
})
}
func applyGlobalFlags() {
if *verbose {
logger.SetLogLevel(logger.LOG_VERBOSE)
}
if *quiet {
logger.SetLogLevel(logger.LOG_WARNING)
}
init_env.Main(*envPath)
initDB()
}
func listSubCommands() {
cmds := map[string]subCommand{
"check:health": {
desc: "Validate the health of the application configuration",
},
"migrate:db": {
desc: "Migrate or initialise the database",
},
"remote:sync": {
desc: "Synchronise all Contentstack entries into the database",
},
"server": {
desc: "Run a webserver with API endpoints",
},
"create:content-type": {
desc: "Create a new content type",
},
}
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
for idx, cmd := range cmds {
fmt.Printf(" %s:\n", idx)
fmt.Printf(" %s\n", cmd.desc)
}
if flag.Lookup("test.v") == nil {
os.Exit(1)
}
}