Class: DnsMadeEasy::CLI::Commands::Account

Inherits:
Base
  • Object
show all
Defined in:
lib/dnsmadeeasy/cli/commands/account.rb

Overview

Dispatches existing DNS Made Easy API operations under dme account.

Constant Summary collapse

RECORD_TYPES =
%w[A AAAA ANAME CNAME HTTPRED MX NS PTR SOA SPF SRV TXT].freeze
COMMON_OPTION_NAMES =
%i[format credentials api_key api_secret record_type].freeze
RECORD_LIST_OPERATIONS =
%w[all records_for].freeze
OPERATION_METADATA =
{
  'all' => {
    usage: 'dme account all DOMAIN_NAME [--record-type=TYPE]',
    arguments: ['DOMAIN_NAME                         # Domain name to list records for'],
    options: ["--record-type=TYPE, -t TYPE       # Optional record type filter: (#{RECORD_TYPES.join('/')})"],
    summary: 'List records for a managed domain'
  },
  'records_for' => {
    usage: 'dme account records_for DOMAIN_NAME [--record-type=TYPE]',
    arguments: ['DOMAIN_NAME                         # Domain name to list records for'],
    options: ["--record-type=TYPE, -t TYPE       # Optional record type filter: (#{RECORD_TYPES.join('/')})"],
    summary: 'List records for a managed domain'
  },
  'domains' => {
    usage: 'dme account domains',
    arguments: [],
    options: [],
    summary: 'List managed domains'
  }
}.freeze

Constants inherited from Base

Base::DEFAULT_CREDENTIAL_PATHS, Base::SUPPORTED_FORMATS

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.argument_help(metadata) ⇒ Object



98
99
100
101
102
103
# File 'lib/dnsmadeeasy/cli/commands/account.rb', line 98

def self.argument_help()
  arguments = .fetch(:arguments)
  return [] if arguments.empty?

  ['', 'Arguments:', *arguments.map { |argument| "  #{argument}" }]
end

.build_operation_command(operation_name) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/dnsmadeeasy/cli/commands/account.rb', line 45

def self.build_operation_command(operation_name)
  operation_class = Class.new(AccountOperation)
  operation_class.operation_name = operation_name
  operation_class.operation_parameters = DnsMadeEasy::Api::Client.instance_method(operation_name).parameters
  operation_class.desc((operation_name).fetch(:summary))
  operation_class.define_operation_arguments
  operation_class.define_record_type_option if RECORD_LIST_OPERATIONS.include?(operation_name)
  operation_class
end

.introspected_operation_metadata(operation_name) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dnsmadeeasy/cli/commands/account.rb', line 75

def self.(operation_name)
  return unless public_operation_names.include?(operation_name)

  method_parameters = DnsMadeEasy::Api::Client.instance_method(operation_name).parameters
  required_arguments = method_parameters.filter_map do |parameter_type, parameter_name|
    parameter_name.to_s.upcase if parameter_type == :req
  end

  {
    usage: (['dme account', operation_name] + required_arguments).join(' '),
    arguments: required_arguments.map { |argument_name| "#{argument_name.ljust(35)} # Required" },
    options: [],
    summary: "Execute #{operation_name}"
  }
end

.operation_help(operation_name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dnsmadeeasy/cli/commands/account.rb', line 55

def self.operation_help(operation_name)
   = (operation_name)
  return unknown_operation_help(operation_name) unless 

  [
    'Command:',
    "  #{.fetch(:usage)}",
    '',
    'Description:',
    "  #{.fetch(:summary)}",
    *argument_help(),
    *option_help()
  ].join("\n")
end

.operation_metadata(operation_name) ⇒ Object



70
71
72
73
# File 'lib/dnsmadeeasy/cli/commands/account.rb', line 70

def self.(operation_name)
  normalized_operation_name = operation_name.to_s
  OPERATION_METADATA[normalized_operation_name] || (normalized_operation_name)
end

.option_help(metadata) ⇒ Object



105
106
107
108
109
110
# File 'lib/dnsmadeeasy/cli/commands/account.rb', line 105

def self.option_help()
  options = .fetch(:options)
  return [] if options.empty?

  ['', 'Options:', *options.map { |option| "  #{option}" }]
end

.public_operation_namesObject



41
42
43
# File 'lib/dnsmadeeasy/cli/commands/account.rb', line 41

def self.public_operation_names
  DnsMadeEasy::Api::Client.public_operations
end

.unknown_operation_help(operation_name) ⇒ Object



91
92
93
94
95
96
# File 'lib/dnsmadeeasy/cli/commands/account.rb', line 91

def self.unknown_operation_help(operation_name)
  [
    "Error: account operation #{operation_name.inspect} is not valid.",
    'Hint: run `dme account --list` to see valid operations.'
  ].join("\n")
end

Instance Method Details

#call(args: [], list: false, list_operations: false) ⇒ Object



112
113
114
115
116
# File 'lib/dnsmadeeasy/cli/commands/account.rb', line 112

def call(args: [], list: false, list_operations: false, **)
  reject_unknown_subcommand!(args) unless list || list_operations

  print_operations
end