Skip to content

Commit ef8663a

Browse files
author
Chris Hunt
committed
Replace OptionsParser with Thor
1 parent c3a7527 commit ef8663a

2 files changed

Lines changed: 50 additions & 25 deletions

File tree

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.execute(ARGV)
5+
Github::Auth::CLI.start ARGV

lib/github/auth/cli.rb

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,72 @@
1+
require 'thor'
2+
13
module Github::Auth
24
# Command Line Interface for parsing and executing commands
3-
class CLI
4-
attr_reader :options
5+
class CLI < Thor
6+
option :users, type: :array, required: true
7+
option :command, type: :string
8+
desc 'add', 'Add GitHub users to authorized keys'
9+
long_desc <<-LONGDESC
10+
`gh-auth add` is used to add one or more GitHub user's public SSH keys
11+
to ~/.ssh/authorized_keys. All keys stored on github.com for that
12+
user will be added.
513
6-
def execute(args)
7-
@options = Options.new.parse(args)
8-
send options.command
9-
end
14+
> $ gh-auth add --users=chrishunt zachmargolis
15+
\x5> Adding 6 key(s) to '/Users/chris/.ssh/authorized_keys'
1016
11-
private
17+
By default, users will be granted normal shell access. If you'd like to
18+
specify an ssh command that should execute when the user connects, use
19+
the `--command` option.
1220
21+
> $ gh-auth add --users=chrishunt --command="tmux attach"
22+
LONGDESC
1323
def add
1424
on_keys_file :write!,
15-
"Adding #{keys.count} key(s) to '#{keys_file.path}'"
25+
"Adding #{keys(options[:users]).count} key(s) to '#{keys_file.path}'",
26+
{ command: options[:command] }
1627
end
1728

29+
option :users, type: :array, required: true
30+
desc 'remove', 'Remove GitHub users from authorized keys'
31+
long_desc <<-LONGDESC
32+
`gh-auth remove` is used to remove one or more GitHub user's public SSH
33+
keys from ~/.ssh/authorized_keys. All keys stored on github.com for
34+
that user will be removed.
35+
36+
> $ gh-auth remove --users=chrishunt zachmargolis
37+
\x5> Removing 6 key(s) to '/Users/chris/.ssh/authorized_keys'
38+
LONGDESC
1839
def remove
1940
on_keys_file :delete!,
20-
"Removing #{keys.count} key(s) from '#{keys_file.path}'"
41+
"Removing #{keys(options[:users]).count} key(s) from '#{keys_file.path}'"
2142
end
2243

44+
desc 'list', 'List all GitHub users already added to authorized keys'
45+
long_desc <<-LONGDESC
46+
`gh-auth list` will list all GitHub users that have been added to
47+
~/.ssh/authorized_keys by `gh-auth`.
48+
49+
> $ gh-auth list
50+
\x5> chrishunt, zachmargolis
51+
LONGDESC
2352
def list
24-
puts "Added users: #{keys_file.github_users.join(', ')}"
53+
puts keys_file.github_users.join(', ')
2554
end
2655

56+
desc 'version', 'Show gh-auth version'
2757
def version
2858
puts Github::Auth::VERSION
2959
end
3060

31-
def usage
32-
puts options.usage
61+
private
62+
63+
def keys(usernames = [])
64+
@keys ||= Array(usernames).map { |user| keys_for user }.flatten.compact
3365
end
3466

35-
def on_keys_file(action, message)
67+
def on_keys_file(action, message, options = {})
3668
puts message
37-
rescue_keys_file_errors { keys_file.send action, keys }
69+
rescue_keys_file_errors { keys_file(options).send action, keys }
3870
end
3971

4072
def rescue_keys_file_errors
@@ -51,10 +83,6 @@ def rescue_keys_file_errors
5183
puts " $ touch #{keys_file.path}"
5284
end
5385

54-
def keys
55-
@keys ||= options.usernames.map { |user| keys_for user }.flatten.compact
56-
end
57-
5886
def keys_for(username)
5987
Github::Auth::KeysClient.new(
6088
hostname: github_hostname,
@@ -68,12 +96,9 @@ def keys_for(username)
6896
puts "https://status.github.com"
6997
end
7098

71-
def keys_file
72-
Github::Auth::KeysFile.new keys_file_options
73-
end
74-
75-
def keys_file_options
76-
options.keys_file_options.merge path: keys_file_path
99+
def keys_file(options = {})
100+
Github::Auth::KeysFile.new \
101+
options.merge path: keys_file_path
77102
end
78103

79104
def keys_file_path

0 commit comments

Comments
 (0)