class DatadogBackup::Cli

Public Class Methods

new(options) click to toggle source
# File lib/datadog_backup/cli.rb, line 88
def initialize(options)
  @options = options
  initialize_client
end

Public Instance Methods

all_diff_futures() click to toggle source
# File lib/datadog_backup/cli.rb, line 11
def all_diff_futures
  logger.info("Starting diffs on #{::DatadogBackup::ThreadPool::TPOOL.max_length} threads")
  any_resource_instance
    .all_file_ids_for_selected_resources
    .map do |id|
    Concurrent::Promises.future_on(::DatadogBackup::ThreadPool::TPOOL, id) do |id|
      [id, getdiff(id)]
    end
  end
end
any_resource_instance() click to toggle source
# File lib/datadog_backup/cli.rb, line 22
def any_resource_instance
  resource_instances.first
end
backup() click to toggle source
# File lib/datadog_backup/cli.rb, line 26
def backup
  resource_instances.each(&:purge)
  resource_instances.each(&:backup)
  any_resource_instance.all_files
end
definitive_resource_instance(id) click to toggle source
# File lib/datadog_backup/cli.rb, line 39
def definitive_resource_instance(id)
  matching_resource_instance(any_resource_instance.class_from_id(id))
end
diffs() click to toggle source
# File lib/datadog_backup/cli.rb, line 43
def diffs
  futures = all_diff_futures
  ::DatadogBackup::ThreadPool.watcher(logger).join

  format_diff_output(
    Concurrent::Promises
      .zip(*futures)
      .value!
      .compact
  )
end
format_diff_output(diff_output) click to toggle source
# File lib/datadog_backup/cli.rb, line 69
def format_diff_output(diff_output)
  case diff_format
  when nil, :color
    diff_output.map do |id, diff|
      " ---\n id: #{id}\n#{diff}"
    end.join("\n")
  when :html
    '<html><head><style>' +
      Diffy::CSS +
      '</style></head><body>' +
      diff_output.map do |id, diff|
        "<br><br> ---<br><strong> id: #{id}</strong><br>" + diff
      end.join('<br>') +
      '</body></html>'
  else
    raise 'Unexpected diff_format.'
  end
end
getdiff(id) click to toggle source
# File lib/datadog_backup/cli.rb, line 55
def getdiff(id)
  result = definitive_resource_instance(id).diff(id)
  case result
  when ''
    nil
  when "\n"
    nil
  when '<div class="diff"></div>'
    nil
  else
    result
  end
end
initialize_client() click to toggle source
# File lib/datadog_backup/cli.rb, line 32
def initialize_client
  @options[:client] ||= Dogapi::Client.new(
    datadog_api_key,
    datadog_app_key
  )
end
matching_resource_instance(klass) click to toggle source
# File lib/datadog_backup/cli.rb, line 93
def matching_resource_instance(klass)
  resource_instances.select { |resource_instance| resource_instance.instance_of?(klass) }.first
end
resource_instances() click to toggle source
# File lib/datadog_backup/cli.rb, line 97
def resource_instances
  @resource_instances ||= resources.map do |resource|
    resource.new(@options)
  end
end
restore() click to toggle source
# File lib/datadog_backup/cli.rb, line 103
def restore
  futures = all_diff_futures
  watcher = ::DatadogBackup::ThreadPool.watcher(logger)

  futures.each do |future|
    id, diff = *future.value!
    next unless diff

    if @options[:force_restore]
      definitive_resource_instance(id).restore(id)
    else
      puts '--------------------------------------------------------------------------------'
      puts format_diff_output([id, diff])
      puts '(r)estore to Datadog, overwrite local changes and (d)ownload, (s)kip, or (q)uit?'
      response = $stdin.gets.chomp
      case response
      when 'q'
        exit
      when 'r'
        puts "Restoring #{id} to Datadog."
        definitive_resource_instance(id).restore(id)
      when 'd'
        puts "Downloading #{id} from Datadog."
        definitive_resource_instance(id).get_and_write_file(id)
      when 's'
        next
      else
        puts 'Invalid response, please try again.'
        response = $stdin.gets.chomp
      end
    end
  end
  watcher.join if watcher.status
end
run!() click to toggle source
# File lib/datadog_backup/cli.rb, line 138
def run!
  puts(send(action.to_sym))
rescue SystemExit, Interrupt
  ::DatadogBackup::ThreadPool.shutdown(logger)
end