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

Class Method 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



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

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
  @cl_show_set   = false
  @cl_output_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

  @tag_format = "[TAG] - COMMENT"

  # Remote options
  @remote_valid   = false

  @github_valid    = false
  @github_api      = Hash.new
  @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 = STDOUT.tty? ? Watson::Formatters::DefaultFormatter :
                                 Watson::Formatters::NoColorFormatter


end

Instance Attribute Details

#asana_apiObject

Asana API Key



99
100
101
# File 'lib/watson/config.rb', line 99

def asana_api
  @asana_api
end

#asana_issuesObject

Hash to hold list of all Asana issues associated with repo



105
106
107
# File 'lib/watson/config.rb', line 105

def asana_issues
  @asana_issues
end

#asana_projectObject

Asana project within the workspace to place issues



103
104
105
# File 'lib/watson/config.rb', line 103

def asana_project
  @asana_project
end

#asana_validObject

Flag for whether Asana access is avaliable



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

def asana_valid
  @asana_valid
end

#asana_workspaceObject

Asana workspace



101
102
103
# File 'lib/watson/config.rb', line 101

def asana_workspace
  @asana_workspace
end

#bitbucket_apiObject

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



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

def bitbucket_api
  @bitbucket_api
end

#bitbucket_issuesObject

Hash to hold list of all Bitbucket issues associated with repo



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

def bitbucket_issues
  @bitbucket_issues
end

#bitbucket_pwObject

Bitbucket password for access until OAuth is implemented for Bitbucket



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

def bitbucket_pw
  @bitbucket_pw
end

#bitbucket_repoObject

Bitbucket repo associated with current directory + watson config



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

def bitbucket_repo
  @bitbucket_repo
end

#bitbucket_validObject

Flag for whether Bitbucket access is avaliable



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

def bitbucket_valid
  @bitbucket_valid
end

#cl_context_setObject

Flag for command line setting of context depth



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

def cl_context_set
  @cl_context_set
end

#cl_entry_setObject

Flag for command line setting of file/dir to parse



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

def cl_entry_set
  @cl_entry_set
end

#cl_ignore_setObject

Flag for command line setting of file/dir to ignore



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

def cl_ignore_set
  @cl_ignore_set
end

#cl_output_setObject

Flag for command line setting of output format



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

def cl_output_set
  @cl_output_set
end

#cl_parse_setObject

Flag for command line setting of parse depth



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

def cl_parse_set
  @cl_parse_set
end

#cl_show_setObject

Flag for command line setting of showtype



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

def cl_show_set
  @cl_show_set
end

#cl_tag_setObject

Flag for command line setting of tag to parse for



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

def cl_tag_set
  @cl_tag_set
end

#context_depthObject

Number of lines of issue context to grab



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

def context_depth
  @context_depth
end

#dir_listObject

List of directories to parse



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

def dir_list
  @dir_list
end

#file_listObject

List of all files to parse



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

def file_list
  @file_list
end

#github_apiObject

GitHub API key generated from Remote::GitHub setup



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

def github_api
  @github_api
end

#github_endpointObject

GitHub Endpoint (for GitHub Enterprise)



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

def github_endpoint
  @github_endpoint
end

#github_issuesObject

Hash to hold list of all GitHub issues associated with repo



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

def github_issues
  @github_issues
end

#github_repoObject

GitHub repo associated with current directory + watson config



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

def github_repo
  @github_repo
end

#github_validObject

Flag for whether GitHub access is avaliable



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

def github_valid
  @github_valid
end

#gitlab_apiObject

GitLab API key generated from Remote::GitHub setup



111
112
113
# File 'lib/watson/config.rb', line 111

def gitlab_api
  @gitlab_api
end

#gitlab_endpointObject

GitLab Endpoint (for GitHub Enterprise)



113
114
115
# File 'lib/watson/config.rb', line 113

def gitlab_endpoint
  @gitlab_endpoint
end

#gitlab_issuesObject

Hash to hold list of all GitLab issues associated with repo



117
118
119
# File 'lib/watson/config.rb', line 117

def gitlab_issues
  @gitlab_issues
end

#gitlab_repoObject

GitLab repo associated with current directory + watson config



115
116
117
# File 'lib/watson/config.rb', line 115

def gitlab_repo
  @gitlab_repo
end

#gitlab_validObject

Flag for whether GitLab access is avaliable



109
110
111
# File 'lib/watson/config.rb', line 109

def gitlab_valid
  @gitlab_valid
end

#ignore_listObject

List of all files/folders to ignore when parsing



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

def ignore_list
  @ignore_list
end

#issue_countObject

Count of number of issues found



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

def issue_count
  @issue_count
end

#output_formatObject

Formatter



120
121
122
# File 'lib/watson/config.rb', line 120

def output_format
  @output_format
end

#parse_depthObject

Number of directories to parse recursively



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

def parse_depth
  @parse_depth
end

#rc_fileObject

Location of .watsonrc, modified when working with remote API tokens



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

def rc_file
  @rc_file
end

#remote_validObject

Flag for whether remote access is avaliable



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

def remote_valid
  @remote_valid
end

#show_typeObject

Entries that watson should show



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

def show_type
  @show_type
end

#tag_formatObject

Tag format for look for



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

def tag_format
  @tag_format
end

#tag_listObject

List of tags to look for when parsing



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

def tag_list
  @tag_list
end

#tmp_fileObject (readonly)

Flag for where the temp file for printing is located



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

def tmp_file
  @tmp_file
end

#type_listObject

List of custom filetypes to accept



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

def type_list
  @type_list
end

#use_lessObject (readonly)

Flag for whether less is avaliable to print results



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

def use_less
  @use_less
end

Class Method Details

.home_confObject



8
9
10
11
12
13
14
# File 'lib/watson/config.rb', line 8

def home_conf
# Return Conf object for $HOME/.watsonrc
  _home_conf = Watson::Config.new()
  _home_conf.rc_file = File.expand_path('~') + '/.watsonrc'
  _home_conf.read_conf
  return _home_conf
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



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/watson/config.rb', line 240

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



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

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

#github_tokenObject

Get first key from API list (hash)



747
748
749
# File 'lib/watson/config.rb', line 747

def github_token
  self.github_api[github_api.keys[0]]
end

#read_confObject

Read configuration file and populate Config container class



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
501
502
503
504
505
506
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
603
604
605
606
607
608
609
610
611
612
613
614
615
# File 'lib/watson/config.rb', line 303

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"
      # If set from command line, ignore config file
      if @cl_context_set
        debug_print "Directories or files set from command line ignoring rc [context_depth]\n"
        next
      end

      # 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
      debug_print "@context_depth --> #{ @context_depth }\n"


    when "parse_depth"
      # If set from command line, ignore config file
      if @cl_parse_set
        debug_print "Directories or files set from command line ignoring rc [parse_depth]\n"
        next
      end

      # 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!
      debug_print "@parse_depth --> #{ @parse_depth }\n"


    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 "output_format"
      if @cl_output_set
        debug_print "Output type set from command line, ignoring rc [output_format]\n"
        next
      end

      # Set default output format for printing
      _output = _line.chomp!

      @output_format = case _output.downcase
        when 'json'
          debug_print "Output format set to JSON\n"
          Watson::Formatters::JsonFormatter
        when 'unite'
          debug_print "Output format set to Unite\n"
          Watson::Formatters::UniteFormatter
        when 'silent'
          debug_print "Output format set to Silent\n"
          Watson::Formatters::SilentFormatter
        when 'nocolor'
          debug_print "Output format set to NoColor\n"
          Watson::Formatters::NoColorFormatter
        else
         debug_print "Output format set to default (color or nocolor depending on tty)\n"
         STDOUT.tty? ? Watson::Formatters::DefaultFormatter :
                       Watson::Formatters::NoColorFormatter
      end

      debug_print "@output_format --> #{ @output_format }\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 "tag_format"
      @tag_format = _line.chomp!
      debug_print "@tag_format --> #{ @tag_format }\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

      deug_print "@type_list --> #{ @type_list }\n"


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

      # Same as previous for ignores
      # [review] - Populate @tag_list, then check size instead
      # 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 "show_type"
      if @cl_show_set
        debug_print "Show type set from command line, ignoring rc [show_type]\n"
        next
      end

      # No need for parsing, just check case
      case _line.chomp.downcase
      when "clean"
        @show_type = "clean"
        debug_print "@show_type set to \"clean\" from config\n"

      when "dirty"
        @show_type = "dirty"
        debug_print "@show_type set to \"dirty\" from config\n"

      else
        @show_type = "all"
        debug_print "@show_type set to \"all\" from config\n"
      end

      debug_print "@show_type --> #{ @show_type }\n"


    # Project directories reference $HOME/.watsonrc for GitHub API token
    # If we don't find a username=token format string, use username
    # as Hash reference to $HOME/.watsonrc --> github_api
    when "github_api"
      # Regex for username=token
      _mtch = _line.chomp.match(/(\S+)=(\S+)/)

      # If no = match, then it is a hash reference
      if _mtch.nil?
        _home = Watson::Config.home_conf
        @github_api[_line.chomp] = _home.github_api[_line.chomp]

      # If we do find match, this is a $HOME/.watsonrc
      # Populate home conf with all API tokens
      else
        @github_api[_mtch[1]] = _mtch[2]
      end

      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



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

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



622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
# File 'lib/watson/config.rb', line 622

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 }, attempting to create\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 | _name |
    _update.write("[#{ _name }]\n")
    _param = self.instance_variable_get("@#{ _name }")

    if _param.is_a?(Hash)
      # If the config file we are dealing with is in $HOME/.watsonrc
      # then write as username=token, else write just username
      pp(_param)
      if @rc_file == File.expand_path('~') + '/.watsonrc'
        _param.each do |val|
          _update.write("#{val[0]}=#{val[1]}\n")
        end
      else
        _param.each do |val|
          _update.write("#{val[0]}\n")
        end
      end

    elsif _param.is_a?(Array)
      _param.each do |val|
        _update.write("#{val}\n")
      end

    else
      _update.write("#{_param}\n")
    end

    _update.write("\n\n\n")
  end

  _update.close
end