Skip to content

Commit 8debe6f

Browse files
committed
Merge pull request #16 from chrishunt/option-parser
Use OptionParser for command line options
2 parents acd2ad2 + ff12c44 commit 8debe6f

8 files changed

Lines changed: 128 additions & 123 deletions

File tree

README.md

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,36 @@ After you've [installed](#installation) `gh-auth`, you can give me ssh access
2323
with:
2424

2525
```bash
26-
$ gh-auth add chrishunt
26+
$ gh-auth --add chrishunt
2727
Adding 2 key(s) to '/Users/chris/.ssh/authorized_keys'
2828
```
2929

3030
That was easy! When we're done working, you can revoke my access with:
3131

3232
```bash
33-
$ gh-auth remove chrishunt
33+
$ gh-auth --remove chrishunt
3434
Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys'
3535
```
3636

3737
You can add and remove any number of users at the same time.
3838

3939
```bash
40-
$ gh-auth add chrishunt zachmargolis
40+
$ gh-auth --add chrishunt,zachmargolis
4141
Adding 4 key(s) to '/Users/chris/.ssh/authorized_keys'
4242

43-
$ gh-auth list
43+
$ gh-auth --list
4444
Added users: chrishunt, zachmargolis
4545

46-
$ gh-auth remove chrishunt
46+
$ gh-auth --remove chrishunt
4747
Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys'
4848

49-
$ gh-auth remove zachmargolis
49+
$ gh-auth --list
50+
Added users: zachmargolis
51+
52+
$ gh-auth --remove zachmargolis
5053
Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys'
5154

52-
$ gh-auth list
55+
$ gh-auth --list
5356
Added users:
5457
```
5558

@@ -78,7 +81,13 @@ Added users:
7881
`gh-auth` can be used from the command line after the gem has been installed.
7982

8083
```bash
81-
usage: gh-auth [--version] [add|remove|list] <username>
84+
usage: gh-auth [--version] [--list] [--add|--remove] <username>
85+
86+
options:
87+
--add doug,sally Add GitHub users
88+
--remove doug,sally Remove GitHub users
89+
--list List all GitHub users added
90+
--version Show version
8291
```
8392

8493
### In Your Project
@@ -90,14 +99,10 @@ too.
9099
require 'github/auth'
91100

92101
# Add keys for GitHub user 'chrishunt'
93-
Github::Auth::CLI.new(%w(
94-
add chrishunt
95-
)).execute
102+
Github::Auth::CLI.new.execute %w(--add chrishunt)
96103

97104
# Remove keys for GitHub user 'chrishunt'
98-
Github::Auth::CLI.new(%w(
99-
remove chrishunt
100-
)).execute
105+
Github::Auth::CLI.new.execute %w(--remove chrishunt)
101106
```
102107

103108
## Installation
@@ -108,7 +113,13 @@ Install the `github-auth` gem:
108113
$ gem install github-auth
109114

110115
$ gh-auth
111-
usage: gh-auth [--version] [add|remove|list] <username>
116+
usage: gh-auth [--version] [--list] [--add|--remove] <username>
117+
118+
options:
119+
--add doug,sally Add GitHub users
120+
--remove doug,sally Remove GitHub users
121+
--list List all GitHub users added
122+
--version Show version
112123
```
113124

114125
### SSH Public Key Authentication (Mac OS X)
@@ -136,7 +147,7 @@ First, authorize yourself for ssh. (Make sure to replace 'chrishunt' with
136147
*your* GitHub username)
137148

138149
```bash
139-
$ gh-auth add chrishunt
150+
$ gh-auth --add chrishunt
140151
Adding 2 key(s) to '/Users/chris/.ssh/authorized_keys'
141152
```
142153

@@ -152,7 +163,7 @@ $ ssh -o PreferredAuthentications=publickey localhost
152163
Next, remove your public keys from the keys file:
153164

154165
```bash
155-
$ gh-auth remove chrishunt
166+
$ gh-auth --remove chrishunt
156167
Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys'
157168
```
158169

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ desc 'Check code quality'
1212
Cane::RakeTask.new(:quality) do |task|
1313
task.abc_max = 9
1414
task.use Cane::HashCheck
15+
task.abc_exclude = %w(Auth::Options#initialize)
1516
end
1617

1718
task default: :spec

bin/gh-auth

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
require 'github/auth'
44

5-
Github::Auth::CLI.new(ARGV).execute
5+
Github::Auth::CLI.new.execute(ARGV)

lib/github/auth.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
require 'github/auth/key'
33
require 'github/auth/keys_client'
44
require 'github/auth/keys_file'
5+
require 'github/auth/options'
56
require 'github/auth/cli'

lib/github/auth/cli.rb

Lines changed: 18 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,21 @@
11
module Github::Auth
22
# Command Line Interface for parsing and executing commands
33
class CLI
4-
attr_reader :command, :usernames
4+
attr_reader :options
55

6-
COMMANDS = %w(add remove list)
7-
8-
def initialize(argv)
9-
@command = argv.shift
10-
@usernames = argv
11-
end
12-
13-
def execute
14-
if COMMANDS.include?(command)
15-
send command
16-
elsif command == '--version'
17-
print_version
18-
else
19-
print_usage
20-
end
6+
def execute(args)
7+
@options = Options.new.parse(args)
8+
send options.command
219
end
2210

2311
private
2412

2513
def add
26-
if usernames.empty?
27-
print_usage
28-
return
29-
end
30-
3114
on_keys_file :write!,
3215
"Adding #{keys.count} key(s) to '#{keys_file.path}'"
3316
end
3417

3518
def remove
36-
if usernames.empty?
37-
print_usage
38-
return
39-
end
40-
4119
on_keys_file :delete!,
4220
"Removing #{keys.count} key(s) from '#{keys_file.path}'"
4321
end
@@ -46,6 +24,14 @@ def list
4624
puts "Added users: #{keys_file.github_users.join(', ')}"
4725
end
4826

27+
def version
28+
puts Github::Auth::VERSION
29+
end
30+
31+
def usage
32+
puts options.usage
33+
end
34+
4935
def on_keys_file(action, message)
5036
puts message
5137
rescue_keys_file_errors { keys_file.send action, keys }
@@ -54,45 +40,19 @@ def on_keys_file(action, message)
5440
def rescue_keys_file_errors
5541
yield
5642
rescue KeysFile::PermissionDeniedError
57-
print_permission_denied
58-
rescue KeysFile::FileDoesNotExistError
59-
print_file_does_not_exist
60-
end
61-
62-
def print_usage
63-
puts "usage: gh-auth [--version] [#{COMMANDS.join '|'}] <username>"
64-
end
65-
66-
def print_version
67-
puts "gh-auth version #{Github::Auth::VERSION}"
68-
end
69-
70-
def print_permission_denied
7143
puts 'Permission denied!'
7244
puts
7345
puts "Make sure you have write permissions for '#{keys_file.path}'"
74-
end
75-
76-
def print_file_does_not_exist
46+
rescue KeysFile::FileDoesNotExistError
7747
puts "Keys file does not exist!"
7848
puts
7949
puts "Create one now and try again:"
8050
puts
8151
puts " $ touch #{keys_file.path}"
8252
end
8353

84-
def print_github_user_does_not_exist(username)
85-
puts "Github user '#{username}' does not exist"
86-
end
87-
88-
def print_github_unavailable
89-
puts "Github appears to be unavailable :("
90-
puts
91-
puts "https://status.github.com"
92-
end
93-
9454
def keys
95-
@keys ||= usernames.map { |username| keys_for username }.flatten.compact
55+
@keys ||= options.usernames.map { |user| keys_for user }.flatten.compact
9656
end
9757

9858
def keys_for(username)
@@ -101,9 +61,11 @@ def keys_for(username)
10161
username: username
10262
).keys
10363
rescue Github::Auth::KeysClient::GithubUserDoesNotExistError
104-
print_github_user_does_not_exist username
64+
puts "Github user '#{username}' does not exist"
10565
rescue Github::Auth::KeysClient::GithubUnavailableError
106-
print_github_unavailable
66+
puts "Github appears to be unavailable :("
67+
puts
68+
puts "https://status.github.com"
10769
end
10870

10971
def keys_file

lib/github/auth/options.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require 'optparse'
2+
3+
module Github::Auth
4+
# Parses command line options
5+
class Options
6+
attr_reader :parser
7+
8+
def initialize
9+
@parser = OptionParser.new do |opts|
10+
opts.banner = [
11+
'usage: gh-auth',
12+
'[--version]',
13+
'[--list]',
14+
'[--add|--remove]',
15+
'<username>'
16+
].join(' ')
17+
18+
opts.separator "\noptions:"
19+
20+
opts.on(
21+
'--add doug,sally', Array, 'Add GitHub users'
22+
) do |usernames|
23+
raise OptionParser::MissingArgument if usernames.empty?
24+
@command = 'add'
25+
@usernames = usernames
26+
end
27+
28+
opts.on(
29+
'--remove doug,sally', Array, 'Remove GitHub users'
30+
) do |usernames|
31+
raise OptionParser::MissingArgument if usernames.empty?
32+
@command = 'remove'
33+
@usernames = usernames
34+
end
35+
36+
opts.on('--list', 'List all GitHub users added') do
37+
@command = 'list'
38+
end
39+
40+
opts.on('--version', 'Show version') do
41+
@command = 'version'
42+
end
43+
end
44+
end
45+
46+
def command
47+
@command ||= 'usage'
48+
end
49+
50+
def usernames
51+
@usernames ||= []
52+
end
53+
54+
def usage
55+
parser.help
56+
end
57+
58+
def parse(args)
59+
parser.parse(args)
60+
ensure
61+
return self
62+
end
63+
end
64+
end

spec/acceptance/github/auth/cli_spec.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
after { keys_file.unlink }
1313

14-
def cli(argv)
15-
described_class.new(argv).tap do |cli|
14+
def cli
15+
described_class.new.tap do |cli|
1616
cli.stub(
1717
github_hostname: hostname,
1818
keys_file_path: keys_file.path
@@ -21,35 +21,43 @@ def cli(argv)
2121
end
2222

2323
it 'adds and removes keys from the keys file' do
24-
cli(%w(add chrishunt)).execute
24+
cli.execute %w(--add chrishunt)
2525

2626
keys_file.read.tap do |keys_file_content|
2727
keys.each { |key| expect(keys_file_content).to include key.to_s }
2828
end
2929

30-
cli(%w(remove chrishunt)).execute
30+
cli.execute %w(--remove chrishunt)
3131

3232
expect(keys_file.read).to be_empty
3333

3434
keys_file.unlink
3535
end
3636

3737
it 'lists users from the keys file' do
38-
cli(%w(add chrishunt)).execute
38+
cli.execute %w(--add chrishunt)
3939

4040
output = capture_stdout do
41-
cli(%w(list)).execute
41+
cli.execute %w(--list)
4242
end
4343

4444
expect(output).to include('chrishunt')
4545
end
4646

4747
it 'prints version information' do
4848
output = capture_stdout do
49-
cli(%w(--version)).execute
49+
cli.execute %w(--version)
5050
end
5151

5252
expect(output).to include Github::Auth::VERSION
5353
end
54+
55+
it 'prints usage for invalid arguments' do
56+
[[], %w(invalid), %w(--add)].each do |invalid_arguments|
57+
expect(
58+
capture_stdout { cli.execute invalid_arguments }
59+
).to include 'usage: gh-auth'
60+
end
61+
end
5462
end
5563
end

0 commit comments

Comments
 (0)