Class: ChefLicensing::ArgFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-licensing/config_fetcher/arg_fetcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV) ⇒ ArgFetcher

Returns a new instance of ArgFetcher.



4
5
6
# File 'lib/chef-licensing/config_fetcher/arg_fetcher.rb', line 4

def initialize(argv = ARGV)
  @argv = argv
end

Class Method Details

.fetch_value(arg_name, arg_type = :string) ⇒ Object



34
35
36
# File 'lib/chef-licensing/config_fetcher/arg_fetcher.rb', line 34

def self.fetch_value(arg_name, arg_type = :string)
  new.fetch_value(arg_name, arg_type)
end

Instance Method Details

#fetch_value(arg_name, arg_type = :string) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/chef-licensing/config_fetcher/arg_fetcher.rb', line 8

def fetch_value(arg_name, arg_type = :string)
  case arg_type
  when :boolean
    @argv.include?(arg_name)
  when :string
    # TODO: Refactor this code to use some library in near future
    # There were some issues with OptionParser, so we are using this
    # custom code for now.

    # Currently, we are supporting two ways of passing arguments:
    # 1. --chef-license-server foo
    # 2. --chef-license-server=foo

    # Check if argument is passed as: --chef-license-server foo
    arg_value = @argv.include?(arg_name) ? @argv[@argv.index(arg_name) + 1] : nil

    # Check if argument is passed as: --chef-license-server=foo
    # only if arg_value is nil
    if arg_value.nil?
      arg_value = @argv.select { |arg| arg.start_with?("#{arg_name}=") }.first
      arg_value = arg_value.split("=").last if arg_value
    end
    arg_value
  end
end