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

Constants included from Watson

BLUE, BOLD, CYAN, 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



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

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()
  @type_list    = Hash.new()
  @issue_count  = 0

  # Remote options
  @remote_valid   = false

  @github_valid    = false
  @github_api      = ""
  @github_endpoint = ""
  @github_repo     = ""
  @github_issues   = Hash.new()



  # Keep API param (and put username there) for OAuth update later
  @bitbucket_valid  = false
  @bitbucket_api    = ""
  @bitbucket_pw     = ""
  @bitbucket_repo   = ""
  @bitbucket_issues = Hash.new()


  @gitlab_valid    = false
  @gitlab_api      = ""
  @gitlab_endpoint = ""
  @gitlab_repo     = ""
  @gitlab_issues   = Hash.new()


  @asana_valid     = false
  @asana_api       = ""
  @asana_workspace = ""
  @asana_project   = ""
  @asana_issues    = Hash.new()


  @output_format = Watson::Formatters::DefaultFormatter


end

Instance Attribute Details

#asana_apiObject

Asana API Key



75
76
77
# File 'lib/watson/config.rb', line 75

def asana_api
  @asana_api
end

#asana_issuesObject

Hash to hold list of all Asana issues associated with repo



81
82
83
# File 'lib/watson/config.rb', line 81

def asana_issues
  @asana_issues
end

#asana_projectObject

Asana project within the workspace to place issues



79
80
81
# File 'lib/watson/config.rb', line 79

def asana_project
  @asana_project
end

#asana_validObject

Flag for whether Asana access is avaliable



73
74
75
# File 'lib/watson/config.rb', line 73

def asana_valid
  @asana_valid
end

#asana_workspaceObject

Asana workspace



77
78
79
# File 'lib/watson/config.rb', line 77

def asana_workspace
  @asana_workspace
end

#bitbucket_apiObject

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



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

def bitbucket_api
  @bitbucket_api
end

#bitbucket_issuesObject

Hash to hold list of all Bitbucket issues associated with repo



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

def bitbucket_issues
  @bitbucket_issues
end

#bitbucket_pwObject

Bitbucket password for access until OAuth is implemented for Bitbucket



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

def bitbucket_pw
  @bitbucket_pw
end

#bitbucket_repoObject

Bitbucket repo associated with current directory + watson config



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

def bitbucket_repo
  @bitbucket_repo
end

#bitbucket_validObject

Flag for whether Bitbucket access is avaliable



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

def bitbucket_valid
  @bitbucket_valid
end

#cl_entry_setObject

Flag for command line setting of file/dir to parse



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

def cl_entry_set
  @cl_entry_set
end

#cl_ignore_setObject

Flag for command line setting of file/dir to ignore



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

def cl_ignore_set
  @cl_ignore_set
end

#cl_tag_setObject

Flag for command line setting of tag to parse for



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

def cl_tag_set
  @cl_tag_set
end

#context_depthObject

Number of lines of issue context to grab



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

def context_depth
  @context_depth
end

#dir_listObject

List of directories to parse



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

def dir_list
  @dir_list
end

#file_listObject

List of all files to parse



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

def file_list
  @file_list
end

#github_apiObject

GitHub API key generated from Remote::GitHub setup



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

def github_api
  @github_api
end

#github_endpointObject

GitHub Endpoint (for GitHub Enterprise)



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

def github_endpoint
  @github_endpoint
end

#github_issuesObject

Hash to hold list of all GitHub issues associated with repo



58
59
60
# File 'lib/watson/config.rb', line 58

def github_issues
  @github_issues
end

#github_repoObject

GitHub repo associated with current directory + watson config



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

def github_repo
  @github_repo
end

#github_validObject

Flag for whether GitHub access is avaliable



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

def github_valid
  @github_valid
end

#gitlab_apiObject

GitLab API key generated from Remote::GitHub setup



87
88
89
# File 'lib/watson/config.rb', line 87

def gitlab_api
  @gitlab_api
end

#gitlab_endpointObject

GitLab Endpoint (for GitHub Enterprise)



89
90
91
# File 'lib/watson/config.rb', line 89

def gitlab_endpoint
  @gitlab_endpoint
end

#gitlab_issuesObject

Hash to hold list of all GitLab issues associated with repo



93
94
95
# File 'lib/watson/config.rb', line 93

def gitlab_issues
  @gitlab_issues
end

#gitlab_repoObject

GitLab repo associated with current directory + watson config



91
92
93
# File 'lib/watson/config.rb', line 91

def gitlab_repo
  @gitlab_repo
end

#gitlab_validObject

Flag for whether GitLab access is avaliable



85
86
87
# File 'lib/watson/config.rb', line 85

def gitlab_valid
  @gitlab_valid
end

#ignore_listObject

List of all files/folders to ignore when parsing



14
15
16
# File 'lib/watson/config.rb', line 14

def ignore_list
  @ignore_list
end

#issue_countObject

Count of number of issues found



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

def issue_count
  @issue_count
end

#output_formatObject

Formatter



96
97
98
# File 'lib/watson/config.rb', line 96

def output_format
  @output_format
end

#parse_depthObject

Number of directories to parse recursively



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

def parse_depth
  @parse_depth
end

#remote_validObject

Flag for whether remote access is avaliable



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

def remote_valid
  @remote_valid
end

#show_typeObject

Entries that watson should show



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

def show_type
  @show_type
end

#tag_listObject

List of tags to look for when parsing



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

def tag_list
  @tag_list
end

#tmp_fileObject (readonly)

Flag for where the temp file for printing is located



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

def tmp_file
  @tmp_file
end

#type_listObject

List of custom filetypes to accept



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

def type_list
  @type_list
end

#use_lessObject (readonly)

Flag for whether less is avaliable to print results



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

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



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/watson/config.rb', line 211

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 Attempts to create config based on $HOME/.watsonrc If this doesn’t exist, copies default config from /assets/defaultConf to $HOME and current directory



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

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"

  # Full path to assets/defaultConf (File class doesn't look at 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"

  # $HOME/.watsonrc exists, '~' should be crossplatform with File.expand_path
  _home_path = File.expand_path('~') + '/.watsonrc'

  # Obtain default config to write to current directory
  if Watson::FS.check_file(_home_path)
    _default = File.open(_home_path, 'r') { |file| file.read }
    debug_print ".watsonrc found in $HOME, using as base\n"
  elsif Watson::FS.check_file(_full_path)
    _default = File.open(_full_path, 'r') { |file| file.read }
    # Write default to $HOME
    File.open(_home_path, 'w') { |file| file.write(_default) }
    debug_print ".watsonrc not found in $HOME, using assets\n"
  else
    print "Unable to find .watsonrc in $HOME or #{ _full_path}\n"
    print "Cannot create a default config, exiting...\n"
    return false
  end

  # Open @rc_file and write the default contents to it
  File.open(@rc_file, 'w') { |file| file.write(_default) }
  debug_print "Successfully wrote defaultConf to current directory\n"
  true
end

#read_confObject

Read configuration file and populate Config container class



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
402
403
404
405
406
407
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
# File 'lib/watson/config.rb', line 274

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(Regexp.escape(".."))
  @ignore_list.push(Regexp.escape(@rc_file))
  @ignore_list.push(Regexp.escape(@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!.to_i


    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 "type"
      # Regex to grab ".type" => ["param1", "param2"]
      _mtch = _line.match(/(\"\S+\")\s+=>\s+(\[(\".+\")+\])/)
      if !_mtch.nil?
        _ext = _mtch[1].gsub(/\"/, '')
        _type = JSON.parse(_mtch[2])
        @type_list[_ext] = _type
      end


    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

      # Convert each ignore into a regex
      # Grab ignore and remove leading ./ and trailing /
      _mtch = _line.match(/^(\.\/)?(\S+)/)[0].gsub(/\/$/, '')

      # Escape all characters then replace \* with \.+
      _mtch = Regexp.escape(_mtch).gsub(/\\\*/, ".+")
      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_pw"
      # Same as GitHub parse above
      @bitbucket_pw = _line.chomp!
      debug_print "Bitbucket PW: #{ @bitbucket_pw }\n"

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

    when "gitlab_api"
      # Same as GitHub
      @gitlab_api = _line.chomp!
      debug_print "GitLab API: #{ @gitlab_api }\n"

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

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

    when "asana_project"
      @asana_project = _line.chomp!
      debug_print "Asana Project: #{ @asana_project }\n"

    when "asana_api"
      @asana_api = _line.chomp!
      debug_print "Asana API: #{ @asana_api }\n"

    when "asana_workspace"
      @asana_workspace = _line.chomp!
      debug_print "Asana Workspace: #{ @asana_workspace }\n"



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

  end

  return true
end

#runObject

Parse through configuration and obtain remote info if necessary



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

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


  # [review] - Theres gotta be a magic ruby way to trim this down
  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

  unless @gitlab_api.empty? && @gitlab_repo.empty?
    Remote::GitLab.get_issues(self)
  end

  unless @asana_api.empty? && @asana_project.empty? && @asana_workspace.empty?
    Remote::Asana.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



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/watson/config.rb', line 507

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