/
main.rb
293 lines (195 loc) · 5.96 KB
/
main.rb
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# encoding: utf-8
##############
# nb: for local testing use:
#
# ruby -I ../../github/sport.db.ruby/lib
# ../../github/sport.db.ruby/lib/sportdb.rb
# setup --delete --sport
# --include ../../github/football.db
require 'gli'
include GLI::App
# our own code / additional for cli only
require 'logutils/db'
require 'sportdb/cli/opts'
program_desc 'sport.db command line tool'
version SportDb::VERSION
LogUtils::Logger.root.level = :info # set logging level to info
logger = LogUtils::Logger.root
=begin
### add to help use new sections
Examples:
sportdb cl/teams cl/2012_13/cl # import champions league (cl)
sportdb --create # create database schema
More Examples:
sportdb # show stats (table counts, table props)
sportdb -i ../sport.db/db cl/teams cl/2012_13/cl # import champions league (cl) in db folder
Further information:
http://geraldb.github.com/sport.db
=end
## todo: find a better name e.g. change to settings? config? safe_opts? why? why not?
opts = SportDb::Opts.new
### global option (required)
## todo: add check that path is valid?? possible?
desc 'Database path'
arg_name 'PATH'
default_value opts.db_path
flag [:d, :dbpath]
desc 'Database name'
arg_name 'NAME'
default_value opts.db_name
flag [:n, :dbname]
desc '(Debug) Show debug messages'
switch [:verbose], negatable: false ## todo: use -w for short form? check ruby interpreter if in use too?
desc 'Only show warnings, errors and fatal messages'
switch [:q, :quiet], negatable: false
def connect_to_db( options )
puts SportDb.banner
puts "working directory: #{Dir.pwd}"
db_config = {
:adapter => 'sqlite3',
:database => "#{options.db_path}/#{options.db_name}"
}
puts "Connecting to db using settings: "
pp db_config
ActiveRecord::Base.establish_connection( db_config )
LogDb.setup # start logging to db
end
desc 'Create DB schema'
command [:create] do |c|
c.action do |g,o,args|
connect_to_db( opts )
LogDb.create
WorldDb.create
SportDb.create
SportDb.read_builtin # e.g. seasons.txt etc
puts 'Done.'
end # action
end # command create
desc "Create DB schema 'n' load all world and sports data"
arg_name 'NAME' # optional setup profile name
command [:setup,:s] do |c|
c.desc 'Sports data path'
c.arg_name 'PATH'
c.default_value opts.data_path
c.flag [:i,:include]
c.desc 'World data path'
c.arg_name 'PATH'
c.flag [:worldinclude] ## todo: use --world-include - how? find better name? add :'world-include' ???
c.action do |g,o,args|
connect_to_db( opts )
## todo: document optional setup profile arg (defaults to all)
setup = args[0] || 'all'
LogDb.create
WorldDb.create
SportDb.create
SportDb.read_builtin # e.g. seasons.txt etc
WorldDb.read_all( opts.world_data_path )
SportDb.read_setup( "setups/#{setup}", opts.data_path )
puts 'Done.'
end # action
end # command setup
desc 'Update all sports data'
arg_name 'NAME' # optional setup profile name
command [:update,:up,:u] do |c|
c.desc 'Sports data path'
c.arg_name 'PATH'
c.default_value opts.data_path
c.flag [:i,:include]
c.desc 'Delete all sports data records'
c.switch [:delete], negatable: false
c.action do |g,o,args|
connect_to_db( opts )
## todo: document optional setup profile arg (defaults to all)
setup = args[0] || 'all'
if o[:delete].present?
SportDb.delete!
SportDb.read_builtin # NB: reload builtins (e.g. seasons etc.)
end
SportDb.read_setup( "setups/#{setup}", opts.data_path )
puts 'Done.'
end # action
end # command setup
desc 'Load sports fixtures'
arg_name 'NAME' # multiple fixture names - todo/fix: use multiple option
command [:load, :l] do |c|
c.desc 'Delete all sports data records'
c.switch [:delete], negatable: false
c.action do |g,o,args|
connect_to_db( opts )
if o[:delete].present?
SportDb.delete!
SportDb.read_builtin # NB: reload builtins (e.g. seasons etc.)
end
reader = SportDb::Reader.new( opts.data_path )
args.each do |arg|
name = arg # File.basename( arg, '.*' )
reader.load( name )
end # each arg
puts 'Done.'
end
end # command load
desc 'Show logs'
command :logs do |c|
c.action do |g,o,args|
connect_to_db( opts )
LogDb::Models::Log.all.each do |log|
puts "[#{log.level}] -- #{log.msg}"
end
puts 'Done.'
end
end
desc 'Show stats'
command :stats do |c|
c.action do |g,o,args|
connect_to_db( opts )
SportDb.tables
puts 'Done.'
end
end
desc 'Show props'
command :props do |c|
c.action do |g,o,args|
connect_to_db( opts )
SportDb.props
puts 'Done.'
end
end
desc '(Debug) Test command suite'
command :test do |c|
c.action do |g,o,args|
puts "hello from test command"
puts "args (#{args.class.name}):"
pp args
puts "o (#{o.class.name}):"
pp o
puts "g (#{g.class.name}):"
pp g
LogUtils::Logger.root.debug 'test debug msg'
LogUtils::Logger.root.info 'test info msg'
LogUtils::Logger.root.warn 'test warn msg'
puts 'Done.'
end
end
pre do |g,c,o,args|
opts.merge_gli_options!( g )
opts.merge_gli_options!( o )
puts SportDb.banner
if opts.verbose?
LogUtils::Logger.root.level = :debug
end
logger.debug "Executing #{c.name}"
true
end
post do |global,c,o,args|
logger.debug "Executed #{c.name}"
true
end
on_error do |e|
puts
puts "*** error: #{e.message}"
if opts.verbose?
puts e.backtrace
end
false # skip default error handling
end
exit run(ARGV)