Class: Fabulous::DNS

Inherits:
Thor
  • Object
show all
Defined in:
lib/fabulous/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ DNS

Returns a new instance of DNS.



86
87
88
89
90
91
# File 'lib/fabulous/cli.rb', line 86

def initialize(*args)
  super
  @pastel = Pastel.new
  @prompt = TTY::Prompt.new
  configure_client
end

Instance Method Details

#add(domain_name) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/fabulous/cli.rb', line 115

def add(domain_name)
  type = @prompt.select("Choose record type:", %w[A AAAA CNAME MX TXT])
  
  case type
  when "A"
    hostname = @prompt.ask("Hostname (e.g., www or @ for root):")
    ip = @prompt.ask("IP Address:")
    ttl = @prompt.ask("TTL:", default: "3600").to_i
    
    spinner = TTY::Spinner.new("#{@pastel.cyan('⚡')} Adding A record... ", format: :dots)
    spinner.auto_spin
    
    if client.dns.add_a_record(domain_name, hostname: hostname, ip_address: ip, ttl: ttl)
      spinner.success(@pastel.green("✓ A record added"))
    else
      spinner.error(@pastel.red("✗ Failed to add record"))
    end
    
  when "MX"
    hostname = @prompt.ask("Mail server hostname:")
    priority = @prompt.ask("Priority:", default: "10").to_i
    ttl = @prompt.ask("TTL:", default: "3600").to_i
    
    spinner = TTY::Spinner.new("#{@pastel.cyan('⚡')} Adding MX record... ", format: :dots)
    spinner.auto_spin
    
    if client.dns.add_mx_record(domain_name, hostname: hostname, priority: priority, ttl: ttl)
      spinner.success(@pastel.green("✓ MX record added"))
    else
      spinner.error(@pastel.red("✗ Failed to add record"))
    end
    
  when "CNAME"
    alias_name = @prompt.ask("Alias (e.g., blog):")
    target = @prompt.ask("Target domain:")
    ttl = @prompt.ask("TTL:", default: "3600").to_i
    
    spinner = TTY::Spinner.new("#{@pastel.cyan('⚡')} Adding CNAME record... ", format: :dots)
    spinner.auto_spin
    
    if client.dns.add_cname_record(domain_name, alias_name: alias_name, target: target, ttl: ttl)
      spinner.success(@pastel.green("✓ CNAME record added"))
    else
      spinner.error(@pastel.red("✗ Failed to add record"))
    end
    
  when "TXT"
    hostname = @prompt.ask("Hostname (@ for root):")
    text = @prompt.ask("Text value:")
    ttl = @prompt.ask("TTL:", default: "3600").to_i
    
    spinner = TTY::Spinner.new("#{@pastel.cyan('⚡')} Adding TXT record... ", format: :dots)
    spinner.auto_spin
    
    if client.dns.add_txt_record(domain_name, hostname: hostname, text: text, ttl: ttl)
      spinner.success(@pastel.green("✓ TXT record added"))
    else
      spinner.error(@pastel.red("✗ Failed to add record"))
    end
  end
rescue Fabulous::Error => e
  puts @pastel.red("✗ Error: #{e.message}")
  exit 1
end

#list(domain_name) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fabulous/cli.rb', line 95

def list(domain_name)
  spinner = TTY::Spinner.new("#{@pastel.cyan('⚡')} Fetching DNS records... ", format: :dots)
  spinner.auto_spin

  begin
    records = client.dns.list_records(domain_name, type: options[:type])
    spinner.success(@pastel.green("✓ Found #{records.length} records"))

    if records.empty?
      puts @pastel.yellow("No DNS records found")
    else
      display_dns_records(records)
    end
  rescue Fabulous::Error => e
    spinner.error(@pastel.red("✗ Error: #{e.message}"))
    exit 1
  end
end