Class: Rubydex::CLI::Command::Query

Inherits:
Command
  • Object
show all
Defined in:
lib/rubydex/cli/command/query.rb

Instance Method Summary collapse

Instance Method Details

#runObject

: -> void



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
# File 'lib/rubydex/cli/command/query.rb', line 20

def run
  schema = false
  format = "table"

  parse_options!(options: true) do |parser|
    parser.on("--schema", "Describe the queryable schema instead of running a query") { schema = true }
    parser.on("--format FORMAT", ["table", "json"], "Output format (table or json)") do |value|
      format = value
    end
  end

  query = argv.shift

  if schema
    warn("warning: ignoring query argument because `--schema` was given") if query
    # The schema is static, so describe it without building a graph.

    print(Rubydex::Query.schema(format))
    return
  end

  if query.nil? || query.empty?
    abort_with_usage("`query` requires a Cypher query argument (or pass `--schema`)")
  end

  # Parse the query up front so a malformed query fails fast, before the expensive indexing.

  parsed = parse_query(query)

  # Progress goes to stderr so stdout carries only the result (e.g. for piping a query's JSON).

  graph = build_graph($stderr)

  begin
    result = parsed.run(graph)
  rescue Rubydex::QueryExecutionError => e
    abort(e.message)
  end

  print(result.render(format))
end