#!/usr/bin/env ruby # frozen_string_literal: true $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../lib') require 'logger' require 'optparse' require 'datadog_backup' require 'dogapi' $stdout.sync = $stderr.sync = true LOGGER = Logger.new($stderr) unless defined?(LOGGER) LOGGER.level = Logger::INFO ## # Default parameters @options = { action: nil, datadog_api_key: ENV.fetch('DATADOG_API_KEY'), datadog_app_key: ENV.fetch('DATADOG_APP_KEY'), backup_dir: File.join(ENV.fetch('PWD'), 'backup'), diff_format: :color, resources: [DatadogBackup::Dashboards, DatadogBackup::Monitors], output_format: :yaml, force_restore: false, logger: LOGGER } def prereqs ARGV << '--help' if ARGV.empty? options = OptionParser.new do |opts| opts.banner = "Usage: #{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('--shh', 'log warnings and above') do LOGGER.level = Logger::WARN end opts.on('--shhh', 'log errors and above') do LOGGER.level = Logger::ERROR end opts.on('--backup-dir PATH', '`backup` by default') do |path| @options[:backup_dir] = path end opts.on('--monitors-only') do @options[:resources] = [DatadogBackup::Monitors] end opts.on('--dashboards-only') do @options[:resources] = [DatadogBackup::Dashboards] 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 @options[:output_format] = :json end opts.on('--no-color', 'removes colored output from diff format') do @options[:diff_format] = nil end opts.on('--diff-format FORMAT', 'one of `color`, `html_simple`, `html`') do |format| @options[:diff_format] = format.to_sym end opts.on('--force-restore', 'force restore to Datadog') do @options[:force_restore] = true end end options.parse! @options[:action] = ARGV.first if %w[backup diffs restore].include?(@options[:action]) else puts options exit 0 end end prereqs DatadogBackup::Cli.new(@options).run!