druid_client 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a81c003d77d8da968f48e5ff8702892b98449819b5629f0eb83fb34e7930cdd
4
- data.tar.gz: 882564c48dd0551757b273c4f2e703d922b64574a61ab4ff9102657beca915e0
3
+ metadata.gz: d73b3aacd3fd035ffa9c8d172ca2f185c274b09ade9d7e859cb45d3d5132cb37
4
+ data.tar.gz: 116b09541bf93686a6d305ed4e21e643113f3a0273428eb702314cb95d106f79
5
5
  SHA512:
6
- metadata.gz: e723a1dc81da695bde0987398597105f75aa878ae21d6c22c4406ccf09f3abfe4e51f4dfe350fa13c72e9a0641df7e5a1aa6044195c1b2c83fa1ab8d98564065
7
- data.tar.gz: 91660b9d43dcbe92b9d4ea38c963566b8dd36358705b532974a7fb0186f6eea8be9a0be6bc58fd7f3185b739359004dc39d8b8caeb855ad5b24cde2ce4149b72
6
+ metadata.gz: c4de4aaa5b97488d401b249ec2d0ac44c9cbb18aea557967ec53dce890ca092ae0385c6b11eeef54d026ed214746f18a451779793a8c88cc11981ce56cb2c6b1
7
+ data.tar.gz: 40368bd913861a91db6a915cb96a623f328d5fb002d9ef66e5407796eea072d7439212fd6acd412a79bd2e70a220f5e08ab53b9c4d389d8e40edce814ae52aef
data/exe/druid_client ADDED
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'gli'
5
+ require 'druid_client'
6
+ require 'druid_client/cli'
7
+
8
+ class DruidClientCli
9
+ extend GLI::App
10
+
11
+ program_desc 'Druid client'
12
+ version DruidClient::VERSION
13
+
14
+ subcommand_option_handling :normal
15
+ arguments :strict
16
+
17
+ desc 'Druid coordinator URL'
18
+ flag %i[url U], default_value: 'http://localhost:8081'
19
+
20
+ desc 'Username for basic authentication'
21
+ flag %i[username u]
22
+
23
+ desc 'Password for basic authentication'
24
+ flag %i[password p]
25
+
26
+ desc 'Output format (table, json)'
27
+ flag [:F, :format],
28
+ default_value: 'table',
29
+ must_match: %w[table json]
30
+
31
+ desc 'Submit query to Druid'
32
+ command :query do |q|
33
+ q.desc 'Submit SQL query'
34
+ q.arg_name 'sql_query', 'The SQL query to execute'
35
+ q.command :sql do |sql|
36
+
37
+ desc 'Output format (table, json)'
38
+ sql.flag [:F, :format],
39
+ default_value: 'table',
40
+ must_match: %w[table json]
41
+
42
+ sql.action do |global_options, options, args|
43
+ DruidClient::Cli.new(options: global_options)
44
+ .sql
45
+ .query(query: args.join(' ').strip,
46
+ options: options)
47
+ end
48
+ end
49
+ end
50
+
51
+ pre do |_global, _command, _options, _args|
52
+ true
53
+ end
54
+
55
+ post do |_global, _command, _options, _args|
56
+ true
57
+ end
58
+
59
+ on_error do |_exception|
60
+ true
61
+ end
62
+ end
63
+
64
+ exit DruidClientCli.run(ARGV)
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ require_relative 'output_format'
6
+
7
+ module DruidClient
8
+ class CLi
9
+ class JsonFormat
10
+ include OutputFormat
11
+
12
+ def format(data)
13
+ JSON.pretty_generate(data)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DruidClient
4
+ class CLi
5
+ module OutputFormat
6
+ def format(data)
7
+ raise NotImplementedError, "Subclasses must implement #format method"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'table_format'
4
+ require_relative 'json_format'
5
+
6
+ module DruidClient
7
+ class CLi
8
+ class OutputFormatFactory
9
+ puts self
10
+ FORMATS = {
11
+ table: TableFormat,
12
+ json: JsonFormat,
13
+ }.freeze
14
+
15
+ def self.create(format_name)
16
+ format_class = FORMATS[format_name.to_sym]
17
+ raise "Unknown format: #{format_name}" unless format_class
18
+ format_class.new
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'output_format_factory'
4
+
5
+ module DruidClient
6
+ class Cli
7
+ class Sql
8
+ def initialize(parent: nil)
9
+ @parent = parent
10
+ end
11
+
12
+ def query(query:, options: {})
13
+ query_body = { query: query }
14
+ query_response = client.sql.query(
15
+ **query_body
16
+ )
17
+
18
+ unless query_response.success?
19
+ display_error(
20
+ status_code: query_response.status_code,
21
+ error_message: query_response.body
22
+ )
23
+ return
24
+ end
25
+
26
+ formater = DruidClient::CLi::OutputFormatFactory.create(options.fetch(:format, :table))
27
+ puts formater.format(query_response.body)
28
+ end
29
+
30
+ def display_error(status_code: , error_message: )
31
+ puts "failed: status_code:#{status_code}, message:#{error_message}"
32
+ end
33
+
34
+ def client
35
+ @parent.client
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'terminal-table'
4
+
5
+ require_relative 'output_format'
6
+
7
+ module DruidClient
8
+ class CLi
9
+ class TableFormat
10
+ include OutputFormat
11
+
12
+ def format(data)
13
+ table = Terminal::Table.new do |t|
14
+ headings = data.first.keys
15
+ t.headings = headings
16
+ data[1..-1].each do |row|
17
+ t << headings.map { |heading| row[heading] }
18
+ end
19
+ end
20
+ table.to_s
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api'
4
+ require_relative 'cli/sql'
5
+
6
+ module DruidClient
7
+ class Cli
8
+ def initialize(options: {})
9
+ @options = options
10
+ end
11
+
12
+ def sql
13
+ @sql ||= Cli::Sql.new(parent: self)
14
+ end
15
+
16
+ def client
17
+ @client ||= DruidClient::Api.new(
18
+ url: @options[:url],
19
+ username: @options[:username],
20
+ password: @options[:password],
21
+ )
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DruidClient
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.3'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: druid_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Tych
@@ -230,13 +230,15 @@ dependencies:
230
230
  description: Apache Druid DB client
231
231
  email:
232
232
  - thomas.tych@gmail.com
233
- executables: []
233
+ executables:
234
+ - druid_client
234
235
  extensions: []
235
236
  extra_rdoc_files: []
236
237
  files:
237
238
  - LICENSE.txt
238
239
  - README.md
239
240
  - Rakefile
241
+ - exe/druid_client
240
242
  - extra/fake_druid_api.rb
241
243
  - lib/druid_client.rb
242
244
  - lib/druid_client/api.rb
@@ -245,6 +247,12 @@ files:
245
247
  - lib/druid_client/api/response.rb
246
248
  - lib/druid_client/api/rest_client.rb
247
249
  - lib/druid_client/api/sql.rb
250
+ - lib/druid_client/cli.rb
251
+ - lib/druid_client/cli/json_format.rb
252
+ - lib/druid_client/cli/output_format.rb
253
+ - lib/druid_client/cli/output_format_factory.rb
254
+ - lib/druid_client/cli/sql.rb
255
+ - lib/druid_client/cli/table_format.rb
248
256
  - lib/druid_client/druid_config.rb
249
257
  - lib/druid_client/error.rb
250
258
  - lib/druid_client/version.rb