Class: Minitar::CLI::Command::Create

Inherits:
Minitar::CLI::Command
  • Object
show all
Includes:
CatchMinitarErrors
Defined in:
lib/minitar/cli/command/create.rb

Overview

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.

Constant Summary collapse

HELP =
<<-EOH.freeze
  minitar create [OPTIONS] <tarfile|-> <file|directory|-->+

Creates a new tarfile. If the tarfile is named .tar.gz or .tgz, then it
will be compressed automatically. If the tarfile is "-", then it will be
output to standard output (stdout) so that minitar may be piped.

The files or directories that will be packed into the tarfile are
specified after the name of the tarfile itself. Directories will be
processed recursively. If the token "--" is found in the list of files
to be packed, additional filenames will be read from standard input
(stdin). If any file is not found, the packaging will be halted.

create Options:
  --compress, -z  Compresses the tarfile with gzip.

EOH

Instance Method Summary collapse

Methods included from CatchMinitarErrors

#call

Instance Method Details

#altnameObject



11
12
13
# File 'lib/minitar/cli/command/create.rb', line 11

def altname
  'cr'
end

#helpObject



97
98
99
# File 'lib/minitar/cli/command/create.rb', line 97

def help
  HELP
end

#nameObject



7
8
9
# File 'lib/minitar/cli/command/create.rb', line 7

def name
  'create'
end

#run(args, opts = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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