#!/usr/bin/env ruby # frozen_string_literal: true $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../lib') require 'logger' require 'optparse' $stdout.sync = $stderr.sync = true LOGGER = Logger.new($stderr) unless defined?(LOGGER) LOGGER.level = Logger::INFO require 'datadog_backup' def fatal(message) LOGGER.fatal(message) exit 1 end def options_valid?(options) %w[backup diffs restore].include?(options[:action]) %w[DD_API_KEY DD_APP_KEY].all? { |key| ENV.fetch(key, nil) } end def prereqs(defaults) # rubocop:disable Metrics/AbcSize ARGV << '--help' if ARGV.empty? result = defaults.dup options = OptionParser.new do |opts| opts.banner = "Usage: DD_API_KEY=abc123 DD_APP_KEY=abc123 #{File.basename($PROGRAM_NAME)} " opts.separator '' opts.on_tail('-h', '--help', 'Show this message') do puts opts exit 0 end opts.on('--debug', 'log debug and above') do LOGGER.level = Logger::DEBUG end opts.on('--quiet', 'log errors and above') do LOGGER.level = Logger::ERROR end opts.on('--backup-dir PATH', '`backup` by default') do |path| result[:backup_dir] = path end opts.on('--monitors-only') do result[:resources] = [DatadogBackup::Monitors] end opts.on('--dashboards-only') do result[:resources] = [DatadogBackup::Dashboards] end opts.on('--slos-only') do result[:resources] = [DatadogBackup::SLOs] end opts.on('--synthetics-only') do result[:resources] = [DatadogBackup::Synthetics] end opts.on( '--json', 'format backups as JSON instead of YAML. Does not impact `diffs` nor `restore`, but do not mix formats in the same backup-dir.' ) do result[:output_format] = :json end opts.on('--no-color', 'removes colored output from diff format') do result[:diff_format] = nil end opts.on('--diff-format FORMAT', 'one of `color`, `html_simple`, `html`') do |format| result[:diff_format] = format.to_sym end opts.on('--force-restore', 'Force restore to Datadog. Do not ask to validate. Non-interactive.') do result[:force_restore] = true end opts.on('--disable-array-sort', 'Do not sort array elements, to preserver order of dashboard widgets.') do result[:disable_array_sort] = true end end options.parse! result[:action] = ARGV.first fatal(options.banner) unless options_valid?(result) result end ## # Default parameters defaults = { action: nil, backup_dir: File.join(ENV.fetch('PWD'), 'backup'), diff_format: :color, resources: [DatadogBackup::Dashboards, DatadogBackup::Monitors, DatadogBackup::SLOs, DatadogBackup::Synthetics], output_format: :yaml, force_restore: false, disable_array_sort: false } DatadogBackup::Cli.new(prereqs(defaults)).run!