Class: Omnizip::Commands::ArchiveExtractCommand

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

Overview

Command to extract .7z archives to a directory.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ArchiveExtractCommand

Initialize archive extract command with options.

Parameters:

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

    Command options from Thor



28
29
30
# File 'lib/omnizip/commands/archive_extract_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_extract_command.rb', line 23

def options
  @options
end

Instance Method Details

#run(archive_file, output_dir = nil) ⇒ void

This method returns an undefined value.

Execute the archive extract command.

Parameters:

  • archive_file (String)

    Path to .7z archive

  • output_dir (String) (defaults to: nil)

    Directory to extract to



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
96
# File 'lib/omnizip/commands/archive_extract_command.rb', line 37

def run(archive_file, output_dir = nil)
  validate_input(archive_file)

  # Default to current directory if not specified
  output_dir ||= "."
  output_dir = File.expand_path(output_dir)

  verbose = options[:verbose] || false
  patterns = Array(options[:pattern]) if options[:pattern]
  excludes = Array(options[:exclude]) if options[:exclude]
  regex = options[:regex]

  if verbose
    CliOutputFormatter.verbose_puts(
      "Extracting archive: #{archive_file}",
      true,
    )
    CliOutputFormatter.verbose_puts(
      "Output directory: #{output_dir}",
      true,
    )
    if patterns
      CliOutputFormatter.verbose_puts(
        "Include patterns: #{patterns.join(', ')}",
        true,
      )
    end
    if excludes
      CliOutputFormatter.verbose_puts(
        "Exclude patterns: #{excludes.join(', ')}",
        true,
      )
    end
    if regex
      CliOutputFormatter.verbose_puts(
        "Regex pattern: #{regex}",
        true,
      )
    end
  end

  start_time = Time.now

  file_count = if patterns || excludes || regex
                 extract_with_patterns(archive_file, output_dir, verbose)
               else
                 extract_archive(archive_file, output_dir, verbose)
               end

  elapsed = Time.now - start_time

  if verbose
    puts ""
    puts "Extraction completed successfully"
    puts "Files extracted: #{file_count}"
    puts "Time elapsed: #{elapsed.round(2)}s"
  else
    puts "Extracted #{file_count} file(s) to: #{output_dir}"
  end
end