lib/awskeyring_command.rb in awskeyring-1.12.0 vs lib/awskeyring_command.rb in awskeyring-1.12.1

- old
+ new

@@ -184,12 +184,12 @@ desc 'exec ACCOUNT command...', I18n.t('exec_desc') method_option 'no-bundle', type: :boolean, aliases: '-b', desc: I18n.t('method_option.nobundle'), default: false method_option 'no-token', type: :boolean, aliases: '-n', desc: I18n.t('method_option.notoken'), default: false # execute an external command with env set - def exec(account, *command) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize - if command.empty? + def exec(account, *exec) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize + if exec.empty? warn I18n.t('message.exec') exit 1 end account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists), @@ -197,11 +197,11 @@ ) cred = age_check_and_get(account: account, no_token: options['no-token']) env_vars = Awskeyring::Awsapi.get_env_array(cred) unbundle if options['no-bundle'] begin - pid = Process.spawn(env_vars, command.join(' ')) + pid = Process.spawn(env_vars, exec.join(' ')) Process.wait pid exit 1 if Process.last_status.exitstatus.positive? rescue Errno::ENOENT => e warn e exit 1 @@ -454,11 +454,11 @@ exit 1 end comp_lines = comp_line[0..(comp_point_str.to_i)].split - comp_type, sub_cmd = comp_type(comp_lines: comp_lines, prev: prev) + comp_type, sub_cmd = comp_type(comp_lines: comp_lines, prev: prev, curr: curr) list = fetch_auto_resp(comp_type, sub_cmd) puts list.select { |elem| elem.start_with?(curr) }.sort!.join("\n") end private @@ -471,34 +471,34 @@ [curr, prev] end end # determine the type of completion needed - def comp_type(comp_lines:, prev:) + def comp_type(comp_lines:, prev:, curr:) sub_cmd = sub_command(comp_lines) comp_idx = comp_lines.rindex(prev) case prev when '--path', '-p' comp_type = :path_type when '--browser', '-b' comp_type = :browser_type else comp_type = :command - comp_type = param_type(comp_idx, sub_cmd) unless sub_cmd.empty? + comp_type = param_type(comp_idx, sub_cmd, curr) unless sub_cmd.empty? end [comp_type, sub_cmd] end # check params for named params or fall back to flags - def param_type(comp_idx, sub_cmd) - types = %i[opt req] + def param_type(comp_idx, sub_cmd, curr) + types = %i[opt req rest] param_list = method(sub_cmd).parameters.select { |elem| types.include? elem[0] } if comp_idx.zero? :command - elsif comp_idx > param_list.length + elsif comp_idx > param_list.length || curr.start_with?('-') :flag else param_list[comp_idx - 1][1] end end @@ -513,11 +513,11 @@ (Awskeyring.solo_select(list_commands, sub_cmd) || '').tr('-', '_') end # given a type return the right list for completions - def fetch_auto_resp(comp_type, sub_cmd) + def fetch_auto_resp(comp_type, sub_cmd) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength case comp_type when :command list_commands when :account Awskeyring.list_account_names @@ -527,18 +527,31 @@ Awskeyring.list_console_path when :token Awskeyring.list_token_names when :browser_type Awskeyring.list_browsers + when :exec + list_exec else list_arguments(command: sub_cmd) end end # list command names def list_commands commands = self.class.all_commands.keys.map { |elem| elem.tr('_', '-') } commands.reject! { |elem| %w[autocomplete default decode].include?(elem) } + end + + # list executables + def list_exec + list = [] + ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| + list.concat(Dir.children(File.expand_path(path))) + rescue Errno::ENOENT + next + end + list.flatten end # list flags for a command def list_arguments(command:) options = self.class.all_commands[command].options.values