Class: Watson::Config

Inherits:
Object
  • Object
show all
Includes:
Watson
Defined in:
lib/watson/config.rb

Overview

Configuration container class Contains all configuration options and state variables that are accessed throughout watson

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Watson

check_less, debug_print

Constructor Details

#initializeConfig

Config initialization method to setup necessary parameters, states, and vars



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

def initialize

# [review] - Read and store rc FP inside initialize?
# This way we don't need to keep reopening the FP to use it
# but then we need a way to reliably close the FP when done

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

  # Program config
  @rc_file    = ".watsonrc"
  @tmp_file     = ".watsonresults"

  @parse_depth  = 0
  @context_depth  = 15

  # State flags
  @cl_entry_set   = false
  @cl_tag_set   = false
  @cl_ignore_set  = false

  @show_type = 'all'

  # System flags
  # [todo] - Add option to save output to file also
  @use_less = false

  # Data containers
  @ignore_list  = Array.new()
  @dir_list     = Array.new()
  @file_list    = Array.new()
  @tag_list     = Array.new()

  # Remote options
  @remote_valid   = false

  @github_valid    = false
  @github_api      = ""
  @github_endpoint = ""
  @github_repo     = ""
  @github_issues   = {:open   => Hash.new(),
                      :closed => Hash.new()
                      }

  # Keep API param (and put username there) for OAuth update later
  @bitbucket_valid  = false
  @bitbucket_api    = ""
  @bitbucket_pw     = ""
  @bitbucket_repo   = ""
  @bitbucket_issues   = {:open   => Hash.new(),
               :closed => Hash.new()
              }
  @output_format = Watson::Formatters::DefaultFormatter
end

Instance Attribute Details

#bitbucket_apiObject

Bitbucket API key generated from Remote::Bitbucket setup (username for now)



62
63
64
# File 'lib/watson/config.rb', line 62

def bitbucket_api
  @bitbucket_api
end

#bitbucket_issuesObject

Hash to hold list of all Bitbucket issues associated with repo



68
69
70
# File 'lib/watson/config.rb', line 68

def bitbucket_issues
  @bitbucket_issues
end

#bitbucket_pwObject

Bitbucket password for access until OAuth is implemented for Bitbucket



64
65
66
# File 'lib/watson/config.rb', line 64

def bitbucket_pw
  @bitbucket_pw
end

#bitbucket_repoObject

Bitbucket repo associated with current directory + watson config



66
67
68
# File 'lib/watson/config.rb', line 66

def bitbucket_repo
  @bitbucket_repo
end

#bitbucket_validObject

Flag for whether Bitbucket access is avaliable



60
61
62
# File 'lib/watson/config.rb', line 60

def bitbucket_valid
  @bitbucket_valid
end

#cl_entry_setObject

Flag for command line setting of file/dir to parse



30
31
32
# File 'lib/watson/config.rb', line 30

def cl_entry_set
  @cl_entry_set
end

#cl_ignore_setObject

Flag for command line setting of file/dir to ignore



32
33
34
# File 'lib/watson/config.rb', line 32

def cl_ignore_set
  @cl_ignore_set
end

#cl_tag_setObject

Flag for command line setting of tag to parse for



34
35
36
# File 'lib/watson/config.rb', line 34

def cl_tag_set
  @cl_tag_set
end

#context_depthObject

Number of lines of issue context to grab



27
28
29
# File 'lib/watson/config.rb', line 27

def context_depth
  @context_depth
end

#dir_listObject

List of directories to parse



19
20
21
# File 'lib/watson/config.rb', line 19

def dir_list
  @dir_list
end

#file_listObject

List of all files to parse



21
22
23
# File 'lib/watson/config.rb', line 21

def file_list
  @file_list
end

#github_apiObject

GitHub API key generated from Remote::GitHub setup



50
51
52
# File 'lib/watson/config.rb', line 50

def github_api
  @github_api
end

#github_endpointObject

GitHub Endpoint (for GitHub Enterprise)



52
53
54
# File 'lib/watson/config.rb', line 52

def github_endpoint
  @github_endpoint
end

#github_issuesObject

Hash to hold list of all GitHub issues associated with repo



56
57
58
# File 'lib/watson/config.rb', line 56

def github_issues
  @github_issues
end

#github_repoObject

GitHub repo associated with current directory + watson config



54
55
56
# File 'lib/watson/config.rb', line 54

def github_repo
  @github_repo
end

#github_validObject

Flag for whether GitHub access is avaliable



48
49
50
# File 'lib/watson/config.rb', line 48

def github_valid
  @github_valid
end

#ignore_listObject

List of all files/folders to ignore when parsing



17
18
19
# File 'lib/watson/config.rb', line 17

def ignore_list
  @ignore_list
end

#output_formatObject

Formatter



70
71
72
# File 'lib/watson/config.rb', line 70

def output_format
  @output_format
end

#parse_depthObject

Number of directories to parse recursively



25
26
27
# File 'lib/watson/config.rb', line 25

def parse_depth
  @parse_depth
end

#remote_validObject

Flag for whether remote access is avaliable



45
46
47
# File 'lib/watson/config.rb', line 45

def remote_valid
  @remote_valid
end

#show_typeObject

Entries that watson should show



37
38
39
# File 'lib/watson/config.rb', line 37

def show_type
  @show_type
end

#tag_listObject

List of tags to look for when parsing



23
24
25
# File 'lib/watson/config.rb', line 23

def tag_list
  @tag_list
end

#tmp_fileObject (readonly)

Flag for where the temp file for printing is located



42
43
44
# File 'lib/watson/config.rb', line 42

def tmp_file
  @tmp_file
end

#use_lessObject (readonly)

Flag for whether less is avaliable to print results



40
41
42
# File 'lib/watson/config.rb', line 40

def use_less
  @use_less
end

Instance Method Details

#check_confObject

Check for config file in directory of execution Should have individual .rc for each dir that watson is used in This allows you to keep different preferences for different projects Create conf (with #create_conf) if not found



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/watson/config.rb', line 156

def check_conf

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

  # Check for .rc
  # If one doesn't exist, create default one with create_conf method
  if !Watson::FS.check_file(@rc_file)
    debug_print "#{ @rc_file } not found\n"
    debug_print "Creating default #{ @rc_file }\n"

    # Create default .rc and return create_conf (true if created,
    # false if not)
    return create_conf
  else
    debug_print "#{ @rc_file } found\n"
    return true
  end
end

#create_confObject

Watson config creater Copies default config from /assets/defaultConf to the current directory



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

def create_conf
# [review] - Not sure if I should use the open/read/write or Fileutils.cp

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


  # Generate full path since File doesn't care about the LOAD_PATH
  # [review] - gsub uses (.?)+ to grab anything after lib (optional), better regex?
  _full_path = __dir__.gsub(%r!/lib/watson(.?)+!, '') + "/assets/defaultConf"
  debug_print "Full path to defaultConf (in gem): #{ _full_path }\n"

  # Check to make sure we can access the default file
  if !Watson::FS.check_file(_full_path)
    print "Unable to open #{ _full_path }\n"
    print "Cannot create default, exiting...\n"
    return false
  else
    # Open default config file in read mode and read into temp
    _input = File.open(_full_path, 'r')
    _default = _input.read

    # Open rc file in current directory in write mode and write default
    _output = File.open(@rc_file, 'w')
    _output.write(_default)

    # Close both default and new rc files
    _input.close
    _output.close

    debug_print "Successfully wrote defaultConf to current directory\n"
    return true
  end
end

#read_confObject

Read configuration file and populate Config container class



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
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
322
323
324
325
326
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
353
354
355
356
357
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/watson/config.rb', line 218

def read_conf

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


  debug_print "Reading #{ @rc_file }\n"
  if !Watson::FS.check_file(@rc_file)
    print "Unable to open #{@rc_file}, exiting\n"
    return false
  else
    debug_print "Opened #{ @rc_file } for reading\n"
  end


  # Check if system has less for output
  @use_less = check_less


  # Add all the standard items to ignorelist
  # This gets added regardless of ignore list specified
  # [review] - Keep *.swp in there?
  # [todo] - Add conditional to @rc_file such that if passed by -f we accept it
  # [todo] - Add current file (watson) to avoid accidentally printing app tags
  @ignore_list.push("..")
  @ignore_list.push(@rc_file)
  @ignore_list.push(@tmp_file)

  # Open and read rc
  # [review] - Not sure if explicit file close is required here
  _rc = File.open(@rc_file, 'r').read

  debug_print "\n\n"

  # Create temp section var to keep track of what we are populating in config
  _section = ""

  # Keep index to print what line we are on
  # Could fool around with Enumerable + each_with_index but oh well
  _i = 0;

  # Fix line endings so we can support Windows/Linux edited rc files
  _rc.gsub!(/\r\n?/, "\n")
  _rc.each_line do | _line |
    debug_print "#{ _i }: #{ _line }" if (_line != "\n")
    _i = _i + 1


    # Ignore full line comments or newlines
    if _line.match(/(^#)|(^\n)|(^ )/)
      debug_print "Full line comment or newline found, skipping\n"
      # [review] - More "Ruby" way of going to next line?
      next
    end


    # [review] - Use if with match so we can call next on the line reading loop
    # Tried using match(){|_mtch|} as well as do |_mtch| but those don't seem to
    # register the next call to the outer loop, so this way will do for now

    # Regex on line to find out if we are in a new [section] of
    # config parameters. If so, store it into section var and move
    # to next line
    _mtch = _line.match(/^\[(\w+)\]/)
    if _mtch
      debug_print "Found section #{ _mtch[1] }\n"
      _section = _mtch[1]
      next
    end


    case _section
    when "context_depth"
      # No need for regex on context value, command should read this in only as a #
      # Chomp to get rid of any nonsense
      @context_depth = _line.chomp!


    when "parse_depth"
      # No need for regex on parse value, command should read this in only as a #
      # Chomp to get rid of any nonsense
      @parse_depth = _line.chomp!


    when "dirs"
      # If @dir_list or @file_list wasn't populated by CL args
      # then populate from rc
      # [review] - Populate @dirs/files_list first, then check size instead
      if @cl_entry_set
        debug_print "Directories or files set from command line ignoring rc [dirs]\n"
        next
      end

      # Regex to grab directory
      # Then substitute trailing / (necessary for later formatting)
      # Then push to @dir_list
      _mtch = _line.match(/^((\w+)?\.?\/?)+/)[0].gsub(/(\/)+$/, "")
      if !_mtch.empty?
        @dir_list.push(_mtch)
        debug_print "#{ _mtch } added to @dir_list\n"
      end
      debug_print "@dir_list --> #{ @dir_list }\n"


    when "tags"
      # Same as previous for tags
      # [review] - Populate @tag_list, then check size instead
      if @cl_tag_set
        debug_print "Tags set from command line, ignoring rc [tags]\n"
        next
      end

      # Same as previous for tags
      # [review] - Need to think about what kind of tags this supports
      # Check compatibility with GitHub + Bitbucket and what makes sense
      # Only supports single word+number tags
      _mtch = _line.match(/^(\S+)/)[0]
      if !_mtch.empty?
        @tag_list.push(_mtch)
        debug_print "#{ _mtch } added to @tag_list\n"
      end
      debug_print "@tag_list --> #{ @tag_list }\n"


    when "ignore"
      # Same as previous for ignores
      # [review] - Populate @tag_list, then check size instead

      if @cl_ignore_set
        debug_print "Ignores set from command line, ignoring rc [ignores]\n"
        next
      end

      # Same as previous for ignores (regex same as dirs)
      # Don't eliminate trailing / because not sure if dir can have
      # same name as file (Linux it can't, but not sure about Win/Mac)
      # [review] - Can Win/Mac have dir + file with same name in same dir?
      _mtch = _line.match(/^(\*?)((\w+)?\.?\/?)+/)[0]
      if !_mtch.empty?
        @ignore_list.push(_mtch)
        debug_print "#{ _mtch } added to @ignore_list\n"
      end
      debug_print "@ignore_list --> #{ @ignore_list }\n"


    when "github_api"
      # No need for regex on API key, GitHub setup should do this properly
      # Chomp to get rid of any nonsense
      @github_api = _line.chomp!
      debug_print "GitHub API: #{ @github_api }\n"


    when "github_endpoint"
      # Same as above
      @github_endpoint = _line.chomp!
      debug_print "GitHub Endpoint #{ @github_endpoint }\n"


    when "github_repo"
      # Same as above
      @github_repo = _line.chomp!
      debug_print "GitHub Repo: #{ @github_repo }\n"


    when "bitbucket_api"
      # Same as GitHub parse above
      @bitbucket_api = _line.chomp!
      debug_print "Bitbucket API: #{ @bitbucket_api }\n"


    when "bitbucket_repo"
      # Same as GitHub repo parse above
      @bitbucket_repo = _line.chomp!
      debug_print "Bitbucket Repo: #{ @bitbucket_repo }\n"


    else
      debug_print "Unknown tag found #{_section}\n"
    end

  end

  return true
end

#runObject

Parse through configuration and obtain remote info if necessary



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/watson/config.rb', line 132

def run

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

  # check_conf should create if no conf found, exit entirely if can't do either
  exit if check_conf == false
  read_conf

  unless @github_api.empty? && @github_repo.empty?
    Remote::GitHub.get_issues(self)
  end

  unless @bitbucket_api.empty? && @bitbucket_repo.empty?
    Remote::Bitbucket.get_issues(self)
  end
end

#update_conf(*params) ⇒ Object

Update config file with specified parameters Accepts input parameters that should be updated and writes to file Selective updating to make bookkeeping easier



408
409
410
411
412
413
414
415
416
417
418
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/watson/config.rb', line 408

def update_conf(*params)

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

  # Check if RC exists, if not create one
  if !Watson::FS.check_file(@rc_file)
    print "Unable to open #{ @rc_file }, exiting\n"
    create_conf
  else
    debug_print "Opened #{ @rc_file } for reading\n"
  end

  # Go through all given params and make sure they are actually config vars
  params.each_with_index do | _param, _i |
    if !self.instance_variable_defined?("@#{ _param }")
      debug_print "#{ _param } does not exist in Config\n"
      debug_print "Check your input(s) to update_conf\n"
      params.slice!(_i)
    end
  end


  # Read in currently saved RC and go through it line by line
  # Only update params that were passed to update_conf
  # This allows us to clean up the config file at the same time


  # Open and read rc
  # [review] - Not sure if explicit file close is required here
  _rc = File.open(@rc_file, 'r').read
  _update = File.open(@rc_file, 'w')


  # Keep index to print what line we are on
  # Could fool around with Enumerable + each_with_index but oh well
  _i = 0;

  # Keep track of newlines for prettying up the conf
  _nlc = 0
  _section = ""

  # Fix line endings so we can support Windows/Linux edited rc files
  _rc.gsub!(/\r\n?/, "\n")
  _rc.each_line do | _line |
    # Print line for debug purposes
    debug_print "#{ _i }: #{ _line }"
    _i = _i + 1


    # Look for sections and set section var
    _mtch = _line.match(/^\[(\w+)\]/)
    if _mtch
      debug_print "Found section #{ _mtch[1] }\n"
      _section = _mtch[1]
    end

    # Check for newlines
    # If we already have 2 newlines before any actual content, skip
    # This is just to make the RC file output nicer looking
    if _line == "\n"
      debug_print "Newline found\n"
      _nlc = _nlc + 1
      if _nlc < 3
        debug_print "Less than 3 newlines so far, let it print\n"
        _update.write(_line)
      end
    # If the section we are in doesn't match the params passed to update_conf
    # it is safe to write the line over to the new config
    elsif !params.include?(_section)
      debug_print "Current section NOT a param to update\n"
      debug_print "Writing to new rc\n"
      _update.write(_line)

      # Reset newline
      _nlc = 0
    end

    debug_print "line: #{ _line }\n"
    debug_print "nlc: #{ _nlc }\n"
  end

  # Make sure there is at least 3 newlines between last section before writing new params
  (2 - _nlc).times do
    _update.write("\n")
  end

  # Now that we have skipped all the things that need to be updated, write them in
  params.each do | _param |
    _update.write("[#{ _param }]\n")
    _update.write("#{ self.instance_variable_get("@#{ _param }") }")
    _update.write("\n\n\n")
  end

  _update.close
end