Class: Watson::Command

Inherits:
Object
  • Object
show all
Extended by:
Watson
Defined in:
lib/watson/command.rb

Overview

Command line parser class Controls program flow and parses options given by command line

Constant Summary collapse

DEBUG =

Debug printing for this class

false

Constants included from Watson

BLUE, BOLD, CYAN, GLOBAL_DEBUG_OFF, GLOBAL_DEBUG_ON, GRAY, GREEN, MAGENTA, RED, RESET, UNDERLINE, VERSION, WHITE, YELLOW

Class Method Summary collapse

Methods included from Watson

check_less, debug_print

Class Method Details

.execute(*args) ⇒ Object

Command line controller Manages program flow from given command line arguments



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
# File 'lib/watson/command.rb', line 18

def execute(*args)
# [review] - Should command line args append or overwrite config/RC parameters?
# 			 Currently we overwrite, maybe add flag to overwrite or not?

	# Identify method entry
	debug_print "#{ self } : #{ __method__ }\n"

	# List of possible flags, used later in parsing and for user reference
	_flag_list = ["-c", "--context-depth",
				  "-d", "--dirs",
			 	  "-f", "--files",
				  "-h", "--help",
			 	  "-i", "--ignore",
			 	  "-p", "--parse-depth",
			 	  "-r", "--remote",
			 	  "-t", "--tags",
			 	  "-u", "--update",
				  "-v", "--version"
				 ]


	# If we get the version or help flag, ignore all other flags
	# Just display these and exit
	# Using .index instead of .include? to stay consistent with other checks
	return help   			if args.index('-h') != nil  || args.index('--help')    != nil
	return version			if args.index('-v') != nil  || args.index('--version') != nil



	# If not one of the above then we are performing actual watson stuff
	# Create all the necessary watson components so we can perform
	# all actions associated with command line args


	# Only create Config, don't call run method
	# Populate Config parameters from CL THEN call run to fill in gaps from RC
	# Saves some messy checking and sorting/organizing of parameters
	@config 	= Watson::Config.new
	@parser 	= Watson::Parser.new(@config)
	@printer 	= Watson::Printer.new(@config)
	
	# Capture Ctrl+C interrupt for clean exit
	# [review] - Not sure this is the correct place to put the Ctrl+C capture
	trap("INT") do
		File.delete(@config.tmp_file) if File.exists?(@config.tmp_file)
		exit 2
	end

	# Parse command line options
	# Begin by slicing off until we reach a valid flag
	
	# Always look at first array element in case and then slice off what we need
	# Accept parameters to be added / overwritten if called twice
	# Slice out from argument until next argument

	# Clean up argument list by removing elements until the first valid flag	
	until _flag_list.include?(args[0]) || args.length == 0
		# [review] - Make this non-debug print to user?
		debug_print "Unrecognized flag #{ args[0] }\n"
		args.slice!(0)
	end

	# Parse command line options
	# Grab flag (should be first arg) then slice off args until next flag
	# Repeat until all args have been dealt with

	until args.length == 0
		# Set flag for calling function later
		_flag = args.slice!(0)

		debug_print "Current Flag: #{ _flag }\n"

		# Go through args until we find the next valid flag or all args are parsed 
		_i = 0
		until _flag_list.include?(args[_i]) ||  _i > (args.length - 1) 
			debug_print "Arg: #{ args[_i] }\n"
			_i = _i + 1	
		end
			
		# Slice off the args for the flag (inclusive) using index from above
		# [review] - This is a bit messy (to slice by _i - 1) when we have control
		# over the _i index above but I don't want to
		# think about the logic right now so look at later
		_flag_args = args.slice!(0..(_i-1))

		case _flag 
		when "-c", "--context-depth"
			debug_print "Found -c/--context-depth argument\n"
			set_context(_flag_args)

		when "-d", "--dirs"
			debug_print "Found -d/--dirs argument\n"
			set_dirs(_flag_args)
		
		when "-f", "--files"
			debug_print "Found -f/--files argument\n"
			set_files(_flag_args)

		when "-i", "--ignore"
			debug_print "Found -i/--ignore argument\n"
			set_ignores(_flag_args)

		when "-p", "--parse-depth"
			debug_print "Found -r/--parse-depth argument\n"
			set_parse_depth(_flag_args)
		
		when "-r", "--remote"
			debug_print "Found -r/--remote argument\n"
			# Run config to populate all the fields and such
			# [review] - Not a fan of running these here but want to avoid getting all issues when running remote (which @config.run does)
			@config.check_conf
			@config.read_conf
			setup_remote(_flag_args)
			
			# If setting up remote, exit afterwards
			exit true
		
		when "-t", "--tags"
			debug_print "Found -t/--tags argument\n"
			set_tags(_flag_args)

		when "-u", "--update"
			debug_print "Found -u/--update argument\n"
			@config.remote_valid =  true


		else
			print "Unknown argument #{ _flag }\n"
		end
	end

	debug_print "Args length 0, running watson...\n"
	@config.run
	structure = @parser.run
	@printer.run(structure)
end

.helpObject

Print help for watson



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
# File 'lib/watson/command.rb', line 158

def help
# [todo] - Add bold and colored printing
	
	# Identify method entry
	debug_print "#{ self } : #{ __method__ }\n"
	
	print BOLD;
	print "Usage: watson [OPTION]...\n"
  		print "Running watson with no arguments will parse with settings in RC file\n"
  		print "If no RC file exists, default RC file will be created\n"

  		print "\n"
	print "   -c, --context-depth   number of lines of context to provide with posted issue\n"
  		print "   -d, --dirs            list of directories to search in\n"
  		print "   -f, --files           list of files to search in\n"
  		print "   -h, --help            print help\n"
  		print "   -i, --ignore          list of files, directories, or types to ignore\n"
  		print "   -p, --parse-depth     depth to recursively parse directories\n"
  		print "   -r, --remote          list / create tokens for Bitbucket/GitHub\n"
  		print "   -t, --tags            list of tags to search for\n"
  		print "   -u, --update          update remote repos with current issues\n"
  		print "   -v, --version    	 print watson version and info\n"
  		print "\n"

  		print "Any number of files, tags, dirs, and ignores can be listed after flag\n"
  		print "Ignored files should be space separated\n"
  		print "To use *.filetype identifier, encapsulate in \"\" to avoid shell substitutions \n"
  		print "\n"

  		print "Report bugs to: watson\@goosecode.com\n"
  		print "watson home page: <http://goosecode.com/projects/watson>\n"
  		print "[goosecode] labs | 2012-2013\n"
  		print RESET;

    return true

end

.set_context(args) ⇒ Object

set_context Set context_depth parameter in config



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
# File 'lib/watson/command.rb', line 217

def set_context(args) 

	# Identify method entry
	debug_print "#{ self } : #{ __method__ }\n"
	
	# Need at least one dir in args
	if args.length <= 0
		# [review] - Make this a non-debug print to user?
		debug_print "No args passed, exiting\n"
		return false
	end


	# For context_depth we do NOT append to RC, ALWAYS overwrite
	# For each argument passed, make sure valid, then set @config.parse_depth 
	args.each do | _context_depth |
		if _context_depth.match(/^(\d+)/)
			debug_print "Setting #{ _context_depth } to config context_depth\n"	
			@config.context_depth = _context_depth.to_i
		else
			debug_print "#{ _context_depth } invalid depth, ignoring\n"
		end
	end

	# Doesn't make much sense to set context_depth for each individual post
	# When you use this command line arg, it writes the config parameter	
	@config.update_conf("context_depth")

	debug_print "Updated context_depth: #{ @config.context_depth }\n"
	return true
end

.set_dirs(args) ⇒ Object

set_dirs

Set directories to be parsed by watson



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
# File 'lib/watson/command.rb', line 253

def set_dirs(args) 

	# Identify method entry
	debug_print "#{ self } : #{ __method__ }\n"
	
	# Need at least one dir in args
	if args.length <= 0
		# [review] - Make this a non-debug print to user?
		debug_print "No args passed, exiting\n"
		return false
	end

	# Set config flag for CL entryset  in config
	@config.cl_entry_set = true	
	debug_print "Updated cl_entry_set flag: #{ @config.cl_entry_set }\n"
	
	# [review] - Should we clean the dir before adding here?
	# For each argument passed, make sure valid, then add to @config.dir_list
	args.each do | _dir |
		# Error check on input
		if !Watson::FS.check_dir(_dir)
			print "Unable to open #{ _dir }\n"
		else
			# Clean up directory path
			_dir = _dir.match(/^((\w+)?\.?\/?)+/)[0].gsub(/(\/)+$/, "")
			if !_dir.empty?
				debug_print "Adding #{ _dir } to config dir_list\n"
				@config.dir_list.push(_dir)
			end
		end
	end

	debug_print "Updated dirs: #{ @config.dir_list }\n"
	return true
end

.set_files(args) ⇒ Object

set_files

Set files to be parsed by watson



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/watson/command.rb', line 293

def set_files(args) 

	# Identify method entry
	debug_print "#{ self } : #{ __method__ }\n"
	
	# Need at least one file in args
	if args.length <= 0
		debug_print "No args passed, exiting\n"
		return false
	end

	# Set config flag for CL entryset  in config
	@config.cl_entry_set = true	
	debug_print "Updated cl_entry_set flag: #{ @config.cl_entry_set }\n"

	# For each argument passed, make sure valid, then add to @config.file_list
	args.each do | _file |
		# Error check on input
		if !Watson::FS.check_file(_file)
			print "Unable to open #{ _file }\n"
		else
			debug_print "Adding #{ _file } to config file_list\n"
			@config.file_list.push(_file)
		end
	end

	debug_print "Updated files: #{ @config.file_list }\n"
	return true
end

.set_ignores(args) ⇒ Object

set_ignores Set files and dirs to be ignored when parsing by watson



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/watson/command.rb', line 327

def set_ignores(args)

	# Identify method entry
	debug_print "#{ self } : #{ __method__ }\n"
	
	# Need at least one ignore in args
	if args.length <= 0
		debug_print "No args passed, exiting\n"
		return false
	end

	# Set config flag for CL ignore set in config
	@config.cl_ignore_set = true	
	debug_print "Updated cl_ignore_set flag: #{ @config.cl_ignore_set }\n"


	# For ignores we do NOT overwrite RC, just append	
	# For each argument passed, add to @config.ignore_list
	args.each do | _ignore |
		debug_print "Adding #{ _ignore } to config ignore_list\n"
		@config.ignore_list.push(_ignore)
	end

	debug_print "Updated ignores: #{ @config.ignore_list }\n"
	return true
end

.set_parse_depth(args) ⇒ Object

set_parse_depth Set how deep to recursively parse directories



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/watson/command.rb', line 358

def set_parse_depth(args)

	# Identify method entry
	debug_print "#{ self } : #{ __method__ }\n"
	
	# This should be a single, numeric, value
	# If they pass more, just take the last valid value
	if args.length <= 0
		debug_print "No args passed, exiting\n"
		return false
	end

	# For max_dpeth we do NOT append to RC, ALWAYS overwrite	
	# For each argument passed, make sure valid, then set @config.parse_depth 
	args.each do | _parse_depth |
		if _parse_depth.match(/^(\d+)/)
			debug_print "Setting #{ _parse_depth } to config parse_depth\n"	
			@config.parse_depth = _parse_depth
		else
			debug_print "#{ _parse_depth } invalid depth, ignoring\n"
		end
	end

	debug_print "Updated parse_depth: #{ @config.parse_depth }\n"
	return true
end

.set_tags(args) ⇒ Object

set_tags Set tags to look for when parsing files and folders



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/watson/command.rb', line 389

def set_tags(args)

	# Identify method entry
	debug_print "#{ self } : #{ __method__ }\n"
	
	# Need at least one tag in args
	if args.length <= 0
		debug_print "No args passed, exiting\n"
		return false
	end
	
	# Set config flag for CL tag set in config
	@config.cl_tag_set = true	
	debug_print "Updated cl_tag_set flag: #{ @config.cl_tag_set }\n"

	# If set from CL, we overwrite the RC parameters
	# For each argument passed, add to @config.tag_list
	args.each do | _tag |
		debug_print "Adding #{ _tag } to config tag_list\n"
		@config.tag_list.push(_tag)
	end

	debug_print "Updated tags: #{ @config.tag_list }\n"
	return true
end

.setup_remote(args) ⇒ Object

setup_remote Handle setup of remote issue posting for GitHub and Bitbucket



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/watson/command.rb', line 419

def setup_remote(args)

	# Identify method entry
	debug_print "#{ self } : #{ __method__ }\n"

	Printer.print_header

	print BOLD + "Existing Remotes:\n" + RESET
	
	# Check the config for any remote entries (GitHub or Bitbucket) and print
	# We *should* always have a repo + API together, but API should be enough
	if @config.github_api.empty? && @config.bitbucket_api.empty?
		Printer.print_status "!", YELLOW
		print BOLD + "No remotes currently exist\n\n" + RESET
	end

	if !@config.github_api.empty?
		print BOLD + "GitHub User : " + RESET + "#{ @config.github_api }\n"
		print BOLD + "GitHub Repo : " + RESET + "#{ @config.github_repo }\n\n"
	end

	if !@config.bitbucket_api.empty?
		print BOLD + "Bitbucket User : " + RESET + "#{ @config.bitbucket_api }\n" + RESET
		print BOLD + "Bitbucket Repo : " + RESET + "#{ @config.bitbucket_repo }\n\n" + RESET
	end

	# If github or bitbucket passed, setup
	# If just -r (0 args) do nothing and only have above printed
	# If more than 1 arg is passed, unrecognized, warn user	
	if args.length == 1
		case args[0].downcase
		when "github"
			debug_print "GitHub setup called from CL\n"
			Watson::Remote::GitHub.setup(@config) 
		
		when "bitbucket"
			debug_print "Bitbucket setup called from CL\n"
			Watson::Remote::Bitbucket.setup(@config) 
		end
	elsif args.length > 1
		Printer.print_status "x", RED
		print BOLD + "Incorrect arguments passed\n" + RESET
		print "      Please specify either Github or Bitbucket to setup remote\n"
		print "      Or pass without argument to see current remotes\n"
		print "      See help (-h/--help) for more details\n"
		return false
	end
end

.versionObject

Print version information about watson



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/watson/command.rb', line 199

def version

	# Identify method entry
	debug_print "#{ self } : #{ __method__ }\n"

	print "watson v1.0\n"
	print "Copyright (c) 2012-2013 goosecode labs\n"
	print "Licensed under MIT, see LICENSE for details\n"
	print "\n"

	print "Written by nhmood, see <http://goosecode.com/projects/watson>\n"		
	return true
end