From 44234b59545497ea8793e8e409ca495ce93a528a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 4 Nov 2015 16:54:41 +0100 Subject: [PATCH] Feature #4082: Add editor support for user inputs in CLI --- src/cli/one_helper.rb | 57 +++++++++++++----------- src/cli/one_helper/onetemplate_helper.rb | 14 ++++-- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/cli/one_helper.rb b/src/cli/one_helper.rb index 12d606bd4a..7520ecd254 100644 --- a/src/cli/one_helper.rb +++ b/src/cli/one_helper.rb @@ -828,39 +828,46 @@ EOT end def OpenNebulaHelper.update_template_helper(append, id, resource, path, xpath, update=true) - unless path - require 'tempfile' + if path + return File.read(path) + elsif append + return editor_input() + else + if update + rc = resource.info - tmp = Tempfile.new(id.to_s) - path = tmp.path - - if !append - if update - rc = resource.info - - if OpenNebula.is_error?(rc) - puts rc.message - exit -1 - end + if OpenNebula.is_error?(rc) + puts rc.message + exit -1 end - - tmp << resource.template_like_str(xpath) - tmp.flush end - editor_path = ENV["EDITOR"] ? ENV["EDITOR"] : EDITOR_PATH - system("#{editor_path} #{path}") + return editor_input(resource.template_like_str(xpath)) + end + end - unless $?.exitstatus == 0 - puts "Editor not defined" - exit -1 - end + def OpenNebulaHelper.editor_input(contents=nil) + require 'tempfile' - tmp.close + tmp = Tempfile.new("one_cli") + + if contents + tmp << contents + tmp.flush end - str = File.read(path) - str + editor_path = ENV["EDITOR"] ? ENV["EDITOR"] : EDITOR_PATH + system("#{editor_path} #{tmp.path}") + + unless $?.exitstatus == 0 + puts "Editor not defined" + exit -1 + end + + tmp.close + + str = File.read(tmp.path) + return str end def self.parse_user_object(user_object) diff --git a/src/cli/one_helper/onetemplate_helper.rb b/src/cli/one_helper/onetemplate_helper.rb index 39a9c0f76d..80e3672aa2 100644 --- a/src/cli/one_helper/onetemplate_helper.rb +++ b/src/cli/one_helper/onetemplate_helper.rb @@ -118,7 +118,7 @@ EOT answers = "" - puts "There are some parameters that require user input." + puts "There are some parameters that require user input. Use the string <> to launch an editor (e.g. for multi-line inputs)" user_inputs.each do |key, val| input_cfg = val.split('|') @@ -137,10 +137,16 @@ EOT print " * (#{key}) #{description}: " case type - when 'text' + when 'text', 'text64' answer = STDIN.readline.chop - when 'text64' - answer = Base64::encode64(STDIN.readline.chop).strip.delete("\n") + + if answer == "<>" + answer = OpenNebulaHelper.editor_input() + end + + if type == 'text64' + answer = Base64::encode64(answer).strip.delete("\n") + end when 'password' answer = OpenNebulaHelper::OneHelper.get_password else