lib/conjur/command/variables.rb in conjur-cli-4.24.0 vs lib/conjur/command/variables.rb in conjur-cli-4.25.0

- old
+ new

@@ -32,59 +32,61 @@ c.arg_name "value" c.desc "Initial value, which may also be specified as the second command argument after the variable id" c.flag [:v, :"value"] - acting_as_option(c) - - c.arg_name 'interactive' - c.desc 'Create variable interactively' - c.switch [:i, :'interactive'] + acting_as_option c + annotate_option c + + interactive_option c + c.action do |global_options,options, args| @default_mime_type = c.flags[:m].default_value @default_kind = c.flags[:k].default_value id = args.shift unless args.empty? - value = args.shift unless args.empty? - raise "Received conflicting value arguments" if value && options[:value] + exit_now! "Received conflicting value arguments" if value && options[:value] - groupid = options[:'ownerid'] - mime_type = options.delete(:m) - kind = options.delete(:k) - value ||= options.delete(:v) - - options.delete(:'interactive') - options.delete(:"mime-type") - options.delete(:"kind") - options.delete(:'value') - + groupid = options[:ownerid] + mime_type = options[:m] + kind = options[:k] + value ||= options[:v] + interactive = options[:interactive] || id.blank? + annotate = options[:annotate] + + exit_now! "Received --annotate option without --interactive" if annotate && !interactive + annotations = {} - - # If the user asked for interactive mode, or he didn't specify - # both an id and a value, prompt for any missing options. - if options.delete(:i) || !(id && value) - id ||= prompt_for_id - + # If the user asked for interactive mode, or he didn't specify and id + # prompt for any missing options. + if interactive + id ||= prompt_for_id :variable + groupid ||= prompt_for_group kind = prompt_for_kind if !kind || kind == @default_kind + + mime_type = prompt_for_mime_type if mime_type.blank? || mime_type == @default_mime_type - mime_type = prompt_for_mime_type if !mime_type || mime_type == @default_mime_type + annotations = prompt_for_annotations if annotate - annotations = prompt_for_annotations - value ||= prompt_for_value + + prompt_to_confirm :variable, "Id" => id, + "Kind" => kind, + "MIME type" => mime_type, + "Owner" => groupid, + "Value" => value end - options[:id] = id - options[:value] = value - options[:'ownerid'] = groupid if groupid - - var = api.create_variable(mime_type, kind, options) + variable_options = { id: id } + variable_options[:ownerid] = groupid if groupid + variable_options[:value] = value unless value.blank? + var = api.create_variable(mime_type, kind, variable_options) api.resource(var).annotations.merge!(annotations) if annotations && !annotations.empty? display(var, options) end end @@ -98,20 +100,22 @@ end var.desc "Decommission a variable" var.arg_name "id" var.command :retire do |c| + retire_options c + c.action do |global_options,options,args| id = require_arg(args, 'id') variable = api.variable(id) + + validate_retire_privileges variable, options retire_resource variable + give_away_resource variable, options - puts "Giving ownership to 'attic'" - variable.resource.give_to api.user('attic') - puts "Variable retired" end end var.desc "List variables" @@ -147,60 +151,26 @@ c.action do |global_options,options,args| id = require_arg(args, 'variable') $stdout.write api.variable(id).value(options[:version]) end end - end - - def self.prompt_for_id - highline.ask('Enter the id: ') - end - - def self.prompt_for_group - highline.ask('Enter the group: ', ->(name) { @group && @group.roleid } ) do |q| - q.validate = ->(name) do - name.empty? || (@group = api.group(name)).exists? - end - q.responses[:not_valid] = "Group '<%= @answer %>' doesn't exist, or you don't have permission to use it" - end - end - + def self.prompt_for_kind highline.ask('Enter the kind: ') {|q| q.default = @default_kind } end def self.prompt_for_mime_type - highline.ask('Enter the MIME type: ') {|q| q.default = @default_mime_type } - end - - def self.prompt_for_annotations - highline.say('Add annotations (press enter to finish):') - {}.tap do |annotations| - until (name = highline.ask('annotation name: ')).empty? - annotations[name] = read_till_eof('annotation value (^D to finish):') + highline.choose do |menu| + menu.prompt = 'Enter the MIME type: ' + menu.choice @default_mime_type + menu.choices *%w(application/json application/xml application/x-yaml application/x-pem-file) + menu.choice "other", nil do |c| + @highline.ask('Enter a custom mime type: ') end end end def self.prompt_for_value - read_till_eof('Enter the value (^D on its own line to finish):') - end - - def self.highline - require 'highline' - @highline ||= HighLine.new($stdin,$stderr) - end - - def self.read_till_eof(prompt = nil) - highline.say(prompt) if prompt - [].tap do |lines| - loop do - begin - lines << highline.ask('') - rescue EOFError - break - end - end - end.join("\n") + read_till_eof('Enter the secret value (^D on its own line to finish):') end end