Module: Shnell::Config
- Included in:
- Shnell, Actions::Database, Actions::Ftp, Backup::BackupScript
- Defined in:
- lib/shnell/config.rb
Instance Method Summary collapse
- #config ⇒ Object
- #db_credentials ⇒ Object
- #db_password ⇒ Object
- #db_user ⇒ Object
- #ftp_host ⇒ Object
- #ftp_password ⇒ Object
- #ftp_user ⇒ Object
- #read_config(scope, key) ⇒ Object
- #write_config(scope, key, value) ⇒ Object
Instance Method Details
#config ⇒ Object
53 54 55 56 57 |
# File 'lib/shnell/config.rb', line 53 def config content = File.read(CONFIG_FILENAME) rescue nil return {} unless content YAML.load(content) end |
#db_credentials ⇒ Object
13 14 15 |
# File 'lib/shnell/config.rb', line 13 def db_credentials "-u#{db_user} #{"-p#{db_password}" if db_password != ''}" end |
#db_password ⇒ Object
9 10 11 |
# File 'lib/shnell/config.rb', line 9 def db_password read_config(:db, :password) end |
#db_user ⇒ Object
5 6 7 |
# File 'lib/shnell/config.rb', line 5 def db_user read_config(:db, :user) end |
#ftp_host ⇒ Object
17 18 19 |
# File 'lib/shnell/config.rb', line 17 def ftp_host read_config(:ftp, :host) end |
#ftp_password ⇒ Object
25 26 27 |
# File 'lib/shnell/config.rb', line 25 def ftp_password read_config(:ftp, :password) end |
#ftp_user ⇒ Object
21 22 23 |
# File 'lib/shnell/config.rb', line 21 def ftp_user read_config(:ftp, :user) end |
#read_config(scope, key) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/shnell/config.rb', line 29 def read_config(scope, key) hash = config[scope.to_s] || {} value = hash[key.to_s] if value.nil? question = "Give me the #{scope} #{key}:" value = if key.to_s =~ /password/ ask(question) { |q| q.echo = "*" } else ask(question) end write_config scope, key, value end value end |
#write_config(scope, key, value) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/shnell/config.rb', line 44 def write_config(scope, key, value) hash = config hash[scope.to_s] ||= {} hash[scope.to_s].update key.to_s => value File.open(CONFIG_FILENAME, "w") do |file| file << YAML.dump(hash) end end |