class MasterDelivery::CliMasterDelivery

command line wrapper

Attributes

params[RW]

Public Class Methods

new() click to toggle source
# File lib/cli_master_delivery.rb, line 82
def initialize
  @params = { type: VALUE_DELIVERY_TYPE[0].to_sym, dryrun: false, quiet: false }
end

Public Instance Methods

parse_options() click to toggle source
# File lib/cli_master_delivery.rb, line 86
def parse_options
  OptionParser.new do |opts|
    opts = define_options(opts)
    opts.parse!(ARGV, into: @params)
  end
end
run() click to toggle source
# File lib/cli_master_delivery.rb, line 93
def run
  return unless check_param_consistency

  fix_param_paths(%i[master delivery backup])
  basics = delivery_basics
  md = MasterDelivery.new(basics[:master_dir], basics[:backup_root])
  return unless md.confirm(basics, @params.slice(:type, :quiet, :dryrun, :yes))

  mfiles, _tmpdir = md.deliver(basics, **@params.slice(:type, :dryrun, :verbose))
  puts "done! (#{mfiles.size} master files are delivered.)" unless @params[:quiet]
end

Private Instance Methods

check_param_argv() click to toggle source
# File lib/cli_master_delivery.rb, line 211
def check_param_argv
  return true if ARGV.empty?

  puts "Invalid arguments are given: #{ARGV}"
  false
end
check_param_backup() click to toggle source
# File lib/cli_master_delivery.rb, line 187
    def check_param_backup
      master_dir = File.expand_path(@params[:master])
      bkp = @params[:backup]
      if !bkp.nil? && !bkp.empty?
        backup_root = File.expand_path(bkp)
        if backup_root.start_with?(master_dir)
          puts <<~INCLUSION
            Invalid dirs. MASTER_DIR must not include BACKUP_ROOT.
             MASTER_DIR:  #{master_dir}
             BACKUP_ROOT: #{backup_root}
          INCLUSION
          return false
        end
      end
      true
    end
check_param_consistency() click to toggle source
# File lib/cli_master_delivery.rb, line 147
def check_param_consistency
  return true if check_param_master &&
                 check_param_delivery &&
                 check_param_backup &&
                 check_param_type &&
                 check_param_argv

  puts 'See more with --help option'
  false
end
check_param_delivery() click to toggle source
# File lib/cli_master_delivery.rb, line 169
    def check_param_delivery
      if @params[:delivery].nil?
        puts "Specify delivery root by option '-d'"
        return false
      end
      master_dir = File.expand_path(@params[:master])
      delivery_root = File.expand_path(@params[:delivery])
      if delivery_root.start_with?(master_dir)
        puts <<~INCLUSION
          Invalid dirs. MASTER_DIR must not include DELIVERY_ROOT.
           MASTER_DIR:    #{master_dir}
           DELIVERY_ROOT: #{delivery_root}
        INCLUSION
        return false
      end
      true
    end
check_param_master() click to toggle source
# File lib/cli_master_delivery.rb, line 158
def check_param_master
  if @params[:master].nil?
    puts "Specify master snapshot directory by option '-m'"
  elsif !File.directory?(@params[:master])
    puts "Invalid master snapshot directory: #{@params[:master]}"
  else
    return true
  end
  false
end
check_param_type() click to toggle source
# File lib/cli_master_delivery.rb, line 204
def check_param_type
  return true if VALUE_DELIVERY_TYPE.include?(@params[:type].to_s)

  puts "Invalid delivery type: #{@params[:type]} (#{VALUE_DELIVERY_TYPE.join(' or ')})"
  false
end
define_options(opts) click to toggle source
# File lib/cli_master_delivery.rb, line 114
    def define_options(opts) # rubocop:disable Metrics/AbcSize
      opts.version = VERSION
      opts.separator ' Required:'
      opts.on('-m [MASTER_DIR]',    '--master [MASTER_DIR]', *DESC_MASTER_DIR.split(/\R/)) { |v| v }
      opts.on('-d [DELIVERY_ROOT]', '--delivery [DELIVERY_ROOT]', *DESC_DELIVERY_ROOT.split(/\R/)) { |v| v }
      opts.separator ''
      opts.separator ' Optional:'
      opts.on('-t [DELIVERY_TYPE]', '--type [DELIVERY_TYPE]', *DESC_DELIVERY_TYPE.split(/\R/), &:to_sym)
      opts.on('-b [BACKUP_ROOT]',   '--backup [BACKUP_ROOT]', *DESC_BACKUP_ROOT.split(/\R/)) { |v| v }
      opts.on('-D',                 '--[no-]dryrun', *DESC_DRYRUN.split(/\R/)) { |v| v }
      opts.on('-q',                 '--[no-]quiet', *DESC_QUIET.split(/\R/)) { |v| v }
      opts.on('-v',                 '--[no-]verbose', *DESC_VERBOSE.split(/\R/)) { |v| v }
      opts.on('-y',                 '--[no-]yes', *DESC_SKIP_CONF.split(/\R/)) { |v| v }
      opts.separator ''
      opts.separator ' Common options:'
      opts.on_tail('-h', '--help', 'Show this message') do
        puts opts
        exit
      end
      opts.on_tail('-V', '--version', 'Show version') do
        puts opts.ver
        exit
      end
      opts.banner = <<~BANNER

        #{opts.ver}
        #{DESCRIPTION}
        #{DESC_EXAMPLE}
        Usage: #{opts.program_name} -m <dir> -d <dir> [options]
      BANNER
      opts
    end
delivery_basics() click to toggle source
# File lib/cli_master_delivery.rb, line 107
def delivery_basics
  { master_dir: File.dirname(@params[:master]),
    master_id: File.basename(@params[:master]),
    delivery_root: @params[:delivery],
    backup_root: @params[:backup] }
end
fix_param_paths(options) click to toggle source
# File lib/cli_master_delivery.rb, line 218
def fix_param_paths(options)
  options.each do |opt|
    @params[opt] = fix_path_format(@params[opt])
  end
end
fix_path_format(param_path) click to toggle source
# File lib/cli_master_delivery.rb, line 224
def fix_path_format(param_path)
  return param_path if !param_path.nil? && !param_path.empty?

  Pathname.new(File.expand_path(param_path)).cleanpath.to_s
end