Skip to content

Commit

Permalink
Merge pull request #551 from DannyBen/add/completions-visibility
Browse files Browse the repository at this point in the history
Update completions library to consider private visibility setting
  • Loading branch information
DannyBen authored Aug 15, 2024
2 parents af15e64 + e16dde1 commit 57ee4ac
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/bashly/concerns/completions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def completion_data(with_version: true)
end
end

public_commands.each do |command|
visible_commands.each do |command|
result.merge! command.completion_data(with_version: false)
end

Expand Down Expand Up @@ -62,7 +62,7 @@ def completion_generator
end

def completion_flag_names
public_flags.map(&:name) + public_flags.map(&:short)
visible_flags.map(&:name) + public_flags.map(&:short)
end

def completion_allowed_args
Expand All @@ -73,7 +73,7 @@ def completion_words(with_version: false)
trivial_flags = %w[--help -h]
trivial_flags += %w[--version -v] if with_version
all = (
public_command_aliases + trivial_flags +
visible_command_aliases + trivial_flags +
completion_flag_names + completion_allowed_args
)

Expand Down
11 changes: 11 additions & 0 deletions lib/bashly/script/introspection/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ def public_commands
def public_command_aliases
public_commands.map(&:aliases).flatten
end

# Returns only public commands, or both public and private commands
# if Settings.private_reveal_key is set
def visible_commands
Settings.private_reveal_key ? commands : public_commands
end

# Returns a full list of the visible Command names and aliases combined
def visible_command_aliases
visible_commands.map(&:aliases).flatten
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/bashly/script/introspection/flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ def short_flag_exist?(flag)
flags.any? { |f| f.short == flag }
end

# Returns only public flags, or both public and private flags if
# Settings.private_reveal_key is set
def visible_flags
Settings.private_reveal_key ? flags : public_flags
end

# Returns an array of all the flags with a whitelist arg
def whitelisted_flags
flags.select(&:allowed)
Expand Down
40 changes: 39 additions & 1 deletion spec/bashly/script/introspection/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,49 @@
end
end

describe '#public_commands_aliases' do
describe '#public_command_aliases' do
let(:fixture) { :private_commands }

it 'returns an array of command aliases of public subcommands' do
expect(subject.public_command_aliases).to eq %w[connect c]
end
end

describe '#visible_commands' do
let(:fixture) { :private_commands }

it 'returns public commands only (same as #public_commands)' do
expect(subject.visible_commands.size).to eq 1
expect(subject.visible_commands.first.name).to eq 'connect'
end

context 'when Settings.private_reveal_key is set' do
before { Settings.private_reveal_key = 'SHOW' }
after { Settings.private_reveal_key = nil }

it 'returns all commands (same as #commands)' do
expect(subject.visible_commands.size).to eq 3
expect(subject.visible_commands[1].name).to eq 'connect-ftp'
end
end
end

describe '#visible_command_aliases' do
let(:fixture) { :private_commands }

it 'returns an array of command aliases of public subcommands' do
expect(subject.visible_command_aliases).to eq %w[connect c]
end

context 'when Settings.private_reveal_key is set' do
before { Settings.private_reveal_key = 'SHOW' }
after { Settings.private_reveal_key = nil }

it 'returns an array of command aliases of all subcommands' do
expect(subject.visible_command_aliases).to eq %w[
connect c connect-ftp cf connect-ssh cs
]
end
end
end
end
19 changes: 19 additions & 0 deletions spec/bashly/script/introspection/flags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@
end
end

describe '#visible_flags' do
let(:fixture) { :private_flags }

it 'returns public flags only (same as #public_flags)' do
expect(subject.visible_flags.size).to eq 1
expect(subject.visible_flags.first.long).to eq '--new'
end

context 'when Settings.private_reveal_key is set' do
before { Settings.private_reveal_key = 'SHOW' }
after { Settings.private_reveal_key = nil }

it 'returns all flags (same as #flags)' do
expect(subject.visible_flags.size).to eq 2
expect(subject.visible_flags.first.long).to eq '--legacy'
end
end
end

describe '#whitelisted_flags' do
let(:fixture) { :whitelist }

Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/script/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@
alias: cf
private: true
- name: connect-ssh
alias: cf
alias: cs
private: true

:private_flags:
Expand Down

0 comments on commit 57ee4ac

Please sign in to comment.