Skip to content

Commit dc7190f

Browse files
authored
Merge pull request #45 from DannyBen/add/install-command
Add `completely install` command
2 parents dfa5cff + ac276fb commit dc7190f

File tree

13 files changed

+241
-2
lines changed

13 files changed

+241
-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: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
require 'completely/commands/base'
2+
3+
module Completely
4+
module Commands
5+
class Install < Base
6+
TARGETS = %W[
7+
/usr/share/bash-completion/completions
8+
/usr/local/etc/bash_completion.d
9+
#{Dir.home}/.bash_completion.d
10+
]
11+
12+
summary 'Install a bash completion script'
13+
14+
help <<~HELP
15+
This command will copy the specified file to one of the following directories:
16+
17+
#{TARGETS.map { |c| " - #{c}" }.join "\n"}
18+
19+
The target filename will be the program name, and sudo will be used if necessary.
20+
HELP
21+
22+
usage 'completely install PROGRAM [SCRIPT_PATH --force --dry]'
23+
usage 'completely install (-h|--help)'
24+
25+
option '-f --force', 'Overwrite target file if it exists'
26+
option '-d --dry', 'Show the installation command but do not run it'
27+
28+
param 'PROGRAM', 'Name of the program the completions are for.'
29+
param 'SCRIPT_PATH', 'Path to the source bash script [default: completely.bash].'
30+
31+
def run
32+
bounce
33+
34+
if args['--dry']
35+
puts command.join ' '
36+
return
37+
end
38+
39+
success = system(*command)
40+
raise "Failed running command:\nnb`#{command.join ' '}`" unless success
41+
42+
say "Saved m`#{target_path}`"
43+
say 'You may need to restart your session to test it'
44+
end
45+
46+
private
47+
48+
def bounce
49+
unless completions_path
50+
raise 'Cannot determine system completions directory'
51+
end
52+
53+
unless File.exist? script_path
54+
raise "Cannot find script: m`#{script_path}`"
55+
end
56+
57+
if target_exist? && !args['--force']
58+
raise "File exists: m`#{target_path}`\nUse nb`--force` to overwrite"
59+
end
60+
end
61+
62+
def target_exist?
63+
File.exist? target_path
64+
end
65+
66+
def command
67+
result = root? ? [] : %w[sudo]
68+
result + %W[cp #{script_path} #{target_path}]
69+
end
70+
71+
def script_path
72+
args['SCRIPT_PATH'] || 'completely.bash'
73+
end
74+
75+
def target_path
76+
"#{completions_path}/#{args['PROGRAM']}"
77+
end
78+
79+
def root?
80+
Process.uid.zero?
81+
end
82+
83+
def completions_path
84+
@completions_path ||= completions_path!
85+
end
86+
87+
def completions_path!
88+
TARGETS.each do |tarnet|
89+
return tarnet if Dir.exist? tarnet
90+
end
91+
92+
nil
93+
end
94+
end
95+
end
96+
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

spec/approvals/cli/install/dry

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sudo cp README.md /usr/share/bash-completion/completions/completely-test

spec/approvals/cli/install/help

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Install a bash completion script
2+
3+
This command will copy the specified file to one of the following directories:
4+
5+
- /usr/share/bash-completion/completions
6+
- /usr/local/etc/bash_completion.d
7+
- /home/vagrant/.bash_completion.d
8+
9+
The target filename will be the program name, and sudo will be used if
10+
necessary.
11+
12+
Usage:
13+
completely install PROGRAM [SCRIPT_PATH --force --dry]
14+
completely install (-h|--help)
15+
16+
Options:
17+
-f --force
18+
Overwrite target file if it exists
19+
20+
-d --dry
21+
Show the installation command but do not run it
22+
23+
-h --help
24+
Show this help
25+
26+
Parameters:
27+
PROGRAM
28+
Name of the program the completions are for.
29+
30+
SCRIPT_PATH
31+
Path to the source bash script [default: completely.bash].
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Saved /usr/share/bash-completion/completions/completely-test
2+
You may need to restart your session to test it
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Saved /usr/share/bash-completion/completions/completely-test
2+
You may need to restart your session to test it
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<RuntimeError: Cannot find script: m`completely.bash`>

spec/approvals/cli/install/no-args

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Usage:
2+
completely install PROGRAM [SCRIPT_PATH --force --dry]
3+
completely install (-h|--help)

0 commit comments

Comments
 (0)