Class: Archive::Tar::Minitar::Command::CommandCreate

Inherits:
CommandPattern show all
Defined in:
lib/archive/tar/minitar/command.rb

Instance Method Summary collapse

Methods inherited from CommandPattern

<<, [], #[], add, command, command?, default_ioe

Instance Method Details

#altnameObject



363
364
365
# File 'lib/archive/tar/minitar/command.rb', line 363

def altname
  "cr"
end

#call(args, opts = {}, ioe = {}) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/archive/tar/minitar/command.rb', line 367

def call(args, opts = {}, ioe = {})
  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"
    CommandPattern["help"][["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]
    output = Zlib::GzipWriter.new(output)
  end

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

  files << argv.to_a
  files.flatten!

  if opts[:verbose]
    watcher = lambda do |action, name, stats|
      opts[:output] << "#{name}\n" if action == :dir or action == :file_done
    end
    finisher = lambda { opts[:output] << "\n" }
  elsif opts[:progress]
    progress = ProgressBar.new(opts[:name], 1)
    watcher = lambda do |action, name, stats|
      case action
      when :file_start, :dir
        progress.title = File.basename(name)
        if action == :dir
          progress.total += 1
          progress.inc
        else
          progress.total += stats[:size]
        end
      when :file_progress
        progress.inc(stats[:currinc])
      end
    end
    finisher = lambda do
      progress.title = opts[:name]
      progress.finish
    end
  else
    watcher = nil
    finisher = lambda { }
  end

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

#helpObject



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/archive/tar/minitar/command.rb', line 449

def help
  help = <<-EOH
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
end

#nameObject



359
360
361
# File 'lib/archive/tar/minitar/command.rb', line 359

def name
  "create"
end