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
# File lib/dnsmadeeasy/runner.rb, line 64 def print_error(*args, exception: nil) unless args.empty? puts <<~EOF #{'Error:'.bold.red} #{args.join(' ').red} EOF end if exception puts <<~EOF #{'Exception: '.bold.red}#{exception.inspect.red} EOF end end
print_formatted(result, format = nil)
click to toggle source
# File lib/dnsmadeeasy/runner.rb, line 211 def print_formatted(result, format = nil) if format if format.to_sym == :json_pretty puts JSON.pretty_generate(result) elsif format.to_sym == :pp require 'pp' pp result else m = "to_#{format}".to_sym puts result.send(m) if result.respond_to?(m) end else ap(result, indent: 10) end end
print_help_message()
click to toggle source
# File lib/dnsmadeeasy/runner.rb, line 145 def print_help_message print_usage_message puts <<~EOF #{header 'Credentials'} Store your credentials in a YAML file #{DnsMadeEasy::Credentials.default_credentials_path(user: ENV['USER']).blue} as follows: #{'# ~/.dnsmadeeasy/credentials.yml'.bold.black} #{'credentials: api_key: XXXX api_secret: YYYY'.cyan} Or, you can use a multi-account version, but in that case it helps if you specify which of the accounts is the default: EOF puts <<-YAML #{'# ~/.dnsmadeeasy/credentials.yml'.bold.black} #{'accounts: - name: production credentials: api_key: XXXX api_secret: YYYY encryption_key: my_key - name: development default_account: true credentials: api_key: ZZZZ api_secret: WWWW'.cyan} YAML puts header 'Examples:' puts ' $ dme domain moo.com $ dme domain moo.com --yaml > moo.yml $ dme all moo.com $ dme find_all moo.com A www $ dme find_first moo.com CNAME vpn-west $ dme update_record moo.com www A 11.3.43.56 $ dme create_record moo.com ftp CNAME www.moo.com. $ dme --yaml find_first moo.com CNAME vpn-west'.green exit 1 end
print_signature(method, sig)
click to toggle source
# File lib/dnsmadeeasy/runner.rb, line 78 def print_signature(method, sig) puts <<~EOF #{'Error: '.bold.yellow} #{'You are missing some arguments for this operation:'.red} #{'Correct Usage: '.bold.yellow} #{method.to_s.bold.green} #{sig.join(' ').blue} EOF end
print_supported_operations()
click to toggle source
# File lib/dnsmadeeasy/runner.rb, line 199 def print_supported_operations puts <<~EOF #{header 'Actions:'} Checkout the README and RubyDoc for the arguments to each operation, which is basically a method on a DnsMadeEasy::Api::Client instance. #{'http://www.rubydoc.info/gems/dnsmadeeasy/DnsMadeEasy/Api/Client'.blue.bold.underlined} #{header 'Valid Operations Are:'} #{DnsMadeEasy::Api::Client.public_operations.join("\n ").green.bold} EOF end
print_usage_message()
click to toggle source
# File lib/dnsmadeeasy/runner.rb, line 134 def print_usage_message puts <<~EOF #{'Usage:'.bold.yellow} #{'# Execute an API call:'.dark} #{"dme [ #{SUPPORTED_FORMATS.map { |f| "--#{f}" }.join(' | ')} ] operation [ arg1 arg2 ... ] ".bold.green} #{'# Print suported operations:'.dark} #{'dme op[erations]'.bold.green} EOF end
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