Skip to content

Commit e16dde1

Browse files
committed
- Update completions library to consider private visibility setting
1 parent af15e64 commit e16dde1

File tree

6 files changed

+79
-5
lines changed

6 files changed

+79
-5
lines changed

lib/bashly/concerns/completions.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def completion_data(with_version: true)
2929
end
3030
end
3131

32-
public_commands.each do |command|
32+
visible_commands.each do |command|
3333
result.merge! command.completion_data(with_version: false)
3434
end
3535

@@ -62,7 +62,7 @@ def completion_generator
6262
end
6363

6464
def completion_flag_names
65-
public_flags.map(&:name) + public_flags.map(&:short)
65+
visible_flags.map(&:name) + public_flags.map(&:short)
6666
end
6767

6868
def completion_allowed_args
@@ -73,7 +73,7 @@ def completion_words(with_version: false)
7373
trivial_flags = %w[--help -h]
7474
trivial_flags += %w[--version -v] if with_version
7575
all = (
76-
public_command_aliases + trivial_flags +
76+
visible_command_aliases + trivial_flags +
7777
completion_flag_names + completion_allowed_args
7878
)
7979

lib/bashly/script/introspection/commands.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ def public_commands
9494
def public_command_aliases
9595
public_commands.map(&:aliases).flatten
9696
end
97+
98+
# Returns only public commands, or both public and private commands
99+
# if Settings.private_reveal_key is set
100+
def visible_commands
101+
Settings.private_reveal_key ? commands : public_commands
102+
end
103+
104+
# Returns a full list of the visible Command names and aliases combined
105+
def visible_command_aliases
106+
visible_commands.map(&:aliases).flatten
107+
end
97108
end
98109
end
99110
end

lib/bashly/script/introspection/flags.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ def short_flag_exist?(flag)
4242
flags.any? { |f| f.short == flag }
4343
end
4444

45+
# Returns only public flags, or both public and private flags if
46+
# Settings.private_reveal_key is set
47+
def visible_flags
48+
Settings.private_reveal_key ? flags : public_flags
49+
end
50+
4551
# Returns an array of all the flags with a whitelist arg
4652
def whitelisted_flags
4753
flags.select(&:allowed)

spec/bashly/script/introspection/commands_spec.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,49 @@
109109
end
110110
end
111111

112-
describe '#public_commands_aliases' do
112+
describe '#public_command_aliases' do
113113
let(:fixture) { :private_commands }
114114

115115
it 'returns an array of command aliases of public subcommands' do
116116
expect(subject.public_command_aliases).to eq %w[connect c]
117117
end
118118
end
119+
120+
describe '#visible_commands' do
121+
let(:fixture) { :private_commands }
122+
123+
it 'returns public commands only (same as #public_commands)' do
124+
expect(subject.visible_commands.size).to eq 1
125+
expect(subject.visible_commands.first.name).to eq 'connect'
126+
end
127+
128+
context 'when Settings.private_reveal_key is set' do
129+
before { Settings.private_reveal_key = 'SHOW' }
130+
after { Settings.private_reveal_key = nil }
131+
132+
it 'returns all commands (same as #commands)' do
133+
expect(subject.visible_commands.size).to eq 3
134+
expect(subject.visible_commands[1].name).to eq 'connect-ftp'
135+
end
136+
end
137+
end
138+
139+
describe '#visible_command_aliases' do
140+
let(:fixture) { :private_commands }
141+
142+
it 'returns an array of command aliases of public subcommands' do
143+
expect(subject.visible_command_aliases).to eq %w[connect c]
144+
end
145+
146+
context 'when Settings.private_reveal_key is set' do
147+
before { Settings.private_reveal_key = 'SHOW' }
148+
after { Settings.private_reveal_key = nil }
149+
150+
it 'returns an array of command aliases of all subcommands' do
151+
expect(subject.visible_command_aliases).to eq %w[
152+
connect c connect-ftp cf connect-ssh cs
153+
]
154+
end
155+
end
156+
end
119157
end

spec/bashly/script/introspection/flags_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@
9191
end
9292
end
9393

94+
describe '#visible_flags' do
95+
let(:fixture) { :private_flags }
96+
97+
it 'returns public flags only (same as #public_flags)' do
98+
expect(subject.visible_flags.size).to eq 1
99+
expect(subject.visible_flags.first.long).to eq '--new'
100+
end
101+
102+
context 'when Settings.private_reveal_key is set' do
103+
before { Settings.private_reveal_key = 'SHOW' }
104+
after { Settings.private_reveal_key = nil }
105+
106+
it 'returns all flags (same as #flags)' do
107+
expect(subject.visible_flags.size).to eq 2
108+
expect(subject.visible_flags.first.long).to eq '--legacy'
109+
end
110+
end
111+
end
112+
94113
describe '#whitelisted_flags' do
95114
let(:fixture) { :whitelist }
96115

spec/fixtures/script/commands.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@
437437
alias: cf
438438
private: true
439439
- name: connect-ssh
440-
alias: cf
440+
alias: cs
441441
private: true
442442

443443
:private_flags:

0 commit comments

Comments
 (0)