Class: Mysqlknife::Configs

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mysqlknife/configs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigs

Returns a new instance of Configs.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mysqlknife/configs.rb', line 27

def initialize
  # Define path for config file:
  if ENV['ENV'] == 'test'
    path = File.expand_path(File.join(Dir.pwd, '.db.yml'))
  else
    path = File.expand_path("#{Dir.home}/.db.yml")
  end

  # Load config file:
  @configs = YAML.load_file(path) if File.exist?(path)

  if @configs.nil?
    puts "Not exist config file in #{path}"
    exit 1
  end

  # Define generic variables:
  @path         = path
  @ssh_host     = @configs['ssh']['host']
  @ssh_port     = @configs['ssh']['port']
  @ssh_user     = @configs['ssh']['user']
  @ssh_password = @configs['ssh']['password']
  @ssh_key      = @configs['ssh']['keys']
  @connections  = @configs['databases'].keys.sort
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mysqlknife/configs.rb', line 53

def method_missing(name, *args)
  method = name.to_s.split(/_/)
  if method.first == 'db'
    param = method[1]
    conn  = args[0]

    if @configs['databases'].include?(conn)
      @configs['databases'][conn][param]
    else
      puts "Not exist connecion name: #{conn}"
      exit 1
    end
  end
end

Instance Attribute Details

#configsObject (readonly)

Returns the value of attribute configs.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def configs
  @configs
end

#connectionsObject (readonly)

Returns the value of attribute connections.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def connections
  @connections
end

#mysql_databaseObject

Returns the value of attribute mysql_database.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def mysql_database
  @mysql_database
end

#mysql_hostObject

Returns the value of attribute mysql_host.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def mysql_host
  @mysql_host
end

#mysql_passwordObject (readonly)

Returns the value of attribute mysql_password.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def mysql_password
  @mysql_password
end

#mysql_portObject (readonly)

Returns the value of attribute mysql_port.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def mysql_port
  @mysql_port
end

#mysql_slaveObject (readonly)

Returns the value of attribute mysql_slave.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def mysql_slave
  @mysql_slave
end

#mysql_usernameObject (readonly)

Returns the value of attribute mysql_username.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def mysql_username
  @mysql_username
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def path
  @path
end

#ssh_colorObject (readonly)

Returns the value of attribute ssh_color.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def ssh_color
  @ssh_color
end

#ssh_hostObject (readonly)

Returns the value of attribute ssh_host.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def ssh_host
  @ssh_host
end

#ssh_keyObject (readonly)

Returns the value of attribute ssh_key.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def ssh_key
  @ssh_key
end

#ssh_passwordObject (readonly)

Returns the value of attribute ssh_password.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def ssh_password
  @ssh_password
end

#ssh_portObject (readonly)

Returns the value of attribute ssh_port.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def ssh_port
  @ssh_port
end

#ssh_useObject (readonly)

Returns the value of attribute ssh_use.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def ssh_use
  @ssh_use
end

#ssh_userObject (readonly)

Returns the value of attribute ssh_user.



9
10
11
# File 'lib/mysqlknife/configs.rb', line 9

def ssh_user
  @ssh_user
end

Instance Method Details

#connection(name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/mysqlknife/configs.rb', line 68

def connection(name)
  @name           = name
  @ssh_color      = db_color(name)
  @ssh_use        = db_ssh(name)
  @mysql_host     = db_host(name)
  @mysql_port     = db_port(name)
  @mysql_username = db_username(name)
  @mysql_password = db_password(name)
  @mysql_slave    = db_slave(name)
end

#show(name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mysqlknife/configs.rb', line 79

def show(name)
  host     = db_host(name)
  port     = db_port(name)
  username = db_username(name)
  password = db_password(name)
  slave    = db_slave(name)

  %W[Master:\ #{host}
     Slaves:\ #{slave}
     Port:\ #{port}
     Username:\ #{username}
     Password:\ #{password}].join("\n")
end

#tool(tool) ⇒ Object



102
103
104
105
106
# File 'lib/mysqlknife/configs.rb', line 102

def tool(tool)
  unless db_tools(@name).nil?
    db_tools(@name).select { |command| /#{tool}/.match(command.to_s) }.first
  end
end

#tools(name) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/mysqlknife/configs.rb', line 93

def tools(name)
  unless db_tools(name).nil?
    db_tools(name).map {|tool| tool.split.first(1).first }
  else
    puts "Please, add custom tools in config file."
    exit 1
  end
end