Skip to content

Commit 5b0514d

Browse files
authored
Merge pull request #389 from DannyBen/fix/private-commands-in-completion
Fix completions library to not show private commands and flags
2 parents 9048c9b + 803aebb commit 5b0514d

File tree

10 files changed

+99
-3
lines changed

10 files changed

+99
-3
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Metrics/PerceivedComplexity: { Exclude: [lib/bashly/config_validator.rb] }
1919
Metrics/MethodLength: { Exclude: [lib/bashly/config_validator.rb] }
2020
Style/GuardClause: { Exclude: [lib/bashly/config_validator.rb] }
2121

22+
# FIXME: The `Command` class is too long
23+
Metrics/ClassLength: { Exclude: [lib/bashly/script/command.rb] }
24+
2225
# Allow irregular filenames in some cases
2326
RSpec/FilePath:
2427
Exclude:

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-
commands.each do |command|
32+
public_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-
flags.map(&:name) + flags.map(&:short)
65+
public_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-
command_aliases + trivial_flags +
76+
public_command_aliases + trivial_flags +
7777
completion_flag_names + completion_allowed_args
7878
)
7979

lib/bashly/script/command.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ def public_commands
223223
commands.reject(&:private)
224224
end
225225

226+
# Returns a full list of the public Command names and aliases combined
227+
def public_command_aliases
228+
public_commands.map(&:aliases).flatten
229+
end
230+
226231
# Returns only environment variables that are not private
227232
def public_environment_variables
228233
environment_variables.reject(&:private)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
+ bundle exec bashly add completions_yaml --force
2+
created ./completions.yml
3+
4+
This file can be converted to a completions script using the completely gem.
5+
6+
+ cat completions.yml
7+
---
8+
private:
9+
- "--help"
10+
- "--version"
11+
- "-h"
12+
- "-v"
13+
- c
14+
- connect
15+
private connect:
16+
- "--force"
17+
- "--help"
18+
- "-f"
19+
- "-h"
20+
private c:
21+
- "--force"
22+
- "--help"
23+
- "-f"
24+
- "-h"

spec/bashly/script/command_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,23 @@
339339
end
340340
end
341341

342+
describe '#public_commands' do
343+
let(:fixture) { :private_commands }
344+
345+
it 'returns an array of Command objects excluding private commands' do
346+
expect(subject.public_commands.count).to eq 1
347+
expect(subject.public_commands.first.name).to eq 'connect'
348+
end
349+
end
350+
351+
describe '#public_commands_aliases' do
352+
let(:fixture) { :private_commands }
353+
354+
it 'returns an array of command aliases of public subcommands' do
355+
expect(subject.public_command_aliases).to eq %w[connect c]
356+
end
357+
end
358+
342359
describe '#user_file_path' do
343360
it 'returns the path to the user file' do
344361
expect(subject.user_file_path 'test.sh').to eq 'spec/tmp/src/test.sh'

spec/fixtures/script/commands.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,19 @@
378378
short: -c
379379
allowed: [green, red]
380380

381+
:private_commands:
382+
name: private
383+
help: Test private commands
384+
commands:
385+
- name: connect
386+
alias: c
387+
- name: connect-ftp
388+
alias: cf
389+
private: true
390+
- name: connect-ssh
391+
alias: cf
392+
private: true
393+
381394
:repeatable_arg:
382395
name: get
383396
args:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
completions.yml
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This fixture tests that private commands and flags are not added to the
2+
generated completions scripts.
3+
4+
Reference issue: https://github.com/DannyBen/bashly/issues/388
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: private
2+
help: Test private commands
3+
4+
commands:
5+
- name: connect
6+
alias: c
7+
help: Connect to the metaverse
8+
flags:
9+
- long: --force
10+
short: -f
11+
# Private flag below - should not be present in completions
12+
- long: --hidden-flag
13+
short: -d
14+
private: true
15+
16+
# Private commands below - should not be present in completions
17+
- name: connect-ftp
18+
help: Connect via FTP
19+
alias: cf
20+
private: true
21+
- name: connect-ssh
22+
help: Connect via SSH
23+
alias: cs
24+
private: true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
bundle exec bashly add completions_yaml --force
5+
cat completions.yml

0 commit comments

Comments
 (0)