Class: Abqari::Importers::CLI

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

Overview

Argv parser + dispatcher for bin/import. Lives in the importers namespace because it depends on the registry — if you add a new importer to Importers::REGISTRY, this CLI surfaces it automatically.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_root:) ⇒ CLI

Returns a new instance of CLI.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/abqari/importers/cli.rb', line 16

def initialize(site_root:)
  @site_root = site_root
  @options = {
    collection:      'posts',
    download_images: true,
    flat:            false,
    force:           false,
    dry_run:         false,
    write_canonical: false,
    strip_selectors: []
  }
end

Class Method Details

.run(argv, site_root: Dir.pwd) ⇒ Object



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

def self.run(argv, site_root: Dir.pwd)
  new(site_root: site_root).run(argv)
end

Instance Method Details

#run(argv) ⇒ Object



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
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
# File 'lib/abqari/importers/cli.rb', line 29

def run(argv)
  platform, *rest = argv

  # An explicit `--help` is a success; being invoked with nothing
  # is a usage error. Both printed help and returned true, so
  # `bin/import` alone exited 0 — out of step with every other
  # command here (`abqari new` and `abqari theme` with no args
  # both exit 1) and a trap for any script checking the status.
  if %w[-h --help help].include?(platform)
    print_help
    return true
  end

  if platform.nil?
    warn 'Missing required argument: platform'
    warn ''
    print_help
    return false
  end

  importer_class = Importers.lookup(platform)
  unless importer_class
    warn "Unknown importer: #{platform.inspect}"
    warn ''
    print_help
    return false
  end

  parser = build_parser(platform)
  # `permute!` allows options to appear after the positional source
  # path. `order!` (the default) stops at the first non-option arg,
  # which silently drops trailing flags like `--keep-remote`.
  rest = parser.permute(rest)

  if rest.empty?
    warn "Usage: bin/import #{platform} <source> [options]"
    return false
  end
  source = rest.shift

  opts = Base::Options.new(
    site_root:       @site_root,
    collection:      @options[:collection],
    download_images: @options[:download_images],
    flat:            @options[:flat],
    force:           @options[:force],
    dry_run:         @options[:dry_run],
    write_canonical: @options[:write_canonical],
    strip_selectors: @options[:strip_selectors],
    logger:          ->(line) { puts line }
  )

  puts "Importing from #{platform} (#{source})…"
  puts "Mode: dry-run (no files written)" if opts.dry_run

  importer = importer_class.new(source: source, opts: opts)
  report = importer.run!

  puts ''
  puts report.summary_line
  unless opts.dry_run
    puts "Report: ./_import_report.md"
  end
  true
rescue StandardError => e
  warn "Import failed: #{e.class}: #{e.message}"
  warn e.backtrace.first(8).join("\n") if ENV['ABQARI_DEBUG']
  false
end