Module: Golem::Config
- Defined in:
- lib/golem/config.rb
Overview
Configuration management.
Constant Summary collapse
- CFG_PATHS =
List of paths config file is searched for.
["/usr/local/etc/golem/golem.conf.rb", "/usr/local/etc/golem.conf.rb", "/etc/golem/golem.conf.rb", "/etc/golem.conf.rb", "~/golem.conf.rb"]
- CFG_VARS =
List of available config variable names.
[:db, :user_home, :repository_dir, :cfg_path, :base_dir, :bin_dir, :hooks_dir, :keys_file_use_command, :keys_file_ssh_opts]
Class Method Summary collapse
-
.auto_configure(path = nil, &block) ⇒ Object
Auto configure Golem.
-
.config_hash ⇒ Hash
Get configuration variables that is set (e.g. not
nil
). -
.configure(opts_or_path = nil) {|_self| ... } ⇒ Config
Configure Golem with options given as argument, yield self then setting defaults.
-
.hook_path(hook) ⇒ String
Path to given hook.
-
.keys_file_path ⇒ String
Path to
authorized_keys
file. -
.method_missing(sym, *args, &block) ⇒ Object
Override
method_missing
to handle.config_var
and.config_var=
(e.g. Golem::Config.db = ‘static’). -
.repository_base_path ⇒ String
Path to directory containing repositories.
-
.repository_path(repo) ⇒ String
Path to given repository.
-
.respond_to?(sym) ⇒ Boolean
Override
respond_to?
to respond to.config_var
and.config_var=
(e.g. Golem::Config.db = ‘static’). -
.save! ⇒ Object
Write configuration to file.
Class Method Details
.auto_configure(path = nil, &block) ⇒ Object
Auto configure Golem. Tries to find config file, if one can be found executes it, otherwise calls configure.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/golem/config.rb', line 10 def self.auto_configure(path = nil, &block) path = if ENV.key?('GOLEM_CONFIG') && File.exists?(ENV['GOLEM_CONFIG']) ENV['GOLEM_CONFIG'] elsif ENV.key?('GOLEM_BASE') && File.exists?(ENV['GOLEM_BASE'].to_s + "/golem.conf.rb") ENV['GOLEM_BASE'].to_s + "/golem.conf.rb" else CFG_PATHS.find {|try_path| File.exists?(try_path)} end unless File.exists?(path.to_s) if File.exists?(path.to_s) @auto_configure_path = path.to_s @auto_configure_block = block require path.to_s end configure path unless @vars #configure was not called or there was no config file end |
.config_hash ⇒ Hash
Get configuration variables that is set (e.g. not nil
).
80 81 82 83 |
# File 'lib/golem/config.rb', line 80 def self.config_hash auto_configure unless @vars @vars.reject {|k, v| v.nil?} end |
.configure(path, &block) ⇒ Config .configure(opts, &block) ⇒ Config
Configure Golem with options given as argument, yield self then setting defaults.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/golem/config.rb', line 41 def self.configure(opts_or_path = nil, &block) opts = opts_or_path.is_a?(Hash) ? opts_or_path : {:cfg_path => opts_or_path} opts[:cfg_path] = @auto_configure_path if @auto_configure_path @vars = opts.reject {|k, v| ! CFG_VARS.include?(k)} @auto_configure_block.call(self) if @auto_configure_block yield self if block_given? self.user_home = ENV['HOME'] if user_home.nil? && ENV.key?('HOME') self.repository_dir = user_home + "/repositories" unless repository_dir unless base_dir self.base_dir = if ENV.key?('GOLEM_BASE') ENV['GOLEM_BASE'] elsif File.exists?(cfg_path.to_s) File.dirname(cfg_path.to_s) else File.(File.dirname(__FILE__) + '/../..') end end self.cfg_path = base_dir + '/golem.conf.rb' unless cfg_path self.bin_dir = base_dir + '/bin' unless bin_dir self.hooks_dir = base_dir + '/hooks' unless hooks_dir self.keys_file_use_command = false unless keys_file_use_command self end |
.hook_path(hook) ⇒ String
Returns path to given hook.
109 110 111 |
# File 'lib/golem/config.rb', line 109 def self.hook_path(hook) hooks_dir + "/" + hook.to_s end |
.keys_file_path ⇒ String
Returns path to authorized_keys
file.
92 93 94 |
# File 'lib/golem/config.rb', line 92 def self.keys_file_path user_home + "/.ssh/authorized_keys" end |
.method_missing(sym, *args, &block) ⇒ Object
Override method_missing
to handle .config_var
and .config_var=
(e.g. Golem::Config.db = ‘static’).
71 72 73 74 75 76 |
# File 'lib/golem/config.rb', line 71 def self.method_missing(sym, *args, &block) auto_configure unless @vars return @vars[sym] if CFG_VARS.include?(sym) return @vars[sym.to_s[0..-2].to_sym] = args.first if sym.to_s.match(/=\z/) && CFG_VARS.include?(sym.to_s[0..-2].to_sym) super end |
.repository_base_path ⇒ String
Returns path to directory containing repositories.
97 98 99 |
# File 'lib/golem/config.rb', line 97 def self.repository_base_path (repository_dir[0..0] == "/" ? '' : user_home + '/') + repository_dir end |
.repository_path(repo) ⇒ String
Returns path to given repository.
103 104 105 |
# File 'lib/golem/config.rb', line 103 def self.repository_path(repo) repository_base_path + '/' + repo.to_s + '.git' end |
.respond_to?(sym) ⇒ Boolean
Override respond_to?
to respond to .config_var
and .config_var=
(e.g. Golem::Config.db = ‘static’).
66 67 68 |
# File 'lib/golem/config.rb', line 66 def self.respond_to?(sym) CFG_VARS.include?(sym) || (sym.to_s.match(/=\z/) && CFG_VARS.include?(sym.to_s[0..-2].to_sym)) || super end |
.save! ⇒ Object
Write configuration to file.
86 87 88 89 |
# File 'lib/golem/config.rb', line 86 def self.save! abort "No configuration path given!" unless cfg_path File.open(cfg_path, 'w') {|f| f.write("Golem.configure do |cfg|\n" + config_hash.collect {|k, v| "\tcfg.#{k.to_s} = \"#{v.to_s}\""}.join("\n") + "\nend\n")} end |