open_geo_db 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
- require "mixlib/cli"
2
1
  require "yaml"
2
+ require "mixlib/cli"
3
+ require "open_geo_db/database"
3
4
 
4
5
  module OpenGeoDb
5
6
  class CLI
@@ -22,43 +23,23 @@ module OpenGeoDb
22
23
 
23
24
  def run
24
25
  parse_options
25
- send(config.keys.first)
26
- end
27
-
28
- private
29
-
30
- def load_config
31
- config_file = config.values.first
32
- puts("Using config file #{config_file}")
33
- yaml = YAML.load_file(config_file)["open_geo_db"]
34
- @database = yaml["database"]
35
- @username = yaml["username"]
36
- @password = "-p#{yaml["password"]}" if yaml["password"] and yaml["password"].any?
37
- end
38
-
39
- def create
40
- load_config
41
- sh("mysqladmin -u#{@username} #{@password} create #{@database}")
42
- %w(opengeodb-begin DE DEhier AT AThier CH CHhier opengeodb-end).each do |basename|
43
- file = File.join(File.dirname(__FILE__), %w(.. .. vendor sql), "#{basename}.sql")
44
- sh("mysql -u#{@username} #{@password} #{@database} < #{file}")
26
+ action = config.keys.first
27
+ @config_file = config.values.first
28
+ if action == :generate
29
+ generate
30
+ else
31
+ puts("Using config file #{@config_file}")
32
+ database = OpenGeoDb::Database.new(YAML.load_file(@config_file)["open_geo_db"])
33
+ database.execute(action)
45
34
  end
46
35
  end
47
36
 
48
- def destroy
49
- load_config
50
- sh("mysqladmin -u#{@username} #{@password} drop -f #{@database}")
51
- end
37
+ private
52
38
 
53
39
  def generate
54
- config_file = config.values.first
55
- puts("Writing config to #{config_file}")
56
- File.open(config_file, "w") { |f| f.write(DEFAULT_CONFIG.to_yaml) }
57
- end
58
-
59
- def sh(command)
60
- puts(command)
61
- %x{#{command}}
40
+ @config_file = config.values.first
41
+ puts("Writing config to #{@config_file}")
42
+ File.open(@config_file, "w") { |f| f.write(DEFAULT_CONFIG.to_yaml) }
62
43
  end
63
44
  end
64
45
  end
@@ -0,0 +1,40 @@
1
+ module OpenGeoDb
2
+ class Database
3
+ class UnknownActionError < ArgumentError
4
+ end
5
+
6
+ def initialize(config)
7
+ @database = config["database"]
8
+ @username = config["username"]
9
+ @password = "-p#{config["password"]}" if config["password"] and config["password"].any?
10
+ end
11
+
12
+ def execute(action)
13
+ case action
14
+ when :create then create
15
+ when :destroy then destroy
16
+ else
17
+ raise UnknownActionError, action
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def create
24
+ sh("mysqladmin -u#{@username} #{@password} create #{@database}")
25
+ %w(opengeodb-begin DE DEhier AT AThier CH CHhier opengeodb-end).each do |basename|
26
+ file = File.join(File.dirname(__FILE__), %w(.. .. vendor sql), "#{basename}.sql")
27
+ sh("mysql -u#{@username} #{@password} #{@database} < #{file}")
28
+ end
29
+ end
30
+
31
+ def destroy
32
+ sh("mysqladmin -u#{@username} #{@password} drop -f #{@database}")
33
+ end
34
+
35
+ def sh(command)
36
+ puts(command)
37
+ %x{#{command}}
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module OpenGeoDb
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_geo_db
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kostiantyn Kahanskyi
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-12 00:00:00 Z
18
+ date: 2012-01-19 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: mixlib-cli
@@ -65,6 +65,7 @@ files:
65
65
  - bin/open_geo_db
66
66
  - lib/open_geo_db.rb
67
67
  - lib/open_geo_db/cli.rb
68
+ - lib/open_geo_db/database.rb
68
69
  - lib/open_geo_db/version.rb
69
70
  - open_geo_db.gemspec
70
71
  - vendor/sql/.gitkeep