Move ask procedure out of config function.

This commit is contained in:
Glenn Y. Rolland 2015-11-28 12:28:10 +01:00
parent 8669fdaa83
commit b2f55c89f0

View file

@ -11,7 +11,7 @@ class Epafh::App < Thor
'password' => '' 'password' => ''
}, },
'crm' => { 'crm' => {
'baseurl' => '', 'baseurl' => 'http://crm.gnuside.com/',
'login' => '', 'login' => '',
'password' => '' 'password' => ''
} }
@ -24,40 +24,22 @@ class Epafh::App < Thor
desc 'config', 'Initialize configuration' desc 'config', 'Initialize configuration'
def config def config
puts "Welcome to Epafh !".green puts "Welcome to Epafh !".green
cli = ::HighLine.new
config = CONFIG_DEFAULT config = CONFIG_DEFAULT
if File.exist? Epafh::EPAFI_CONFIG_FILE then if File.exist? Epafh::EPAFI_CONFIG_FILE then
config = config.merge(YAML::load( File.open( Epafh::EPAFI_CONFIG_FILE ) ) || {}) config = config.merge(YAML::load( File.open( Epafh::EPAFI_CONFIG_FILE ) ) || {})
end end
params = { imap_params = {
server: {desc: 'IMAP hostname ? ' }, server: {desc: 'IMAP hostname ? ' },
login: {desc: 'IMAP username ? ' }, login: {desc: 'IMAP username ? ' },
password: {desc: 'IMAP password ? ', hidden: true} password: {desc: 'IMAP password ? ', hidden: true}
} }
imap = config['imap'] crm_params = {
# Ask parameters baseurl: {desc: 'CRM base url ? ' },
params.each.map {|param,values| [param.to_s,values] } login: {desc: 'CRM username ? ' },
.each do |param, values| password: {desc: 'CRM password ? ', hidden: true}
backup = imap[param] }
backup_hidden = imap[param].gsub(/./,'*') config['imap'] = config_with_rules config['imap'], imap_params
imap[param] = cli.ask(values[:desc]) do |q| config['crm'] = config_with_rules config['crm'], crm_params
# 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
FileUtils.mkdir_p(Epafh::EPAFI_CONFIG_DIR) 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
@ -81,6 +63,36 @@ class Epafh::App < Thor
private private
# Ask parameters, with rule constraints
#
def config_with_rules cfg_in, rules
cli = ::HighLine.new
cfg_out = cfg_in.clone
# Loop parameter rules
rules.each.map {|param,values| [param.to_s,values] }
.each do |param, values|
backup = cfg_out[param]
backup_hidden = cfg_out[param].gsub(/./,'*')
cfg_out[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 cfg_out[param].empty? then
q.default =
if (values[:hidden]) then backup_hidden
else cfg_out[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 cfg_out[param] == backup_hidden then
cfg_out[param] = backup
end
end
cfg_out
end
def parse_configuration def parse_configuration
## Load configuration ## Load configuration