# frozen_string_literal: true require 'optparse' require 'logger' require 'amazing_print' module DatadogBackup class Cli include ::DatadogBackup::Options 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 def any_resource_instance resource_instances.first end def backup resource_instances.each(&:purge) resource_instances.each(&:backup) any_resource_instance.all_files end def initialize_client @options[:client] ||= Dogapi::Client.new( datadog_api_key, datadog_app_key ) end def definitive_resource_instance(id) matching_resource_instance(any_resource_instance.class_from_id(id)) end def diffs futures = all_diff_futures ::DatadogBackup::ThreadPool.watcher(logger).join format_diff_output( Concurrent::Promises .zip(*futures) .value! .reject { |_k, v| v.nil? } ) end def getdiff(id) result = definitive_resource_instance(id).diff(id) case result when '' nil when "\n" nil when '
' nil else result end end 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 '' + diff_output.map do |id, diff| "