huqua 1.0.7 → 2.0.0

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: 05d2f6c8aa39b2115f46fcf4f0218f4ea54aaa6195853ee9e71aa979791371e7
4
- data.tar.gz: 6671fdce0afce7d6236a61ac3cd54af835a346dce9e212737bc223e7a79906e7
3
+ metadata.gz: 57910c1454358b08fe5b90fe1b244b1ef9391048939a59bec9a14df43ba78ad6
4
+ data.tar.gz: 5ae62872f43ddf4528eb8bbd7e507946d285c83964320428e0788d474da3aef9
5
5
  SHA512:
6
- metadata.gz: e06128d934285bcf58b40b730090542f8cb8b8a52055f97e8154e25467fb80c10381aecefdce4cf8ebed2a096103d0c3830d3be5ffa23a5dd3743abd5c5b14bf
7
- data.tar.gz: dc746c69778de380c32917d2c6b6b86fe0fef5a5a588643e5c519f0f16c6de8f3375483d428419e8caed883ea145f952445835a810291ac2a553a68b6470cade
6
+ metadata.gz: ca9852ae298f51870e81b05071786ba611e108a086e8e52690fd388d523b4e5d336cfcd24c6e778989833645a5089068589fb20712d0beff18bcdb1164f965a8
7
+ data.tar.gz: 964efe30f07fcaa8b20bfd6e00321b568e8071aa1743598da2bdf414628c565c372975093b3f320b59af34392e5ad786ca0f8d714f90ce7d12b9a94c8b21bb72
@@ -0,0 +1,24 @@
1
+ module DisplayDetail
2
+ def self.show(tables, tmp_arg)
3
+ if tmp_arg[1].to_i > 0
4
+ begin
5
+ term = "SELECT * FROM #{tmp_arg[0]} where id = #{tmp_arg[1].to_i}"
6
+ res = $conn.exec(term)
7
+ rescue StandardError => e
8
+ res = nil
9
+ end
10
+
11
+ if res
12
+ res.each { |row|
13
+ row.each do |key, value|
14
+ puts "#{key}:".yellow + " #{value}\n"
15
+ end
16
+ }
17
+ else
18
+ puts "Can not find record, something went wrong".red
19
+ end
20
+ else
21
+ puts "The second params need a integer!".red
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ module DisplayOverview
2
+ def self.show(tables)
3
+ if !tables.size.zero?
4
+ puts "You have #{tables.size} tables in your database:".green
5
+ tables.size.times do |n|
6
+ term = "SELECT count(id) FROM #{tables[n]}"
7
+ begin
8
+ res = $conn.exec(term)
9
+ rescue StandardError => e
10
+ res = nil
11
+ end
12
+
13
+ result = res ? "~> #{tables[n]} (#{res[0]['count']})".yellow : "Can not find #{tables[n]} in database".red
14
+ puts result
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,41 @@
1
+ module DisplayStructure
2
+ def self.show(tables, tmp_arg)
3
+ if(['-v', '--v', '-version', '--version', 'version'].include?(tmp_arg[0]))
4
+ puts "Your current version is: 2.0.0"
5
+ else
6
+ if(['-h', '--h', '-help', '--help', 'help'].include?(tmp_arg[0]))
7
+ DisplayStructure.show_help
8
+ else
9
+ DisplayStructure.show_data(tables, tmp_arg)
10
+ end
11
+ end
12
+ end
13
+
14
+ def self.show_help
15
+ puts "~> " + "Use " + "huqua".yellow + " to show overview of your database\n"
16
+ puts "~> " + "Use " + "huqua table_name".yellow + " to show the structure of this table\n"
17
+ puts "Ex: huqua users\n"
18
+ puts "~> " + "Use " + "huqua table_name id".yellow + " to show detail record by id\n"
19
+ puts "Ex: huqua users 2\n"
20
+ end
21
+
22
+ def self.show_data(tables, tmp_arg)
23
+ begin
24
+ term = "SELECT * FROM #{tmp_arg[0]} limit 1"
25
+ res = $conn.exec(term)
26
+ rescue StandardError => e
27
+ res = nil
28
+ end
29
+
30
+ if res
31
+ res.each { |row|
32
+ row.each do |key, value|
33
+ puts "#{key}".yellow
34
+ end
35
+ }
36
+ puts "\n ~> To see more details type: " + "huqua #{tmp_arg[0]} id".green + " (ex: huqua #{tmp_arg[0]} 1)\n\n"
37
+ else
38
+ puts "[warning]".yellow + "Can not find #{tmp_arg[0]} in database"
39
+ end
40
+ end
41
+ end
data/lib/huqua.rb CHANGED
@@ -1,161 +1,56 @@
1
1
  require 'pg'
2
2
  require 'yaml'
3
+ require_relative 'string'
4
+ require_relative 'read_schema'
5
+ require_relative 'display_overview'
6
+ require_relative 'display_structure'
7
+ require_relative 'display_detail'
3
8
 
4
- # make color for output messages
5
- class String
6
- def colorize(color_code)
7
- "\e[#{color_code}m#{self}\e[0m"
8
- end
9
-
10
- def red
11
- colorize(31)
12
- end
13
-
14
- def green
15
- colorize(32)
16
- end
17
-
18
- def yellow
19
- colorize(33)
20
- end
21
-
22
- def blue
23
- colorize(33)
24
- end
25
-
26
- def pink
27
- colorize(35)
28
- end
29
-
30
- def light_blue
31
- colorize(36)
32
- end
33
- end
34
-
35
- # read database.yml file
9
+ # Read database.yml
36
10
  begin
37
- config_info = YAML.load_file('config/database.yml')
11
+ config_info = YAML.load_file('config/database.yml')
38
12
 
39
- $username = config_info["development"]["username"] || ENV["USER"]
40
- $password = config_info["development"]["password"] || ''
41
- $database = config_info["development"]["database"]
13
+ $username = config_info["development"]["username"] || ENV["USER"]
14
+ $password = config_info["development"]["password"] || ''
15
+ $database = config_info["development"]["database"]
42
16
  rescue StandardError => e
43
- puts "Missing database.yml or config in this file is wrong".red
17
+ puts "Missing database.yml or config in this file is wrong".red
44
18
  end
45
19
 
46
- # start connect to database
20
+ # Connect to database
47
21
  begin
48
- $conn = PG::Connection.connect(
49
- :hostaddr => "127.0.0.1", :port=>5432,
50
- :dbname => $database,
51
- :user => $username,
52
- :password => $password
22
+ $conn = PG::Connection.connect(
23
+ :hostaddr => "127.0.0.1", :port=>5432,
24
+ :dbname => $database,
25
+ :user => $username,
26
+ :password => $password
53
27
  )
54
- puts "Congrats, you have good connection to database !\n".green
28
+ puts "Congrats, you have good connection to database!\n".green
55
29
  rescue StandardError => e
56
- puts "Can not connect to database...\n".red
30
+ puts "Can not connect to database...\n".red
57
31
  end
58
32
 
59
- # main class
33
+ # Main Class
60
34
  class Huqua
61
-
62
35
  def self.notification
63
- tables = []
64
- # no prams
65
- if ARGV.size.zero?
66
- # read schema.rb file, collect tables name.
67
- begin
68
- File.open("db/schema.rb", "r") do |f|
69
- f.each_line do |line|
70
- if line.include?("create_table")
71
- short_line = line.delete(' ')
72
- table_name = ""
73
- i = 13
74
- while short_line[i] != "'" && short_line[i] != '"'
75
- table_name << short_line[i].to_s
76
- i = i + 1
77
- end
78
- tables.push(table_name)
79
- end
80
- end
81
- rescue StandardError => e
82
- puts "Something is wrong, Missing schema.rb or syntax error.".red
83
- end
84
- end
85
-
86
- # read and show tables name and size data.
87
- if !tables.size.zero?
88
- puts "You have #{tables.size} tables in your database:".green
89
- tables.size.times do |n|
90
- term = "SELECT count(id) FROM #{tables[n]}"
91
- # get size table
92
- begin
93
- res = $conn.exec(term)
94
- rescue StandardError => e
95
- res = nil
96
- end
97
- # make color for fun
98
- if n % 2 == 0
99
- if res
100
- puts "- #{tables[n]} (#{res[0]['count']})".blue
101
- else
102
- puts "Can not find #{tables[n]} in database".red
103
- end
104
- else
105
- if res
106
- puts "- #{tables[n]} (#{res[0]['count']})".yellow
107
- else
108
- puts "Can not find #{tables[n]} in database".yellow
109
- end
110
- end
111
- end
112
- end
113
- end
114
-
115
- # params
116
- if !ARGV.size.zero?
117
- # 1 params
118
- if ARGV.size <= 2
119
- if ARGV.size == 1
120
- begin
121
- term = "SELECT * FROM #{ARGV[0]} limit 1"
122
- res = $conn.exec(term)
123
- rescue StandardError => e
124
- res = nil
125
- end
126
- if res
127
- res.each{ |row|
128
- puts "The first record:".red << "#{row}\n"
129
- }
130
- else
131
- puts "* Can not find #{tables[n].red} in database"
132
- end
133
- end
134
-
135
- # 2 params
136
- if ARGV.size == 2
137
- if ARGV[1].to_i > 0
138
- begin
139
- term = "SELECT * FROM #{ARGV[0]} where id = #{ARGV[1].to_i}"
140
- res = $conn.exec(term)
141
- rescue StandardError => e
142
- res = nil
143
- end
144
- if res
145
- res.each{ |row|
146
- puts "The first record:".red << "#{row}\n"
147
- }
148
- else
149
- puts "Can not find record, something is wrong".red
150
- end
151
- else
152
- puts "The second params need a integer".red
153
- end
154
- end
155
- else
156
- puts "The number of parameters is wrong".red
157
- end
158
- end
36
+ tables = []
37
+ if ARGV.size.zero?
38
+ ReadSchema.read_schema(tables)
39
+ DisplayOverview.show(tables)
40
+ else
41
+ tmp_arg = []
42
+ ARGV.size.times { |item| tmp_arg.push(ARGV[item]) }
43
+
44
+ if ARGV.size <= 2
45
+ if(ARGV.size == 1)
46
+ DisplayStructure.show(tables, tmp_arg)
47
+ end
48
+ if(ARGV.size == 2)
49
+ DisplayDetail.show(tables, tmp_arg)
50
+ end
51
+ else
52
+ puts "The number of arguments went wrong".red
53
+ end
54
+ end
159
55
  end
160
- # end main class
161
56
  end
@@ -0,0 +1,22 @@
1
+ module ReadSchema
2
+ def self.read_schema(tables)
3
+ begin
4
+ File.open("db/schema.rb", "r") do |f|
5
+ f.each_line do |line|
6
+ if line.include?("create_table")
7
+ short_line = line.delete(' ')
8
+ table_name = ""
9
+ i = 13
10
+ while short_line[i] != "'" && short_line[i] != '"'
11
+ table_name << short_line[i].to_s
12
+ i = i + 1
13
+ end
14
+ tables.push(table_name)
15
+ end
16
+ end
17
+ rescue StandardError => e
18
+ puts "Something went wrong, missing schema.rb or syntax error...".red
19
+ end
20
+ end
21
+ end
22
+ end
data/lib/string.rb ADDED
@@ -0,0 +1,29 @@
1
+ class String
2
+ def colorize(color_code)
3
+ "\e[#{color_code}m#{self}\e[0m"
4
+ end
5
+
6
+ def red
7
+ colorize(31)
8
+ end
9
+
10
+ def green
11
+ colorize(32)
12
+ end
13
+
14
+ def yellow
15
+ colorize(33)
16
+ end
17
+
18
+ def blue
19
+ colorize(33)
20
+ end
21
+
22
+ def pink
23
+ colorize(35)
24
+ end
25
+
26
+ def light_blue
27
+ colorize(36)
28
+ end
29
+ end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: huqua
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Duy Chinh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-18 00:00:00.000000000 Z
11
+ date: 2019-08-31 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: "[macOS only]A simple tool for checking postgresql database in development
14
- environment, life too short so make it easy. How to use? find out here: https://github.com/hdchinh/huqua"
13
+ description: A simple tool for checking postgresql database in development without
14
+ access to rails console
15
15
  email: hduychinh@gmail.com
16
16
  executables:
17
17
  - huqua
@@ -19,7 +19,12 @@ extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - bin/huqua
22
+ - lib/display_detail.rb
23
+ - lib/display_overview.rb
24
+ - lib/display_structure.rb
22
25
  - lib/huqua.rb
26
+ - lib/read_schema.rb
27
+ - lib/string.rb
23
28
  homepage: https://rubygems.org/gems/huqua
24
29
  licenses:
25
30
  - MIT
@@ -40,9 +45,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
45
  version: '0'
41
46
  requirements: []
42
47
  rubyforge_project:
43
- rubygems_version: 2.7.8
48
+ rubygems_version: 2.7.6
44
49
  signing_key:
45
50
  specification_version: 4
46
- summary: "[macOS only]A simple tool for checking postgresql database in development
47
- environment in macOS"
51
+ summary: A simple tool for checking postgresql database in development without access
52
+ to rails console
48
53
  test_files: []