Class: FileTreeVisualizer::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/file_tree_visualizer/cli.rb

Defined Under Namespace

Classes: ProgressRenderer

Constant Summary collapse

DEFAULT_OUTPUT =
'file-tree-report.html'
DEFAULT_LOCALE =
'en'

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



12
13
14
# File 'lib/file_tree_visualizer/cli.rb', line 12

def initialize(argv)
  @argv = argv
end

Instance Method Details

#run ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# File 'lib/file_tree_visualizer/cli.rb', line 16

def run
  options, parser, locale = parse_options(@argv.dup)
  if options[:version]
    puts FileTreeVisualizer::VERSION
    return 0
  end

  if options[:help]
    puts parser
    return 0
  end

  expanded_path = File.expand_path(options[:path])
  unless File.directory?(expanded_path)
    raise ArgumentError,
          t(locale, 'cli.errors.invalid_directory', path: expanded_path)
  end

  scanner = Scanner.new(
    top_limit: options[:top],
    follow_symlinks: options[:follow_symlinks],
    excludes: options[:exclude_patterns]
  )
  progress = ProgressRenderer.new(enabled: options[:progress], locale: locale)
  callback = options[:progress] ? progress.method(:render) : nil
  result = scanner.scan(expanded_path, progress: callback)
  progress.finish
  generated_at = Time.now.utc
  output_path = maybe_write_report(options, expanded_path, result, locale, generated_at)
  json_path = write_json_export(options[:json_output], expanded_path, result, locale, generated_at)
  csv_path = write_csv_export(options[:csv_output], result)
  print_summary(expanded_path, result, locale) if options[:summary]
  puts t(locale, 'cli.messages.report_generated', path: output_path) if output_path
  puts t(locale, 'cli.messages.json_generated', path: json_path) if json_path
  puts t(locale, 'cli.messages.csv_generated', path: csv_path) if csv_path
  unless options[:summary]
    puts t(locale, 'cli.messages.scanned',
           directories: result.stats[:directories],
           files: result.stats[:files],
           skipped: result.stats[:skipped])
  end
  puts t(locale, 'cli.messages.total_size', size: format_bytes(result.stats[:bytes])) unless options[:summary]
  0
rescue OptionParser::ParseError, ArgumentError => e
  locale = error_locale
  warn e.message
  warn t(locale, 'cli.errors.use_help')
  1
rescue StandardError => e
  locale = error_locale
  warn t(locale, 'cli.errors.unexpected', message: e.message)
  1
end