class DnsMadeEasy::Runner

Constants

SUPPORTED_FORMATS
SUPPORTED_FORMAT_FLAGS

Attributes

argv[RW]
format[RW]
operation[RW]

Public Class Methods

new(argv = nil) click to toggle source
# File lib/dnsmadeeasy/runner.rb, line 19
def initialize(argv = nil)
  self.argv = argv || ARGV.dup
  self.format = process_flags_format
end

Public Instance Methods

execute!() click to toggle source
# File lib/dnsmadeeasy/runner.rb, line 24
def execute!
  if argv.empty? || argv.empty?
    print_help_message
  else
    configure_authentication
    self.operation = argv.shift.to_sym
    exit call_through_client(operation)
  end
end

Private Instance Methods

call_through_client(method) click to toggle source
# File lib/dnsmadeeasy/runner.rb, line 36
def call_through_client(method)
  result = DnsMadeEasy.client.send(method, *argv)
  case result
  when NilClass
    puts 'No records returned.'
  when Hashie::Mash
    print_formatted(result.to_hash, format)
  when Array
    print_formatted(result, format)
  else
    print_formatted(result, format)
  end
  0
rescue ArgumentError => e
  STDERR.puts(e.backtrace.join("\n").yellow)
  sig = method_signature(e, method)
  print_signature(method, sig) if sig.shift == method.to_s
  print_error('Action', method.to_s.bold.yellow.to_s, 'has generated an error'.red, exception: e)
  1
rescue NoMethodError => e
  print_error('Action', method.to_s.bold.yellow.to_s, 'is not valid.'.red)
  puts 'HINT: try running ' + 'dme operations'.bold.green + ' to see the list of valid operations.'
  2
rescue Net::HTTPServerException => e
  print_error(exception: e)
  3
end
configure_authentication() click to toggle source
# File lib/dnsmadeeasy/runner.rb, line 109
def configure_authentication
  credentials_file = ENV['DNSMADEEASY_CREDENTIALS_FILE'] ||
    DnsMadeEasy::Credentials.default_credentials_path(user: Etc.getlogin)

  if ENV['DNSMADEEASY_API_KEY'] && ENV['DNSMADEEASY_API_SECRET']
    DnsMadeEasy.api_key = ENV['DNSMADEEASY_API_KEY']
    DnsMadeEasy.api_secret = ENV['DNSMADEEASY_API_SECRET']

  elsif credentials_file && ::File.exist?(credentials_file)
    keys = DnsMadeEasy::Credentials.keys_from_file(file: credentials_file)
    if keys
      DnsMadeEasy.api_key = keys.api_key
      DnsMadeEasy.api_secret = keys.api_secret
    end
  end

  if DnsMadeEasy.api_key.nil?
    print_error('API Key/Secret was not detected or read from file')
    puts('You can also set two environment variables: ')
    puts('    • DNSMADEEASY_API_KEY and ')
    puts('    • DNSMADEEASY_API_SECRET')
    exit 123
  end
end
header(message) click to toggle source
# File lib/dnsmadeeasy/runner.rb, line 195
def header(message)
  message.bold.yellow.to_s
end
method_signature(e, method) click to toggle source

e.backtrack.first looks like this: .…/dnsmadeeasy/lib/dnsmadeeasy/api/client.rb:143:in `create_a_record'

# File lib/dnsmadeeasy/runner.rb, line 229
def method_signature(e, method)
  file, line, call_method = e.backtrace.first.split(':')
  call_method = call_method.gsub(/[']/, '').split('`').last
  if call_method && call_method.to_sym == method.to_sym
    source_line = File.open(file).to_a[line.to_i - 1].chomp!
    if source_line =~ /def #{method}/
      signature = source_line.strip.gsub(/,/, '').split(/[ ()]/)
      signature.shift # remove def
      return signature.reject { |a| a =~ /^([={}\)\(])*$/ }
    end
  end
  []
rescue StandardError
  []
end
print_error(*args, exception: nil) click to toggle source
print_formatted(result, format = nil) click to toggle source
print_help_message() click to toggle source
print_signature(method, sig) click to toggle source
print_supported_operations() click to toggle source
print_usage_message() click to toggle source
process_flags_format() click to toggle source
# File lib/dnsmadeeasy/runner.rb, line 89
def process_flags_format
  if argv.first =~ /^op(erations)?$/i
    print_supported_operations

  elsif argv.include?('--help') || argv.include?('-h')
    print_help_message

  elsif !(argv & SUPPORTED_FORMAT_FLAGS).empty?
    format_flag = (argv & SUPPORTED_FORMAT_FLAGS).first
    format = format_flag.delete '--'
    argv.delete(format_flag)
    unless SUPPORTED_FORMATS.include?(format)
      warn "Error: format #{format.bold.red} is not supported."
      warn "Supported values are: #{SUPPORTED_FORMATS.join(', ')}"
      exit 1
    end
    format
  end
end
puts(*args) click to toggle source
Calls superclass method
# File lib/dnsmadeeasy/runner.rb, line 245
def puts(*args)
  super(*args)
end