Skip to content

Commit 2fbd879

Browse files
committed
- Add completely install command
1 parent dfa5cff commit 2fbd879

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

.rubocop.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ AllCops:
1818
Naming/AccessorMethodName:
1919
Exclude:
2020
- 'lib/completely/tester.rb'
21+
22+
# The `bounce` method here is more readable without this cop
23+
Style/GuardClause:
24+
Exclude:
25+
- 'lib/completely/commands/install.rb'

lib/completely/cli.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
require 'mister_bin'
2+
require 'completely/version'
3+
require 'completely/commands/generate'
24
require 'completely/commands/init'
5+
require 'completely/commands/install'
36
require 'completely/commands/preview'
4-
require 'completely/commands/generate'
57
require 'completely/commands/test'
6-
require 'completely/version'
78

89
module Completely
910
class CLI
@@ -16,6 +17,7 @@ def self.runner
1617
runner.route 'preview', to: Commands::Preview
1718
runner.route 'generate', to: Commands::Generate
1819
runner.route 'test', to: Commands::Test
20+
runner.route 'install', to: Commands::Install
1921

2022
runner
2123
end

lib/completely/commands/install.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
require 'completely/commands/base'
2+
3+
module Completely
4+
module Commands
5+
class Install < Base
6+
help 'Install a bash completion script'
7+
8+
usage 'completely install PROGRAM [SCRIPT_PATH --force]'
9+
usage 'completely install (-h|--help)'
10+
11+
option '-f --force', 'Overwrite target file if it exists'
12+
13+
param 'PROGRAM', 'Name of the program the completions are for.'
14+
param 'SCRIPT_PATH', 'Path to the source bash script [default: completely.bash].'
15+
16+
def run
17+
bounce
18+
success = system(*command)
19+
raise "Failed running command:\nnb`#{command.join ' '}`" unless success
20+
21+
say "Saved m`#{target_path}`"
22+
end
23+
24+
private
25+
26+
def bounce
27+
unless completions_path
28+
raise 'Cannot determine system completions directory'
29+
end
30+
31+
unless File.exist? script_path
32+
raise "Cannot find script: m`#{script_path}`"
33+
end
34+
35+
if File.exist?(target_path) && !args['--force']
36+
raise "File exists: m`#{target_path}`\nUse nb`--force` to overwrite"
37+
end
38+
end
39+
40+
def command
41+
result = root? ? [] : %w[sudo]
42+
result + %W[cp #{script_path} #{target_path}]
43+
end
44+
45+
def script_path
46+
args['SCRIPT_PATH'] || 'completely.bash'
47+
end
48+
49+
def target_path
50+
"#{completions_path}/#{args['PROGRAM']}"
51+
end
52+
53+
def root?
54+
Process.uid.zero?
55+
end
56+
57+
def completions_path
58+
@completions_path ||= completions_path!
59+
end
60+
61+
def completions_path!
62+
candidates = %w[
63+
/usr/share/bash-completion/completions
64+
/usr/local/etc/bash_completion.d
65+
]
66+
67+
candidates.each do |candidate|
68+
return candidate if Dir.exist? candidate
69+
end
70+
71+
nil
72+
end
73+
end
74+
end
75+
end

spec/approvals/cli/commands

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ Commands:
55
preview Generate the bash completion script to STDOUT
66
generate Generate the bash completion script to a file
77
test Test completions
8+
install Install a bash completion script
89

910
Run completely COMMAND --help for more information

0 commit comments

Comments
 (0)