-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (152 loc) · 4 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
package main
import (
"flag"
"fmt"
"os"
)
// Options given to the CLI
type Flags struct {
DestSqlite string // If destination is mehstation, the mehstation database write in.
ShowPlatforms bool // To show the list of available platforms.
InputGamelist bool // gamelist.xml file
Scrape bool // to launch the scraper
NewPlatform bool // new platform
DelPlatform bool // delete a platform
NewExecutable bool // new executbale
DelExecutable bool // delete an executable
NewRes bool // new resource
InitSchema bool // to run a .sql file
}
// ParseFlags parses the CLI options.
func ParseFlags() Flags {
flags := Flags{}
flag.StringVar(&(flags.DestSqlite), "db", "database.db", "If destination is mehstation, the mehstation database write in.")
flag.BoolVar(&(flags.InputGamelist), "import-es", false, "Import an EmulationStation gamelist.xml file.")
flag.BoolVar(&(flags.ShowPlatforms), "show-platforms", false, "Display all TheGamesDB supported platforms")
flag.BoolVar(&(flags.NewPlatform), "new-platform", false, "To create a new platform.")
flag.BoolVar(&(flags.DelPlatform), "del-platform", false, "Delete a platform and all its executables")
flag.BoolVar(&(flags.DelExecutable), "del-exec", false, "To delete an executable")
flag.BoolVar(&(flags.NewExecutable), "new-exec", false, "To create a new executable.")
flag.BoolVar(&(flags.NewRes), "new-res", false, "Create a new resource")
flag.BoolVar(&(flags.Scrape), "scrape", false, "To scrape content for a platform.")
flag.BoolVar(&(flags.InitSchema), "init", false, "Init the schema")
flag.Parse()
return flags
}
func main() {
flags := ParseFlags()
// Show platforms mode
// ----------------------
if flags.ShowPlatforms {
ShowPlatforms()
os.Exit(0)
}
// Create platform
// ----------------------
if flags.NewPlatform {
if _, exists, err := NewPlatform(flags); err != nil {
fmt.Println("Error:", err)
os.Exit(1)
} else {
if !exists {
fmt.Println("Platform created")
} else {
fmt.Println("Platform updated")
}
}
os.Exit(0)
}
// Create executable
// ----------------------
if flags.NewExecutable {
if _, existing, err := NewExecutable(flags); err != nil {
fmt.Println("Error:", err)
os.Exit(1)
} else {
if !existing {
fmt.Println("Executable created")
} else {
fmt.Println("Executable updated")
}
}
os.Exit(0)
}
// Delete a platform
// ----------------------
if flags.DelPlatform {
if deleted, err := DelPlatform(flags); err != nil {
fmt.Println("Error:", err)
os.Exit(1)
} else {
if !deleted {
fmt.Println("Platform not deleted")
} else {
fmt.Println("Platform deleted")
}
}
os.Exit(0)
}
// Delete an executable
// ----------------------
if flags.DelExecutable {
if deleted, err := DelExecutable(flags); err != nil {
fmt.Println("Error:", err)
os.Exit(1)
} else {
if !deleted {
fmt.Println("Executable not deleted")
} else {
fmt.Println("Executable deleted")
}
}
os.Exit(0)
}
// Create a new resource
// ----------------------
if flags.NewRes {
if created, err := NewResource(flags); err != nil {
fmt.Println("Error:", err)
os.Exit(1)
} else {
if !created {
fmt.Println("Resource not created")
} else {
fmt.Println("Resource created")
}
}
os.Exit(0)
}
// Scrape mode
// ----------------------
if flags.Scrape {
var amount int
var err error
if amount, err = Scraping(flags); err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
fmt.Println("Content for %d scraped.", amount)
os.Exit(0)
}
// Import from EmulationStation mode.
// ----------------------
if flags.InputGamelist {
if err := ImportES(flags); err != nil {
fmt.Println("Error:", err.Error())
os.Exit(1)
}
os.Exit(0)
}
// Run .SQL files
// ----------------------
if flags.InitSchema {
if done, err := InitSchema(flags); err != nil {
fmt.Println("Error:", err.Error())
os.Exit(1)
} else {
fmt.Println("Init schema:", done)
os.Exit(1)
}
}
flag.PrintDefaults()
}