diff --git a/lib/epafh/app.rb b/lib/epafh/app.rb index c82fced..cb3c0ad 100644 --- a/lib/epafh/app.rb +++ b/lib/epafh/app.rb @@ -1,8 +1,21 @@ require 'highline' class Epafh::App < Thor + class InvalidConfiguration < RuntimeError ; end CONFIG_FILE = 'config/secrey.yml' + CONFIG_DEFAULT = { + 'imap' => { + 'server' => '', + 'login' => '', + 'password' => '' + }, + 'crm' => { + 'baseurl' => '', + 'login' => '', + 'password' => '' + } + } include Thor::Actions default_task :crawl @@ -12,37 +25,45 @@ class Epafh::App < Thor def config puts "Welcome to Epafh !".green cli = ::HighLine.new - config = { - 'imap' => { - 'server' => '', - 'login' => '', - 'password' => '' - }, - 'crm' => { - 'baseurl' => '', - 'login' => '', - 'password' => '' - } - } + config = CONFIG_DEFAULT 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 - config['imap']['server'] = cli.ask("IMAP hostname ? ") do |q| - q.default = "imap.example.com" + params = { + 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 - config['imap']['login'] = cli.ask("IMAP username ? ") do |q| - 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)) + FileUtils.mkdir_p(Epafh::EPAFI_CONFIG_DIR) File.open(Epafh::EPAFI_CONFIG_FILE, 'w'){|f| f.write(config.to_yaml)} end desc 'crawl', 'Crawls email to save mails' def crawl - #saved_info = [] parse_configuration ## Run application @@ -50,7 +71,6 @@ class Epafh::App < Thor app.connect! app.examine_all - #pp saved_info app.disconnect! end @@ -82,7 +102,7 @@ class Epafh::App < Thor } } 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 diff --git a/lib/epafh/constants.rb b/lib/epafh/constants.rb index de695a4..6e3023f 100644 --- a/lib/epafh/constants.rb +++ b/lib/epafh/constants.rb @@ -1,6 +1,7 @@ module Epafh VERSION = "0.1.0" - EPAFI_CONFIG_FILE = File.join(ENV['HOME'],'.epafh','config.yml') - EPAFI_IGNORE_FILE = File.join(ENV['HOME'],'.epafh','ignore.yml') + EPAFI_CONFIG_DIR = File.join(ENV['HOME'], '.epafh') + EPAFI_CONFIG_FILE = File.join(EPAFI_CONFIG_DIR, 'config.yml') + EPAFI_IGNORE_FILE = File.join(ENV['HOME'], '.epafh', 'ignore.yml') end