#!/usr/bin/env ruby # frozen_string_literal: true require 'optparse' require 'pg_export' require 'pg_export/container' ENV['KEEP_DUMPS'] = ENV['KEEP_DUMPS'] || '10' interactive = false database = nil option_parser = OptionParser.new do |opts| opts.banner = 'Usage: pg_export [options]' opts.on('-d', '--database DATABASE', String, '[Required] Name of the database to export') do |d| database = d end opts.on('-k', '--keep [KEEP]', String, "[Optional] Number of dump files to keep on FTP (default: #{ENV['KEEP_DUMPS']})") do |keep| ENV['KEEP_DUMPS'] = keep end opts.on('-t', '--timestamped', '[Optional] Enables log messages with timestamps') do ENV['LOGGER_FORMAT'] = 'timestamped' end opts.on('-m', '--muted', '[Optional] Mutes log messages (overrides -t option)') do ENV['LOGGER_FORMAT'] = 'muted' end opts.on('-i', '--interactive', '[Optional] Interactive command line mode - for restoring dumps into databases') do interactive = true end opts.on('-h', '--help', 'Show this message') do puts opts exit end opts.separator "\nSetting can be verified by running following commands:" opts.on('-c', '--configuration', 'Prints the configuration') do PgExport::Container.start(:config) puts PgExport::Container['config'].to_h exit end opts.on('-f', '--ftp', 'Tries connecting to FTP to verify the connection') do PgExport::Container.start(:ftp) ftp_adapter = PgExport::Container['factories.ftp_adapter_factory'].ftp_adapter ftp = ftp_adapter.open_ftp puts ftp.welcome exit end end begin option_parser.parse! rescue OptionParser::ParseError => e warn e.message.capitalize warn 'Type "pg_export -h" for available options' exit end begin pg_export = if interactive PgExport.interactive else PgExport.plain end rescue PgExport::InitializationError => e warn 'Unable to initialize PgExport due to invalid configuration. Check you ENVs.' warn "Detailed message: #{e.message}" exit end pg_export.call(database) do |result| result.success { puts 'Success' } result.failure { |message:| warn message } end