lib/awskeyring_command.rb in awskeyring-1.8.2 vs lib/awskeyring_command.rb in awskeyring-1.8.3

- old
+ new

@@ -9,10 +9,11 @@ require 'awskeyring/validate' require 'awskeyring/version' # AWSkeyring command line interface. class AwskeyringCommand < Thor # rubocop:disable Metrics/ClassLength + package_name 'Awskeyring' I18n.load_path = Dir.glob(File.join(File.realpath(__dir__), '..', 'i18n', '*.{yml,yaml}')) I18n.backend.load_translations map %w[--version -v] => :__version map %w[--help -h] => :help @@ -25,28 +26,39 @@ map 'rmr' => :remove_role map 'rmt' => :remove_token map 'rot' => :rotate map 'tok' => :token map 'up' => :update + default_command :default # default to returning an error on failure. def self.exit_on_failure? true end - desc '--version, -v', I18n.t('__version.desc') + desc 'default', I18n.t('default_desc'), hide: true + # default command to run + def default + if Awskeyring.prefs.empty? + invoke :initialise + else + invoke :help + end + end + + desc '--version, -v', I18n.t('__version_desc') method_option 'no-remote', type: :boolean, aliases: '-r', desc: I18n.t('method_option.noremote'), default: false # print the version number def __version puts "Awskeyring v#{Awskeyring::VERSION}" if !options['no-remote'] && Awskeyring::VERSION != Awskeyring.latest_version puts "the latest version v#{Awskeyring.latest_version}" end puts "Homepage #{Awskeyring::HOMEPAGE}" end - desc 'initialise', I18n.t('initialise.desc') + desc 'initialise', I18n.t('initialise_desc') method_option :keychain, type: :string, aliases: '-n', desc: I18n.t('method_option.keychain') # initialise the keychain def initialise unless Awskeyring.prefs.empty? puts I18n.t('message.initialise', file: Awskeyring::PREFS_FILE) @@ -67,22 +79,22 @@ exec_name = File.basename($PROGRAM_NAME) puts I18n.t('message.addkeychain', keychain: keychain, exec_name: exec_name) end - desc 'list', I18n.t('list.desc') + desc 'list', I18n.t('list_desc') # list the accounts def list if Awskeyring.list_account_names.empty? warn I18n.t('message.missing_account', bin: File.basename($PROGRAM_NAME)) exit 1 end puts Awskeyring.list_account_names.join("\n") end map 'list-role' => :list_role - desc 'list-role', I18n.t('list_role.desc') + desc 'list-role', I18n.t('list_role_desc') method_option 'detail', type: :boolean, aliases: '-d', desc: I18n.t('method_option.detail'), default: false # List roles def list_role if Awskeyring.list_role_names.empty? warn I18n.t('message.missing_role', bin: File.basename($PROGRAM_NAME)) @@ -93,11 +105,11 @@ else puts Awskeyring.list_role_names.join("\n") end end - desc 'env ACCOUNT', I18n.t('env.desc') + desc 'env ACCOUNT', I18n.t('env_desc') method_option 'no-token', type: :boolean, aliases: '-n', desc: I18n.t('method_option.notoken'), default: false method_option 'unset', type: :boolean, aliases: '-u', desc: I18n.t('method_option.unset'), default: false # Print Env vars def env(account = nil) if options[:unset] @@ -111,11 +123,11 @@ cred = age_check_and_get(account: account, no_token: options['no-token']) put_env_string(cred) end end - desc 'json ACCOUNT', I18n.t('json.desc') + desc 'json ACCOUNT', I18n.t('json_desc') method_option 'no-token', type: :boolean, aliases: '-n', desc: I18n.t('method_option.notoken'), default: false # Print JSON for use with credential_process def json(account) account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists) @@ -128,11 +140,11 @@ token: cred[:token], expiry: (expiry || Time.new + Awskeyring::Awsapi::ONE_HOUR).iso8601 ) end - desc 'import ACCOUNT', I18n.t('import.desc') + desc 'import ACCOUNT', I18n.t('import_desc') method_option 'no-remote', type: :boolean, aliases: '-r', desc: I18n.t('method_option.noremote'), default: false # Import an Account def import(account = nil) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_not_exists) @@ -164,11 +176,11 @@ ) puts I18n.t('message.addtoken', account: account, time: Time.at(new_creds[:expiry].to_i)) end end - desc 'exec ACCOUNT command...', I18n.t('exec.desc') + desc 'exec ACCOUNT command...', I18n.t('exec_desc') method_option 'no-token', type: :boolean, aliases: '-n', desc: I18n.t('method_option.notoken'), default: false method_option 'no-bundle', type: :boolean, aliases: '-b', desc: I18n.t('method_option.nobundle'), default: false # execute an external command with env set def exec(account, *command) if command.empty? @@ -186,11 +198,11 @@ warn e.to_s exit 1 end end - desc 'add ACCOUNT', I18n.t('add.desc') + desc 'add ACCOUNT', I18n.t('add_desc') method_option :key, type: :string, aliases: '-k', desc: I18n.t('method_option.key') method_option :secret, type: :string, aliases: '-s', desc: I18n.t('method_option.secret') method_option :mfa, type: :string, aliases: '-m', desc: I18n.t('method_option.mfa') method_option 'no-remote', type: :boolean, aliases: '-r', desc: I18n.t('method_option.noremote'), default: false # Add an Account @@ -217,11 +229,11 @@ mfa: mfa ) puts I18n.t('message.addaccount', account: account) end - desc 'update ACCOUNT', I18n.t('update.desc') + desc 'update ACCOUNT', I18n.t('update_desc') method_option :key, type: :string, aliases: '-k', desc: I18n.t('method_option.key') method_option :secret, type: :string, aliases: '-s', desc: I18n.t('method_option.secret') method_option 'no-remote', type: :boolean, aliases: '-r', desc: I18n.t('method_option.noremote'), default: false # Update an Account def update(account = nil) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize @@ -245,11 +257,11 @@ ) puts I18n.t('message.upaccount', account: account) end map 'add-role' => :add_role - desc 'add-role ROLE', I18n.t('add_role.desc') + desc 'add-role ROLE', I18n.t('add_role_desc') method_option :arn, type: :string, aliases: '-a', desc: I18n.t('method_option.arn') # Add a role def add_role(role = nil) role = ask_check( existing: role, message: I18n.t('message.role'), @@ -265,42 +277,42 @@ arn: arn ) puts I18n.t('message.addrole', role: role) end - desc 'remove ACCOUNT', I18n.t('remove.desc') + desc 'remove ACCOUNT', I18n.t('remove_desc') # Remove an account def remove(account = nil) account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists), limited_to: Awskeyring.list_account_names ) Awskeyring.delete_account(account: account, message: I18n.t('message.delaccount', account: account)) end - desc 'remove-token ACCOUNT', I18n.t('remove_token.desc') + desc 'remove-token ACCOUNT', I18n.t('remove_token_desc') # remove a session token def remove_token(account = nil) account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:token_exists), limited_to: Awskeyring.list_token_names ) Awskeyring.delete_token(account: account, message: I18n.t('message.deltoken', account: account)) end map 'remove-role' => :remove_role - desc 'remove-role ROLE', I18n.t('remove_role.desc') + desc 'remove-role ROLE', I18n.t('remove_role_desc') # remove a role def remove_role(role = nil) role = ask_check( existing: role, message: I18n.t('message.role'), validator: Awskeyring.method(:role_exists), limited_to: Awskeyring.list_role_names ) Awskeyring.delete_role(role_name: role, message: I18n.t('message.delrole', role: role)) end - desc 'rotate ACCOUNT', I18n.t('rotate.desc') + desc 'rotate ACCOUNT', I18n.t('rotate_desc') # rotate Account keys def rotate(account = nil) # rubocop:disable Metrics/MethodLength account = ask_check( existing: account, message: I18n.t('message.account'), @@ -328,11 +340,11 @@ ) puts I18n.t('message.upaccount', account: account) end - desc 'token ACCOUNT [ROLE] [MFA]', I18n.t('token.desc') + desc 'token ACCOUNT [ROLE] [MFA]', I18n.t('token_desc') method_option :role, type: :string, aliases: '-r', desc: I18n.t('method_option.role') method_option :code, type: :string, aliases: '-c', desc: I18n.t('method_option.code') method_option :duration, type: :string, aliases: '-d', desc: I18n.t('method_option.duration') # generate a sessiopn token def token(account = nil, role = nil, code = nil) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength @@ -383,11 +395,11 @@ ) puts I18n.t('message.addtoken', account: account, time: Time.at(new_creds[:expiry].to_i)) end - desc 'console ACCOUNT', I18n.t('console.desc') + desc 'console ACCOUNT', I18n.t('console_desc') method_option :path, type: :string, aliases: '-p', desc: I18n.t('method_option.path') method_option :browser, type: :string, aliases: '-b', desc: I18n.t('method_option.browser') method_option 'no-token', type: :boolean, aliases: '-n', desc: I18n.t('method_option.notoken'), default: false method_option 'no-open', type: :boolean, aliases: '-o', desc: I18n.t('method_option.noopen'), default: false # Open the AWS Console @@ -422,11 +434,11 @@ pid = Process.spawn(spawn_cmd) Process.wait pid end end - desc 'awskeyring CURR PREV', I18n.t('awskeyring.desc'), hide: true + desc "#{File.basename($PROGRAM_NAME)} CURR PREV", I18n.t('awskeyring_desc'), hide: true map File.basename($PROGRAM_NAME) => :autocomplete # autocomplete def autocomplete(curr, prev) comp_line = ENV['COMP_LINE'] unless comp_line @@ -505,10 +517,11 @@ end puts list.select { |elem| elem.start_with?(curr) }.sort!.join("\n") end def list_commands - self.class.all_commands.keys.map { |elem| elem.tr('_', '-') }.reject! { |elem| elem == 'autocomplete' } + commands = self.class.all_commands.keys.map { |elem| elem.tr('_', '-') } + commands.reject! { |elem| %w[autocomplete default].include?(elem) } end def list_arguments(command:) exit 1 if command.empty? self.class.all_commands[command].options.values.map(&:aliases).flatten! +