mycmd 0.0.4 → 0.0.5

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
  SHA1:
3
- metadata.gz: 4032fa8d14c8bef5767ba0e34e6a43ca9102a5f2
4
- data.tar.gz: 0bdfd6067cadebe7fdb73ce724566bd264d8256e
3
+ metadata.gz: ca8366a87ff73ce76f56bdeadaea36a3b5fa79d2
4
+ data.tar.gz: cfc34ad1fca34a425ca10c2dcdbaeec68dfc11be
5
5
  SHA512:
6
- metadata.gz: 0203fe3e2003fdeb3f210e59b8a0c37887f2322beeb507aa801e8724029279db7af1fe396a80f04372df02c43d869b33ef622b2fab3801d81f5d155d847668ee
7
- data.tar.gz: 9057067f77ee44feb682d5198a022dbf38cc850a4f17729cf78294878c990efbe7ef2c58068fb1aa014d1f33d54f7ae92558bbef0eff0c5a8c825d53ba385f8c
6
+ metadata.gz: 961aad1654773f7697114fea426ca74b7bb46943c15bc951b9bb008fbec557a6359249d4292320688e565093c792126e6ed41d17b88fee55fcbcdefbe1a0349a
7
+ data.tar.gz: 1667d1e50b020bf2fe045a722c1e206e52f54b299a181532aafff52ce013835aa36cc38391bbe8a740c0cbc054c5e035c3326b9e88bddc0d0e41948e0db70168
data/README.md CHANGED
@@ -21,6 +21,10 @@ Or install it yourself as:
21
21
 
22
22
  $ gem install mycmd
23
23
 
24
+ Or clone from Github:
25
+
26
+ $ git clone https://github.com/i2bskn/mycmd.git
27
+
24
28
  ## Settings
25
29
 
26
30
  Create settings file:
data/bin/mycmd CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
  # coding: utf-8
3
3
 
4
- $:.unshift(File.expand_path("../../lib", __FILE__))
4
+ path = File.expand_path("../../lib", __FILE__)
5
+ $:.unshift(path) unless $:.include?(path)
5
6
  require "mycmd"
6
7
 
7
- Mycmd::CLI.start
8
+ Mycmd::CLI.start
@@ -5,4 +5,5 @@ require "mysql2"
5
5
  require "mycmd/version"
6
6
  require "mycmd/configuration"
7
7
  require "mycmd/printer"
8
+ require "mycmd/client"
8
9
  require "mycmd/cli"
@@ -30,17 +30,12 @@ module Mycmd
30
30
 
31
31
  desc 'query "[SQL]"', "query will execute sql."
32
32
  def query(sql)
33
- client = Configuration.connect
34
- printer = Printer.new(client.query(sql), true)
35
- printer.print
33
+ Client.query(sql).print
36
34
  end
37
35
 
38
- desc 'tasks [TASK NAME]', "tasks will execute register sql."
36
+ desc 'tasks [TASK NAME]p', "tasks will execute register sql."
39
37
  def tasks(task)
40
- config = Configuration.new
41
- client = config.connect
42
- printer = Printer.new(client.query(config.tasks[task.to_s]), true)
43
- printer.print
38
+ Client.execute_task(task).print
44
39
  end
45
40
  end
46
41
  end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+
3
+ module Mycmd
4
+ class Client
5
+ def initialize
6
+ @configuration = Configuration.new
7
+ @connection = @configuration.connect
8
+ end
9
+
10
+ def query(sql)
11
+ @result = @connection.query(sql)
12
+ self
13
+ end
14
+
15
+ def execute_task(task)
16
+ @result = @connection.query(@configuration.tasks[task.to_s])
17
+ self
18
+ end
19
+
20
+ def print(header=true)
21
+ Printer.new(@result, header).print
22
+ end
23
+
24
+ class << self
25
+ def method_missing(action, *args)
26
+ client = self.new
27
+ if client.respond_to? action
28
+ client.send(action, *args)
29
+ else
30
+ super
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -12,9 +12,7 @@ module Mycmd
12
12
  else
13
13
  sql = "SELECT T.TABLE_NAME, CAST(((T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = '#{options['database']}' UNION SELECT 'all_tables', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = '#{options['database']}'"
14
14
  end
15
- client = Configuration.connect
16
- printer = Printer.new(client.query(sql), true)
17
- printer.print
15
+ Client.query(sql).print
18
16
  end
19
17
 
20
18
  desc "qcache_hit_rate", "qcache_hit_rate will print query cache hit rate"
@@ -6,8 +6,13 @@ module Mycmd
6
6
  attr_accessor :result, :header, :width
7
7
 
8
8
  def initialize(result, header=false)
9
- @result = result.is_a?(Mysql2::Result) ? result_to_array(result) : result
10
- @header = result.is_a?(Mysql2::Result) ? result.fields : header
9
+ @header = header
10
+ if result.is_a? Mysql2::Result
11
+ @result = result_to_array(result)
12
+ @header = result.fields if @header
13
+ else
14
+ @result = result
15
+ end
11
16
  end
12
17
 
13
18
  def print
@@ -1,3 +1,3 @@
1
1
  module Mycmd
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,6 +1,8 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Mycmd::CLI do
4
+ let(:client) {double("client mock").as_null_object}
5
+
4
6
  let(:conn_mock) {double("connection mock").as_null_object}
5
7
  let(:printer_mock) {double("printer mock").as_null_object}
6
8
 
@@ -31,10 +33,7 @@ describe Mycmd::CLI do
31
33
  end
32
34
 
33
35
  describe "#query" do
34
- before do
35
- Mycmd::Configuration.stub(:connect).and_return(conn_mock)
36
- Mycmd::Printer.stub(:new).and_return(printer_mock)
37
- end
36
+ before {Mycmd::Client.stub(:query).and_return(client)}
38
37
 
39
38
  after do
40
39
  expect {
@@ -42,48 +41,30 @@ describe Mycmd::CLI do
42
41
  }.not_to raise_error
43
42
  end
44
43
 
45
- it "should call Configuration.connect" do
46
- Mycmd::Configuration.should_receive(:connect).and_return(conn_mock)
44
+ it "should call Client.#query" do
45
+ Mycmd::Client.should_receive(:query).and_return(client)
47
46
  end
48
47
 
49
- it "should create Printer object" do
50
- Mycmd::Printer.should_receive(:new).with(conn_mock, true).and_return(printer_mock)
51
- end
52
-
53
- it "should call Printer#print" do
54
- printer_mock.should_receive(:print)
48
+ it "should call Client#print" do
49
+ client.should_receive(:print)
55
50
  end
56
51
  end
57
52
 
58
53
  describe "#tasks" do
59
- let(:config_mock) {double("configuration mock").as_null_object}
60
-
61
- before do
62
- config_mock.stub(:connect).and_return(conn_mock)
63
- Mycmd::Configuration.stub(:new).and_return(config_mock)
64
- Mycmd::Printer.stub(:new).and_return(printer_mock)
65
- end
54
+ before {Mycmd::Client.stub(:execute_task).and_return(client)}
66
55
 
67
56
  after do
68
57
  expect {
69
- Mycmd::CLI.start(["tasks", "some task"])
58
+ Mycmd::CLI.start(["tasks", "some_task"])
70
59
  }.not_to raise_error
71
60
  end
72
61
 
73
- it "should create Configuration object" do
74
- Mycmd::Configuration.should_receive(:new).and_return(config_mock)
75
- end
76
-
77
- it "should call Configuration#connect" do
78
- config_mock.should_receive(:connect).and_return(conn_mock)
79
- end
80
-
81
- it "should create Printer object" do
82
- Mycmd::Printer.should_receive(:new).with(conn_mock, true).and_return(printer_mock)
62
+ it "should call Client.#execute_task" do
63
+ Mycmd::Client.should_receive(:execute_task).and_return(client)
83
64
  end
84
65
 
85
- it "should call Printer#print" do
86
- printer_mock.should_receive(:print)
66
+ it "should call Client#print" do
67
+ client.should_receive(:print)
87
68
  end
88
69
  end
89
70
  end
@@ -0,0 +1,107 @@
1
+ require "spec_helper"
2
+
3
+ describe Mycmd::Client do
4
+ let(:client) {Mycmd::Client.new}
5
+ let(:connection) {double("Mysql2::Client Mock").as_null_object}
6
+ let(:config) {double("Mycmd::Configuration Mock").as_null_object}
7
+
8
+ describe "#initialize" do
9
+ it "should generate Configuration object" do
10
+ Mycmd::Configuration.should_receive(:new).and_return(config)
11
+ Mycmd::Client.new
12
+ end
13
+
14
+ it "should call Configuration#connect" do
15
+ config.should_receive(:connect)
16
+ Mycmd::Configuration.should_receive(:new).and_return(config)
17
+ Mycmd::Client.new
18
+ end
19
+ end
20
+
21
+ describe "#query" do
22
+ before do
23
+ config.stub(:connect).and_return(connection)
24
+ Mycmd::Configuration.stub(:new).and_return(config)
25
+ end
26
+
27
+ it "should call Mysql2::Client#query" do
28
+ connection.should_receive(:query)
29
+ client.query("some sql")
30
+ end
31
+
32
+ it "should set result" do
33
+ connection.should_receive(:query).and_return("result")
34
+ client.query("some sql")
35
+ expect(client.instance_eval{@result}).not_to be_nil
36
+ end
37
+
38
+ it "returns Mycmd::Client object" do
39
+ expect(client.query("some sql").is_a? Mycmd::Client).to be_true
40
+ end
41
+ end
42
+
43
+ describe "#execute_task" do
44
+ before do
45
+ config.stub(:connect).and_return(connection)
46
+ Mycmd::Configuration.stub(:new).and_return(config)
47
+ end
48
+
49
+ it "should call Mysql2::Client#query" do
50
+ connection.should_receive(:query)
51
+ client.execute_task("some_task")
52
+ end
53
+
54
+ it "should set result" do
55
+ connection.should_receive(:query).and_return("result")
56
+ client.execute_task("some_task")
57
+ expect(client.instance_eval{@result}).not_to be_nil
58
+ end
59
+
60
+ it "returns Mycmd::Client object" do
61
+ expect(client.execute_task("some_task").is_a? Mycmd::Client).to be_true
62
+ end
63
+ end
64
+
65
+ describe "#print" do
66
+ let(:printer) {double("Mycmd::Printer Mock").as_null_object}
67
+
68
+ before do
69
+ config.stub(:connect).and_return(connection)
70
+ Mycmd::Configuration.stub(:new).and_return(config)
71
+ end
72
+
73
+ it "should create Mycmd::Printer object" do
74
+ Mycmd::Printer.should_receive(:new).and_return(printer)
75
+ client.print
76
+ end
77
+
78
+ it "should call Mycmd::Printer#print" do
79
+ printer.should_receive(:print)
80
+ Mycmd::Printer.stub(:new).and_return(printer)
81
+ client.print
82
+ end
83
+ end
84
+
85
+ describe ".#method_missing" do
86
+ before do
87
+ connection.stub(:query)
88
+ config.stub(:connect).and_return(connection)
89
+ Mycmd::Configuration.stub(:new).and_return(config)
90
+ end
91
+
92
+ context "with known method" do
93
+ it "should call Mycmd::Client#query" do
94
+ Mycmd::Client.any_instance.should_receive(:query)
95
+ Mycmd::Client.query("some sql")
96
+ end
97
+ end
98
+
99
+ context "with unknown method" do
100
+ it "should generate exception" do
101
+ expect {
102
+ Mycmd::Client.unknown_method
103
+ }.to raise_error
104
+ end
105
+ end
106
+ end
107
+ end
@@ -4,38 +4,38 @@ describe Mycmd::StatusCommands do
4
4
  let(:conn_mock) {double("connection mock")}
5
5
 
6
6
  describe "#size" do
7
- let(:ptr_mock) {double("printer mock").as_null_object}
8
-
9
- before do
10
- conn_mock.stub(:query).and_return(create_result)
11
- Mycmd::Configuration.stub(:connect).and_return(conn_mock)
12
- Mycmd::Printer.stub(:new).and_return(ptr_mock)
13
- end
7
+ let(:client) {double("Mycmd::Client Mock").as_null_object}
14
8
 
15
9
  context "with not arguments" do
16
10
  let(:args) {["size"]}
11
+ let(:sql) {"SELECT T.TABLE_SCHEMA, CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T GROUP BY T.TABLE_SCHEMA UNION SELECT 'all_databases', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T"}
12
+
17
13
  after {Mycmd::StatusCommands.start(args)}
18
14
 
19
- it "should call Configuration.#connect" do
20
- Mycmd::Configuration.should_receive(:connect).and_return(conn_mock)
15
+ it "should call Mycmd::Client.#query" do
16
+ Mycmd::Client.should_receive(:query).with(sql).and_return(client)
21
17
  end
22
18
 
23
- it "should create Printer object" do
24
- Mycmd::Printer.should_receive(:new).and_return(ptr_mock)
19
+ it "should call Mycmd::Client#print" do
20
+ client.should_receive(:print)
21
+ Mycmd::Client.stub(:query).and_return(client)
25
22
  end
23
+ end
26
24
 
27
- it "should call Printer#print" do
28
- ptr_mock.should_receive(:print)
29
- end
25
+ context "with arguments" do
26
+ let(:args) {["size", "-d", "some_db"]}
27
+ let(:sql) {"SELECT T.TABLE_NAME, CAST(((T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = 'some_db' UNION SELECT 'all_tables', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = 'some_db'"}
28
+
29
+ after {Mycmd::StatusCommands.start(args)}
30
30
 
31
- it "should output the size of all databases" do
32
- conn_mock.should_receive(:query).with("SELECT T.TABLE_SCHEMA, CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T GROUP BY T.TABLE_SCHEMA UNION SELECT 'all_databases', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T")
31
+ it "should call Mycmd::Client.#query" do
32
+ Mycmd::Client.should_receive(:query).with(sql).and_return(client)
33
33
  end
34
- end
35
34
 
36
- it "should output the size of all tables" do
37
- conn_mock.should_receive(:query).with("SELECT T.TABLE_NAME, CAST(((T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = 'some_db' UNION SELECT 'all_tables', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = 'some_db'")
38
- Mycmd::StatusCommands.start(["size", "-d", "some_db"])
35
+ it "should call Mycmd::Client#print" do
36
+ client.should_receive(:print)
37
+ Mycmd::Client.stub(:query).and_return(client)
38
+ end
39
39
  end
40
40
  end
41
41
 
@@ -5,23 +5,36 @@ describe Mycmd::Printer do
5
5
  let(:printer) {Mycmd::Printer.new(result)}
6
6
 
7
7
  describe "#initialize" do
8
- it "should set specified result" do
9
- expect(printer.result).to eq(result)
10
- end
8
+ context "without header" do
9
+ let(:printer) {Mycmd::Printer.new(result)}
11
10
 
12
- it "should set default header flug" do
13
- expect(printer.header).to be_false
14
- end
11
+ it "should set specified result" do
12
+ expect(printer.result).to eq(result)
13
+ end
15
14
 
16
- it "should set specified header flug" do
17
- printer = Mycmd::Printer.new(result, true)
18
- expect(printer.header).to be_true
15
+ it "should set default header flug" do
16
+ expect(printer.header).to be_false
17
+ end
19
18
  end
20
19
 
21
- it "should generate exception if not specified arguments" do
22
- expect {
23
- Mycmd::Printer.new
24
- }.to raise_error
20
+ context "with header" do
21
+ let(:printer) {Mycmd::Printer.new(result, true)}
22
+
23
+ it "should set specified header flug" do
24
+ expect(printer.header).to be_true
25
+ end
26
+
27
+ it "should call Printer#result_to_array" do
28
+ result.should_receive(:is_a?).and_return(true)
29
+ Mycmd::Printer.any_instance.should_receive(:result_to_array).and_return(result)
30
+ printer
31
+ end
32
+
33
+ it "should call result#fields if header is true" do
34
+ result.should_receive(:is_a?).and_return(true)
35
+ result.should_receive(:fields)
36
+ printer
37
+ end
25
38
  end
26
39
  end
27
40
 
@@ -20,4 +20,4 @@ Dir[support_files].each {|f| require f}
20
20
  RSpec.configure do |config|
21
21
  config.order = "random"
22
22
  config.include(SpecUtils)
23
- end
23
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mycmd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - i2bskn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-12 00:00:00.000000000 Z
11
+ date: 2013-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -98,6 +98,7 @@ files:
98
98
  - bin/mycmd
99
99
  - lib/mycmd.rb
100
100
  - lib/mycmd/cli.rb
101
+ - lib/mycmd/client.rb
101
102
  - lib/mycmd/clis/config_commands.rb
102
103
  - lib/mycmd/clis/settings_commands.rb
103
104
  - lib/mycmd/clis/status_commands.rb
@@ -106,6 +107,7 @@ files:
106
107
  - lib/mycmd/version.rb
107
108
  - mycmd.gemspec
108
109
  - spec/mycmd/cli_spec.rb
110
+ - spec/mycmd/client_spec.rb
109
111
  - spec/mycmd/clis/config_commands_spec.rb
110
112
  - spec/mycmd/clis/settings_commands_spec.rb
111
113
  - spec/mycmd/clis/status_commands_spec.rb
@@ -133,12 +135,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
135
  version: '0'
134
136
  requirements: []
135
137
  rubyforge_project:
136
- rubygems_version: 2.0.6
138
+ rubygems_version: 2.1.5
137
139
  signing_key:
138
140
  specification_version: 4
139
141
  summary: MySQL command line tool
140
142
  test_files:
141
143
  - spec/mycmd/cli_spec.rb
144
+ - spec/mycmd/client_spec.rb
142
145
  - spec/mycmd/clis/config_commands_spec.rb
143
146
  - spec/mycmd/clis/settings_commands_spec.rb
144
147
  - spec/mycmd/clis/status_commands_spec.rb