Improve configuration step for password.

This commit is contained in:
Glenn Y. Rolland 2015-11-28 11:55:19 +01:00
parent 8654b94cae
commit 8669fdaa83
2 changed files with 48 additions and 27 deletions

View file

@ -1,8 +1,21 @@
require 'highline' require 'highline'
class Epafh::App < Thor class Epafh::App < Thor
class InvalidConfiguration < RuntimeError ; end
CONFIG_FILE = 'config/secrey.yml' CONFIG_FILE = 'config/secrey.yml'
CONFIG_DEFAULT = {
'imap' => {
'server' => '',
'login' => '',
'password' => ''
},
'crm' => {
'baseurl' => '',
'login' => '',
'password' => ''
}
}
include Thor::Actions include Thor::Actions
default_task :crawl default_task :crawl
@ -12,37 +25,45 @@ class Epafh::App < Thor
def config def config
puts "Welcome to Epafh !".green puts "Welcome to Epafh !".green
cli = ::HighLine.new cli = ::HighLine.new
config = { config = CONFIG_DEFAULT
'imap' => {
'server' => '',
'login' => '',
'password' => ''
},
'crm' => {
'baseurl' => '',
'login' => '',
'password' => ''
}
}
if File.exist? Epafh::EPAFI_CONFIG_FILE then if File.exist? Epafh::EPAFI_CONFIG_FILE then
config.merge (YAML::load( File.open( Epafh::EPAFI_CONFIG_FILE ) ) || {}) config = config.merge(YAML::load( File.open( Epafh::EPAFI_CONFIG_FILE ) ) || {})
end end
config['imap']['server'] = cli.ask("IMAP hostname ? ") do |q| params = {
q.default = "imap.example.com" server: {desc: 'IMAP hostname ? ' },
login: {desc: 'IMAP username ? ' },
password: {desc: 'IMAP password ? ', hidden: true}
}
imap = config['imap']
# Ask parameters
params.each.map {|param,values| [param.to_s,values] }
.each do |param, values|
backup = imap[param]
backup_hidden = imap[param].gsub(/./,'*')
imap[param] = cli.ask(values[:desc]) do |q|
# Disable echo if hidden enabled
q.echo = '*' if values[:hidden]
# Replace default value by stars if hidden
if not imap[param].empty? then
q.default =
if (values[:hidden]) then backup_hidden
else imap[param]
end
end
end
# When RETURN is pressed, Highline uses default (starred)
# We have to replace it with the real value
if values[:hidden] and imap[param] = backup_hidden then
imap[param] = backup
end
end end
config['imap']['login'] = cli.ask("IMAP username ? ") do |q| FileUtils.mkdir_p(Epafh::EPAFI_CONFIG_DIR)
q.default = "john.smith@example.com"
end
config['imap']['password'] = cli.ask("IMAP password ? ") do |q|
q.default = "blabla" ; q.echo = false
end
FileUtils.mkdir_p (File.dirname(Epafh::EPAFI_CONFIG_FILE))
File.open(Epafh::EPAFI_CONFIG_FILE, 'w'){|f| f.write(config.to_yaml)} File.open(Epafh::EPAFI_CONFIG_FILE, 'w'){|f| f.write(config.to_yaml)}
end end
desc 'crawl', 'Crawls email to save mails' desc 'crawl', 'Crawls email to save mails'
def crawl def crawl
#saved_info = []
parse_configuration parse_configuration
## Run application ## Run application
@ -50,7 +71,6 @@ class Epafh::App < Thor
app.connect! app.connect!
app.examine_all app.examine_all
#pp saved_info
app.disconnect! app.disconnect!
end end
@ -82,7 +102,7 @@ class Epafh::App < Thor
} }
} }
validator = HashValidator.validate(@config, validations) validator = HashValidator.validate(@config, validations)
raise "Configuration is not valid: #{validator.errors.inspect}" unless validator.valid? raise InvalidConfiguration, "Configuration is not valid: #{validator.errors.inspect}" unless validator.valid?
end end
end end

View file

@ -1,6 +1,7 @@
module Epafh module Epafh
VERSION = "0.1.0" VERSION = "0.1.0"
EPAFI_CONFIG_FILE = File.join(ENV['HOME'],'.epafh','config.yml') EPAFI_CONFIG_DIR = File.join(ENV['HOME'], '.epafh')
EPAFI_IGNORE_FILE = File.join(ENV['HOME'],'.epafh','ignore.yml') EPAFI_CONFIG_FILE = File.join(EPAFI_CONFIG_DIR, 'config.yml')
EPAFI_IGNORE_FILE = File.join(ENV['HOME'], '.epafh', 'ignore.yml')
end end