Class: GithubAuthorizedKeys::CLI
- Inherits:
-
Object
- Object
- GithubAuthorizedKeys::CLI
- Defined in:
- lib/github_authorized_keys.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Instance Method Summary collapse
- #fetch_keys(login) ⇒ Object
- #fetch_members ⇒ Object
- #github_http_get(url, headers) ⇒ Object
- #load_config(config_file) ⇒ Object
- #read_original_authorized_keys ⇒ Object
- #run(config_file) ⇒ Object
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/github_authorized_keys.rb', line 7 def config @config end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/github_authorized_keys.rb', line 7 def headers @headers end |
Instance Method Details
#fetch_keys(login) ⇒ Object
51 52 53 |
# File 'lib/github_authorized_keys.rb', line 51 def fetch_keys(login) github_http_get("/users/#{login}/keys?#{config['oauth_token']}", headers) end |
#fetch_members ⇒ Object
47 48 49 |
# File 'lib/github_authorized_keys.rb', line 47 def fetch_members github_http_get("/orgs/#{config['organization']}/members?#{config['oauth_token']}", headers) end |
#github_http_get(url, headers) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/github_authorized_keys.rb', line 37 def github_http_get(url, headers) unless @github_http @github_http = Net::HTTP.new('api.github.com', 443) @github_http.use_ssl = true @github_http.verify_mode = OpenSSL::SSL::VERIFY_NONE end JSON.parse(@github_http.request_get(url, headers).body) end |
#load_config(config_file) ⇒ Object
55 56 57 58 |
# File 'lib/github_authorized_keys.rb', line 55 def load_config(config_file) config_file ||= "#{ENV['HOME']}/.github_authorized_keys.yml" @config = YAML.load_file(config_file) end |
#read_original_authorized_keys ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/github_authorized_keys.rb', line 60 def File.open("#{ENV['HOME']}/.ssh/authorized_keys") do |file| while(line = file.gets) puts line end end rescue '' # file does not exist end |
#run(config_file) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/github_authorized_keys.rb', line 9 def run(config_file) begin load_config(config_file) @headers = {'User-Agent' => "#{config['organization']} authorized_keys generator"} = [ '### THIS FILE IS AUTOMATICALLY GENERATED', ] if config.include?('additional_keys') .concat(config['additional_keys']) end fetch_members.each do |member| << "# #{member['login']}" fetch_keys(member['login']).each do |ssh_key| << ssh_key['key'] end end .join("\n") rescue Errno::ENOENT $stderr.puts "Unable to read configuration file: '#{config_file}'" unless $testing rescue end end |