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,18 +1,10 @@
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 = {
include Thor::Actions
default_task :crawl
desc 'config', 'Initialize configuration'
def config
puts "Welcome to Epafh !".green
cli = ::HighLine.new
config = {
'imap' => { 'imap' => {
'server' => '', 'server' => '',
'login' => '', 'login' => '',
@ -24,25 +16,54 @@ class Epafh::App < Thor
'password' => '' 'password' => ''
} }
} }
include Thor::Actions
default_task :crawl
desc 'config', 'Initialize configuration'
def config
puts "Welcome to Epafh !".green
cli = ::HighLine.new
config = CONFIG_DEFAULT
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
config['imap']['login'] = cli.ask("IMAP username ? ") do |q|
q.default = "john.smith@example.com"
end end
config['imap']['password'] = cli.ask("IMAP password ? ") do |q|
q.default = "blabla" ; q.echo = false
end end
FileUtils.mkdir_p (File.dirname(Epafh::EPAFI_CONFIG_FILE)) # 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
FileUtils.mkdir_p(Epafh::EPAFI_CONFIG_DIR)
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