Class: IndexUtil::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.normalize_legacy_args(argv) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/index_util/cli.rb', line 50

def self.normalize_legacy_args(argv)
  args = argv.dup
  legacy_commands = { "--index" => "index", "--index-new" => "index-new", "--index-update" => "index-update" }
  legacy_flag = legacy_commands.keys.find { |flag| args.include?(flag) }
  return [legacy_commands.fetch(legacy_flag), *args.reject { |arg| arg == legacy_flag }] if legacy_flag
  return ["query", *args] unless args.empty? || %w[index index-new index-update query].include?(args.first)

  args
end

.run(argv = ARGV, index_class: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/index_util/cli.rb', line 33

def self.run(argv = ARGV, index_class: nil)
  if index_class
    run_for_class(index_class, argv)
  else
    start(normalize_legacy_args(argv))
  end
rescue StandardError => e
  warn JSON.generate(error: e.class.name, message: e.message)
  warn e.backtrace.join("\n") if argv.include?("--verbose") && e.backtrace
  exit 1
end

.run_for_class(index_class, argv) ⇒ Object



45
46
47
48
# File 'lib/index_util/cli.rb', line 45

def self.run_for_class(index_class, argv)
  command_argv = normalize_legacy_args(argv)
  start(command_argv, index_instance: index_class.new)
end

Instance Method Details

#indexObject



13
14
15
# File 'lib/index_util/cli.rb', line 13

def index
  index_instance.index_all!(progress: progress)
end

#index_newObject



18
19
20
# File 'lib/index_util/cli.rb', line 18

def index_new
  index_instance.index_new!(progress: progress)
end

#index_updateObject



23
24
25
# File 'lib/index_util/cli.rb', line 23

def index_update
  index_instance.index_update!(progress: progress)
end

#query(query) ⇒ Object



29
30
31
# File 'lib/index_util/cli.rb', line 29

def query(query)
  puts JSON.pretty_generate(index_instance.query(query, limit: options[:limit], verbose: options[:verbose]))
end