txpadmin 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/bin/txpadmin +29 -16
  2. data/lib/txp_admin.rb +57 -7
  3. data/txpadmin.gemspec +1 -1
  4. metadata +1 -1
data/bin/txpadmin CHANGED
@@ -11,30 +11,36 @@ include TxpAdmin
11
11
 
12
12
  options = {}
13
13
 
14
- op = OptionParser.new do |opts|
14
+ op = OptionParser.new() do |opts|
15
15
  opts.banner = "Usage: "
16
16
 
17
- opts.on("-h", "--help", "Print this message") do |v|
17
+ opts.on("-h", "--help", "Print this message") do
18
18
  puts opts
19
19
  exit
20
20
  end
21
21
 
22
- opts.on("-V", "--version", "Print version") do |v|
22
+ opts.on("-V", "--version", "Print version") do
23
23
  puts "TxpAdmin gem version #{Gem.searcher.find('txp_admin').version.to_s}"
24
24
  exit
25
25
  end
26
26
 
27
- opts.on(nil, "--dbconsole", "Connect to mysql database") do |v|
28
- config = TxpAdmin::Config.new.options
29
- password = config[:pass].strip != "" ? " -p#{config[:pass]}" : ""
30
-
31
- # FIXME: how do a mysql alias evaluated in exec?
32
- # Example: mysql='mysql --socket=/Applications/xampp/xamppfiles/var/mysql/mysql.sock'
33
- config[:host] = config[:host] == "localhost" ? "127.0.0.1" : config[:host]
34
-
35
- commandline = "mysql -u#{config[:user]} #{password} -h#{config[:host]} #{config[:db]}"
36
- puts "executing command: #{commandline}"
37
- exec(commandline)
27
+ opts.on("--backup", "Backup database. Creates a zipped dump in the current directory.") do
28
+ TxpAdmin::MysqlConnect.backup
29
+ end
30
+
31
+ opts.on("--restore FILE_PATH", String, "Restore FILE_PATH dump into database.") do |file_path|
32
+ puts "Importing a dump. Enter to continue..."
33
+ input = gets("\n").strip
34
+ TxpAdmin::MysqlConnect.restore(file_path)
35
+ end
36
+
37
+ opts.on("--dbconsole", "Connect to mysql database.") do
38
+ TxpAdmin::MysqlConnect.console
39
+ exit
40
+ end
41
+
42
+ opts.on("--purge-logs", "Remove logs from txplogs.") do
43
+ TxpAdmin::MysqlConnect.execute "delete from txp_log"
38
44
  exit
39
45
  end
40
46
 
@@ -53,11 +59,18 @@ args = ARGV
53
59
  args = ["-h"] if ARGV.size == 0
54
60
 
55
61
  begin
56
- op.parse(args)
62
+ op.parse!(args)
57
63
  rescue OptionParser::AmbiguousOption => e
58
- op.parse(["-h"])
64
+ op.parse!(["-h"])
59
65
  rescue TxpAdmin::ConfigNotFound => e
60
66
  puts "Config file not found"
67
+ rescue TxpAdmin::RestoreFileNotFound => e
68
+ puts "Restore file not found"
69
+ rescue OptionParser::MissingArgument => e
70
+ puts "Missing argument"
71
+ op.parse!(["-h"])
72
+ rescue Interrupt => e
73
+ puts "Interrupted"
61
74
  end
62
75
 
63
76
 
data/lib/txp_admin.rb CHANGED
@@ -3,7 +3,60 @@ module TxpAdmin
3
3
  class ConfigNotFound < Exception
4
4
  end
5
5
 
6
-
6
+ class RestoreFileNotFound < Exception
7
+ end
8
+
9
+ class MysqlConnect
10
+
11
+ @@config = nil
12
+
13
+ def self.config
14
+ return @@config unless @@config.nil?
15
+ @@config = TxpAdmin::Config.new.options
16
+ # puts @@config.inspect
17
+ # FIXME: how do a mysql alias evaluated in exec?
18
+ # Example: mysql='mysql --socket=/Applications/xampp/xamppfiles/var/mysql/mysql.sock'
19
+ @@config[:host] = @@config[:host] == "localhost" ? "127.0.0.1" : @@config[:host]
20
+ @@password = @@config[:pass].strip != "" ? " -p#{@@config[:pass]}" : ""
21
+ @@config
22
+ end
23
+
24
+ def self.dump_command
25
+ c = "mysqldump #{self.command_options}"
26
+ end
27
+
28
+ def self.backup
29
+ command = "#{self.dump_command} | gzip -c > txpadmin_backup_#{Time.now.to_i}.dump.gz"
30
+ puts "executing: #{command}"
31
+ exec(command)
32
+ end
33
+
34
+ def self.restore(filename)
35
+ raise RestoreFileNotFound unless File.exists?(filename)
36
+ # "Restoring file #{filename}. Continue? [Y/n] "
37
+ command = "gunzip -c #{filename} | #{self.commandline}"
38
+ exec(command)
39
+ end
40
+
41
+ def self.command_options
42
+ " -u#{self.config[:user]} #{@@password} -h#{self.config[:host]} #{self.config[:db]}"
43
+ end
44
+
45
+ def self.commandline
46
+ c = "mysql #{self.command_options}"
47
+ end
48
+
49
+ def self.execute(sql_command)
50
+ puts "executing SQL: #{sql_command}"
51
+ exec("#{commandline} -e \"#{sql_command}\" ")
52
+ end
53
+
54
+ def self.console
55
+ # puts "executing command: #{commandline}"
56
+ exec(commandline)
57
+ end
58
+
59
+ end
7
60
 
8
61
  class Config
9
62
 
@@ -13,8 +66,7 @@ module TxpAdmin
13
66
  @options = self.class.parse_config_file
14
67
  end
15
68
 
16
- def self.parse_config_file(path=".")
17
-
69
+ def self.parse_config_file(path=".")
18
70
  config_file = "#{path}/textpattern/config.php"
19
71
  raise TxpAdmin::ConfigNotFound unless File.exists?(config_file)
20
72
  config = `cat #{config_file}`
@@ -22,13 +74,11 @@ module TxpAdmin
22
74
  config.gsub!("<?php", "")
23
75
  config.gsub!("?>", "")
24
76
  config.gsub!("$txpcfg", "txpcfg")
25
- eval(config)
26
-
77
+ eval(config)
27
78
  txpcfg.each do |key,value|
28
79
  txpcfg[key.to_sym] = value
29
80
  end
30
-
31
-
81
+
32
82
  # puts txpcfg.inspect
33
83
  return txpcfg
34
84
  end
data/txpadmin.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{txpadmin}
5
- s.version = "0.0.2"
5
+ s.version = "0.0.3"
6
6
  s.required_rubygems_version = Gem::Requirement.new(">= 1.3") if s.respond_to? :required_rubygems_version=
7
7
  s.authors = ["Fabrizio Regini"]
8
8
  s.date = %q{2009-12-26}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: txpadmin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabrizio Regini