Skip to content

Commit

Permalink
Merge pull request #54 from servian/detailed-list
Browse files Browse the repository at this point in the history
Add --detail flag for roles.
  • Loading branch information
AzySir authored Nov 27, 2019
2 parents 0d7f99e + 449eb1a commit 18f070f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ language: ruby
os: osx
osx_image: xcode11
rvm:
- 2.3.7
- 2.6.3
- ruby-head
matrix:
Expand Down
1 change: 1 addition & 0 deletions i18n/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ en:
arn: 'AWS role arn.'
code: 'Virtual mfa CODE.'
duration: 'Session DURATION in seconds.'
detail: 'Show more detail.'
key: 'AWS account key id.'
keychain: 'Name of KEYCHAIN to initialise.'
mfa: 'AWS virtual mfa arn.'
Expand Down
5 changes: 5 additions & 0 deletions lib/awskeyring.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ def self.list_role_names
list_roles.map { |elem| elem.attributes[:label][(ROLE_PREFIX.length)..-1] }
end

# Return a list role item names and arns
def self.list_role_names_plus
list_roles.map { |elem| "#{elem.attributes[:label][(ROLE_PREFIX.length)..-1]}\t#{elem.attributes[:account]}" }
end

# Return a list of console paths
def self.list_console_path
prefs.key?('console') ? prefs['console'] : DEFAULT_CONSOLE_LIST
Expand Down
7 changes: 6 additions & 1 deletion lib/awskeyring_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,14 @@ def list

map 'list-role' => :list_role
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
puts Awskeyring.list_role_names.join("\n")
if options['detail']
puts Awskeyring.list_role_names_plus.join("\n")
else
puts Awskeyring.list_role_names.join("\n")
end
end

desc 'env ACCOUNT', I18n.t('env.desc')
Expand Down
7 changes: 7 additions & 0 deletions spec/lib/awskeyring_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
before do
allow(Awskeyring).to receive(:list_account_names).and_return(%w[company personal vibrato])
allow(Awskeyring).to receive(:list_role_names).and_return(%w[admin minion readonly])
allow(Awskeyring).to receive(:list_role_names_plus)
.and_return(%W[admin\tarn1 minion\tarn2 readonly\tarn3])
allow(Awskeyring).to receive(:list_console_path).and_return(%w[iam cloudformation vpc])
end

Expand All @@ -64,6 +66,11 @@
.to output("admin\nminion\nreadonly\n").to_stdout
end

it 'list keychain roles with detail' do
expect { described_class.start(%w[list-role -d]) }
.to output("admin\tarn1\nminion\tarn2\nreadonly\tarn3\n").to_stdout
end

it 'lists accounts with autocomplete' do
ENV['COMP_LINE'] = 'awskeyring token vib'
expect { described_class.start(%w[awskeyring vib token]) }
Expand Down
24 changes: 21 additions & 3 deletions spec/lib/awskeyring_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
let(:role) do
instance_double(
'HashMap',
attributes: { label: 'role test', account: 'arn:aws:iam::012345678901:role/test' },
attributes: { label: 'role role', account: 'arn:aws:iam::012345678901:role/test' },
password: ''
)
end
Expand Down Expand Up @@ -231,12 +231,30 @@
expect { awskeyring.account_not_exists('test') }.to raise_error('Account already exists')
end

it 'lists all accounts' do
expect(awskeyring.list_account_names).to eq(
['test']
)
end

it 'validates a role name' do
expect { awskeyring.role_exists('test') }.not_to raise_error
expect { awskeyring.role_exists('role') }.not_to raise_error
end

it 'invalidates a role name' do
expect { awskeyring.role_not_exists('test') }.to raise_error('Role already exists')
expect { awskeyring.role_not_exists('role') }.to raise_error('Role already exists')
end

it 'lists all roles' do
expect(awskeyring.list_role_names).to eq(
['role']
)
end

it 'lists all roles with detail' do
expect(awskeyring.list_role_names_plus).to eq(
["role\tarn:aws:iam::012345678901:role/test"]
)
end
end
end

0 comments on commit 18f070f

Please sign in to comment.