Module: Shnell::Config

Included in:
Shnell, Actions::Database, Actions::Ftp, Backup::BackupScript
Defined in:
lib/shnell/config.rb

Instance Method Summary collapse

Instance Method Details

#configObject



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_credentialsObject



13
14
15
# File 'lib/shnell/config.rb', line 13

def db_credentials
  "-u#{db_user} #{"-p#{db_password}" if db_password != ''}"
end

#db_passwordObject



9
10
11
# File 'lib/shnell/config.rb', line 9

def db_password
  read_config(:db, :password)
end

#db_userObject



5
6
7
# File 'lib/shnell/config.rb', line 5

def db_user
  read_config(:db, :user)
end

#ftp_hostObject



17
18
19
# File 'lib/shnell/config.rb', line 17

def ftp_host
  read_config(:ftp, :host)
end

#ftp_passwordObject



25
26
27
# File 'lib/shnell/config.rb', line 25

def ftp_password
  read_config(:ftp, :password)
end

#ftp_userObject



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