Class: SQLiteSweep::CLI

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

Overview

Command-line interface. Parses ARGV into a Config and launches the Runner.

Designed for pipe-friendly usage: results go to stdout, progress/errors go to stderr. This means you can do:

sqlitesweep -q "SELECT count(*) FROM users" -a sum -s "cat uris.txt" > result.txt

Class Method Summary collapse

Class Method Details

.run(argv) ⇒ void

This method returns an undefined value.

Parses command-line arguments and runs the sweep.

Parameters:

  • argv (Array<String>)

    Command-line arguments (typically ARGV).



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sqlitesweep/cli.rb', line 16

def self.run(argv)
  options = {}
  parser = OptionParser.new do |opts|
    opts.banner = "Usage: sqlitesweep [options]"

    opts.on("-q", "--query QUERY", "SQL query to execute on each database (required)") do |v|
      options[:query] = v
    end

    opts.on("-a", "--action ACTION", "sum, average/avg, or list (default: sum)") do |v|
      options[:action] = v
    end

    opts.on("-s", "--source COMMAND", "Shell command that outputs database URIs (required)") do |v|
      options[:source] = v
    end

    opts.on("-c", "--concurrency N", Integer, "Max parallel query workers (default: 8)") do |v|
      options[:concurrency] = v
    end

    opts.on("--max-ssh N", Integer, "Max SSH master connections (default: 50)") do |v|
      options[:max_ssh] = v
    end

    opts.on("--no-live", "Disable live progress display") do
      options[:live] = false
    end

    opts.on("--batch-size N", Integer, "Databases to query per SSH call (default: 4)") do |v|
      options[:batch_size] = v
    end

    opts.on("--ssh-timeout N", Integer, "SSH connect timeout in seconds (default: 10)") do |v|
      options[:ssh_timeout] = v
    end

    opts.on("--query-timeout N", Integer, "Per-query timeout in seconds (default: 30)") do |v|
      options[:query_timeout] = v
    end

    opts.on("-h", "--help", "Show this help") do
      $stderr.puts opts
      exit 0
    end

    opts.on("--version", "Show version") do
      puts "sqlitesweep #{VERSION}"
      exit 0
    end
  end

  parser.parse!(argv)

  unless options[:query]
    $stderr.puts "Error: --query is required"
    $stderr.puts parser
    exit 1
  end

  unless options[:source]
    $stderr.puts "Error: --source is required"
    $stderr.puts parser
    exit 1
  end

  config = Config.new(**options)
  Runner.new(config).run
rescue ConfigError => e
  $stderr.puts "Error: #{e.message}"
  exit 1
end