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.



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

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



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

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_databasesObject

Returns the value of attribute mysql_databases.



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

def mysql_databases
  @mysql_databases
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_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

#hosts(name) ⇒ Object



86
87
88
89
90
91
# File 'lib/mysqlknife/configs.rb', line 86

def hosts(name)
  hosts = []
  hosts << db_host(name)
  hosts.concat(slaves(name))
  hosts.sort
end

#settings(name) ⇒ Object



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

def settings(name)
  @name           = name
  @ssh_color      = db_color(name)
  @ssh_use        = db_ssh(name)
  @mysql_port     = db_port(name)
  @mysql_username = db_username(name)
  @mysql_password = db_password(name)
end

#show(name) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mysqlknife/configs.rb', line 93

def show(name)
  host     = db_host(name)
  port     = db_port(name)
  username = db_username(name)
  password = db_password(name)
  slaves   = db_slaves(name)
  slaves   = slaves.join(', ') unless slaves.nil?

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

#slaves(name) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/mysqlknife/configs.rb', line 76

def slaves(name)
  slaves = db_slaves(name)

  if slaves.nil?
    []
  else
    slaves
  end
end

#tool(name, tool) ⇒ Object



117
118
119
120
121
# File 'lib/mysqlknife/configs.rb', line 117

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

#tools(name) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/mysqlknife/configs.rb', line 108

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