Sha256: a2cda9a4dc8afda4bd056454914c1ef77764748649943b8a8ae615d435211fed

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

require 'mixlib/cli'
require 'logger'
require 'shellwords'
require 'prlbackup/version'
require 'prlbackup/cli'
require 'prlbackup/virtual_machine'

module PrlBackup
  class << self
    # The global configuration based on command line options.
    attr_accessor :config
  end

  # Run the command and log the last line from stdout unless --dry-run.
  # @return [String] stdout of the comand.
  def command!(*args)
    logger.info("Running `#{args.shelljoin}`...") if PrlBackup.config[:verbose]
    unless PrlBackup.config[:dry_run]
      output = command(*args)
      logger.info(output.split("\n").last)
    else
      output = ''
    end
    output
  end

  # Run the command until it is finished.
  # @Note This will even run when option --dry-run is selected!
  # @return [String] stdout of the comand.
  def command(*args)
    output = `#{args.shelljoin} 2>&1`
    status = $?
    unless status.success?
      logger.error("Command `#{args.shelljoin}` failed with exit status #{status.exitstatus}:\n#{output}")
      exit(1)
    end
    output
  end

  def logger
    @logger ||= create_logger
  end

private

  def create_logger
    l = Logger.new(STDOUT)
    l.formatter = proc { |severity, datetime, progname, msg| "prlbackup #{severity}: [#{self}] #{msg}\n" }
    l
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
prlbackup-1.0.3 lib/prlbackup.rb
prlbackup-1.0.2 lib/prlbackup.rb
prlbackup-1.0.0 lib/prlbackup.rb