Class: Omnizip::Commands::ArchiveCreateCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/commands/archive_create_command.rb

Overview

Command to create .7z archives from files and directories.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ArchiveCreateCommand

Initialize archive create command with options.

Parameters:

  • options (Hash) (defaults to: {})

    Command options from Thor



28
29
30
# File 'lib/omnizip/commands/archive_create_command.rb', line 28

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/omnizip/commands/archive_create_command.rb', line 23

def options
  @options
end

Instance Method Details

#run(output_file, *inputs) ⇒ void

Execute the archive create command.

Execute the archive create command.

Routes through the Archive facade: the handler registry picks each format's writer and applies its option semantics, so this command only translates CLI vocabulary and reports.

Parameters:

  • output_file (String)

    Path to output archive

  • inputs (Array<String>)

    Paths to files/directories to archive

  • output_file (String)

    Path to output archive

  • inputs (Array<String>)

    Paths to files/directories to archive

Returns:

  • (void)
  • (void)


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
96
97
98
99
100
# File 'lib/omnizip/commands/archive_create_command.rb', line 46

def run(output_file, *inputs)
  validate_inputs(output_file, inputs)

  # Apply profile settings if specified
  opts = options.dup
  if opts[:profile]
    first_file = find_first_file(inputs)
    opts = apply_profile(first_file, opts)
  end

  format = (opts[:format] || detect_format(output_file)).to_sym
  handler_opts = handler_options(format, opts)
  verbose = opts[:verbose] || false

  announce(output_file, format, handler_opts, opts, verbose)

  start_time = Time.now

  Omnizip::Archive.create(output_file, format: format,
                                       **handler_opts) do |archive|
    inputs.each do |input|
      if File.directory?(input)
        CliOutputFormatter.verbose_puts(
          "Adding directory: #{input}",
          verbose,
        )
        archive.add_directory(input)
      else
        CliOutputFormatter.verbose_puts(
          "Adding file: #{input}",
          verbose,
        )
        archive.add_file(input)
      end
    end
  end

  elapsed = Time.now - start_time
  archive_size = calculate_archive_size(output_file,
                                        handler_opts[:volume_size])

  if verbose
    puts ""
    puts "Archive created successfully"
    if handler_opts[:volume_size]
      puts "Total size: #{format_bytes(archive_size)}"
      puts "Volumes: #{count_volumes(output_file)}"
    else
      puts "Archive size: #{format_bytes(archive_size)}"
    end
    puts "Time elapsed: #{elapsed.round(2)}s"
  else
    puts "Created: #{output_file}"
  end
end