Class: Toolshed::Commands::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/toolshed/commands/base.rb

Overview

Base class for all commands responsible for common methods

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



8
9
# File 'lib/toolshed/commands/base.rb', line 8

def initialize(options = {})
end

Class Method Details

.parse(command_class, cli_options = {}) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/LineLength, Metrics/CyclomaticComplexity



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
# File 'lib/toolshed/commands/base.rb', line 11

def self.parse(command_class, cli_options = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/LineLength, Metrics/CyclomaticComplexity
  options = {}
  options_parser = OptionParser.new do |opts|
    opts.banner = cli_options[:banner] if cli_options[:banner]
    cli_options[:options].each do |option_name, option_variables|
      letter_map = ('a'..'z').map { |letter| letter }.delete_if { |letter| letter == 'h' }
      short_on = (option_variables[:short_on]) ? option_variables[:short_on] : letter_map[rand(letter_map.length)] # rubocop:disable Metrics/LineLength
      on = (option_variables[:on]) ? option_variables[:on] : "--#{option_name.to_s.split('_').join('-')} [ARG]" # rubocop:disable Metrics/LineLength
      opts.on(short_on, on) do |opt|
        value = (option_variables[:default].nil?) ? opt : option_variables[:default]
        options.merge!(option_name => value)
      end
    end
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end
  options_parser.order! if options_parser
  begin
    cli = Toolshed::CLI.new
    cli.execute(command_class, ARGV, options)
  rescue Toolshed::CommandNotFound => e
    Toolshed.logger.fatal e.message
    Toolshed.die
  end
end

Instance Method Details

#read_user_input(message, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/toolshed/commands/base.rb', line 39

def read_user_input(message, options = {})
  return options[:default] if Toolshed::Client.instance.use_defaults
  required = options[:required].is_a?(TrueClass)

  value = ''
  if required
    value = prompt_user_input(message, options) while value.empty?
  else
    value = prompt_user_input(message, options)
  end
  value
end

#read_user_input_body(message, options = {}) ⇒ Object



66
67
68
69
# File 'lib/toolshed/commands/base.rb', line 66

def read_user_input_body(message, options = {})
  return options[:body] if options.key?(:body)
  read_user_input(message, options)
end

#read_user_input_password(password, prompt_message = 'Password:') ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/toolshed/commands/base.rb', line 52

def read_user_input_password(password, prompt_message='Password:')
  return password unless password.nil? || password.empty?
  system "stty -echo"
  puts prompt_message
  value = $stdin.gets.chomp.strip
  system "stty echo"
  value
end

#read_user_input_title(message, options = {}) ⇒ Object



61
62
63
64
# File 'lib/toolshed/commands/base.rb', line 61

def read_user_input_title(message, options = {})
  return options[:title] if options.key?(:title)
  read_user_input(message, options)
end

#use_project_idObject



105
106
107
# File 'lib/toolshed/commands/base.rb', line 105

def use_project_id
  Object.const_get("#{ticket_tracker_class}::USE_PROJECT_ID") rescue false # rubocop:disable Style/RescueModifier, Metrics/LineLength
end

#use_ticket_tracker_by_type(options) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/toolshed/commands/base.rb', line 91

def use_ticket_tracker_by_type(options)
  use_field = Object.const_get("#{ticket_tracker_class}::#{options[:ticket_tracker_const]}") rescue false # rubocop:disable Metrics/LineLength, Style/RescueModifier
  if use_field
    ticket_tracker_response = read_user_input(
      options[:default_message],
      options.merge!(
        default: Toolshed::Client.instance.send(options[:default_method])
      )
    )
    options.merge!(options[:type] => ticket_tracker_response)
  end
  options
end

#use_ticket_tracker_project_id(options) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/toolshed/commands/base.rb', line 71

def use_ticket_tracker_project_id(options)
  options.merge!(
    ticket_tracker_const: 'USE_PROJECT_ID',
    type: :project_id,
    default_method: 'default_pivotal_tracker_project_id',
    default_message: "Project ID (Default: #{Toolshed::Client.instance.default_pivotal_tracker_project_id}):" # rubocop:disable Metrics/LineLength
  )
  use_ticket_tracker_by_type(options)
end

#use_ticket_tracker_project_name(options) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/toolshed/commands/base.rb', line 81

def use_ticket_tracker_project_name(options)
  options.merge!(
    ticket_tracker_const: 'USE_PROJECT_NAME',
    type: :project,
    default_method: 'default_ticket_tracker_project',
    default_message: "Project Name (Default: #{Toolshed::Client.instance.default_ticket_tracker_project}):" # rubocop:disable Metrics/LineLength
  )
  use_ticket_tracker_by_type(options)
end