class Minitar::CLI::Command::Create

Tarball creation command. This will be replaced in a future version by one of the better-executed CLI application frameworks like GLI, after Ruby 1.8 and 1.9 support have been dropped.

Constants

HELP

Public Instance Methods

altname() click to toggle source
# File lib/minitar/cli/command/create.rb, line 11
def altname
  'cr'
end
help() click to toggle source
# File lib/minitar/cli/command/create.rb, line 97
def help
  HELP
end
name() click to toggle source
# File lib/minitar/cli/command/create.rb, line 7
def name
  'create'
end
run(args, opts = {}) click to toggle source
# File lib/minitar/cli/command/create.rb, line 35
def run(args, opts = {})
  argv = []

  while (arg = args.shift)
    case arg
    when '--compress', '-z'
      opts[:compress] = true
    else
      argv << arg
    end
  end

  if argv.size < 2
    ioe[:output] << "Not enough arguments.\n\n"
    commander.command('help').call(%w(create))
    return 255
  end

  output = argv.shift
  if '-' == output
    opts[:name] = 'STDOUT'
    output = ioe[:output]
    opts[:output] = ioe[:error]
  else
    opts[:name] = output
    output = File.open(output, 'wb')
    opts[:output] = ioe[:output]
  end

  if opts[:name] =~ /\.tar\.gz$|\.tgz$/ or opts[:compress]
    require 'zlib'
    output = Zlib::GzipWriter.new(output)
  end

  files = []
  if argv.include?('--')
    # Read stdin for the list of files.
    files = ''
    files << ioe[:input].read until ioe[:input].eof?
    files = files.split(/\r\n|\n|\r/)
    args.delete('--')
  end

  files << argv.to_a
  files.flatten!

  watcher, finisher =
    if opts[:verbose]
      verbose
    elsif opts[:progress]
      progress
    else
      silent
    end

  Archive::Tar::Minitar.pack(files, output, &watcher)
  finisher.call
  0
ensure
  output.close if output && !output.closed?
end

Private Instance Methods

progress() click to toggle source
# File lib/minitar/cli/command/create.rb, line 112
def progress
  require 'powerbar'
  progress = PowerBar.new(:msg => opts[:name], :total => 1)
  [
    lambda { |action, name, stats|
      progress_info = {}

      case action
      when :file_start, :dir
        progress_info[:msg] = File.basename(name)

        if action == :dir
          progress_info[:total] = progress.total + 1
          progress_info[:done] = progress.done + 1
        else
          progress_info[:total] = progress.total + stats[:size]
        end
      when :file_progress
        progress_info[:done] = progress.done + stats[:currinc]
      end

      progress.show(progress_info)
    },
    lambda {
      progress.show(:msg => opts[:name])
      progress.close(true)
    }
  ]
end
silent() click to toggle source
# File lib/minitar/cli/command/create.rb, line 142
def silent
  [ lambda { |_, _, _| }, lambda {} ]
end
verbose() click to toggle source
# File lib/minitar/cli/command/create.rb, line 103
def verbose
  [
    lambda { |action, name, _stats|
      opts[:output] << "#{name}\n" if action == :dir || action == :file_done
    },
    lambda { opts[:output] << "\n" }
  ]
end