github-key-upload 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,16 +3,56 @@
3
3
  require 'github-key-upload'
4
4
  require 'slop'
5
5
 
6
- opts = Slop.parse(:longest_flag => 8, :help => true) do
6
+ def handle_exit_status(key)
7
+ if key
8
+ puts "OK"
9
+ else
10
+ puts "NOK"
11
+ exit(1)
12
+ end
13
+ end
14
+
15
+ opts = Slop.parse(:longest_flag => 1, :help => true) do
7
16
  banner "github-key-upload [options]\n"
8
17
  on :k, :key=, "Path to the file containing the public key to upload"
9
18
  on :t, :title=, "Key Title"
10
19
  on :u, :user=, "Github username"
11
20
  on :P, :password=, "Github password"
21
+ on :l, "--check-title", "Checks if the supplied key exists (by title)"
22
+ on :c, "--check-key", "Checks if the supplied key exists (by key)"
23
+ on :C, "--check-both", "Checks if the supplied key exists (exact combination of key and title)"
24
+ end
25
+
26
+ exit if opts[:help]
27
+
28
+ if opts[:k].nil? && (opts[:c] || opts[:C] || opts[:l].nil?)
29
+ puts "You need to specify a path to a key (-k or --key)"
30
+ need_to_break = true
31
+ end
32
+
33
+ if opts[:t].nil? && (opts[:l] || opts[:C] || opts[:c].nil?)
34
+ puts "You need to specify a key title (-t or --title)"
35
+ need_to_break = true
36
+ end
37
+
38
+ if opts[:u].nil? || opts[:P].nil?
39
+ puts "You need to specify both username and password (-u or --user and -P or --password)"
40
+ need_to_break = true
12
41
  end
13
42
 
14
- if (opts[:k].nil? || opts[:u].nil? || opts[:P].nil? || opts[:t].nil?)
43
+ if need_to_break
15
44
  puts opts.help
45
+ exit
46
+ end
47
+
48
+ if opts[:l].nil? && opts[:c].nil? && opts[:C].nil?
49
+ Github::Key::Upload.upload opts.to_hash
50
+ elsif opts[:C] || (opts[:l] && opts[:c])
51
+ handle_exit_status(Github::Key::Upload.check_for_both opts.to_hash)
52
+ elsif opts[:l]
53
+ handle_exit_status(Github::Key::Upload.check_for_title opts.to_hash)
54
+ elsif opts[:c]
55
+ handle_exit_status(Github::Key::Upload.check_for_key opts.to_hash)
16
56
  else
17
- Github::Key::Upload.upload(opts.to_hash)
57
+ puts opts.help
18
58
  end
@@ -4,11 +4,63 @@ require "github_api"
4
4
  module Github
5
5
  module Key
6
6
  module Upload
7
- def self.upload(options)
7
+ def self.create_auth(options)
8
8
  github = Github.new basic_auth: "#{options[:user]}:#{options[:password]}"
9
9
  github.oauth.create 'scopes' => ['user']
10
+ github
11
+ end
12
+
13
+ def self.upload(options)
14
+ github = create_auth(options)
10
15
  github.users.keys.create({title: options[:title], key: File.read(options[:key])})
11
16
  end
17
+
18
+ def self.check_for_both(options)
19
+ github = create_auth(options)
20
+ matching_key = nil
21
+
22
+ github.users.keys.all.each_page do |page|
23
+ if page.map {|k| [k["title"], k["key"]]}.include?(
24
+ [options[:title], File.read(options[:key]).split(" ")[0,2].join(" ")])
25
+ matching_key = true
26
+ break
27
+ end
28
+ end
29
+
30
+ matching_key
31
+ end
32
+
33
+ def self.check(options)
34
+ github = create_auth(options)
35
+ matching_key = nil
36
+ search_key = options[:search_key].to_s
37
+ search_proc = options[:search_proc]
38
+
39
+ github.users.keys.all.each_page do |page|
40
+ if page.map {|k| k[search_key]}.include?(search_proc.call(options))
41
+ matching_key = true
42
+ break
43
+ end
44
+ end
45
+
46
+ matching_key
47
+ end
48
+
49
+ def self.check_for_key(options)
50
+ check(options.merge({
51
+ :search_key => :key,
52
+ :search_proc => Proc.new {|opts|
53
+ File.read(opts[:key]).split(" ")[0,2].join(" ")
54
+ }
55
+ }))
56
+ end
57
+
58
+ def self.check_for_title(options)
59
+ check(options.merge({
60
+ :search_key => :title,
61
+ :search_proc => Proc.new {|opts| opts[:title]}
62
+ }))
63
+ end
12
64
  end
13
65
  end
14
66
  end
@@ -1,7 +1,7 @@
1
1
  module Github
2
2
  module Key
3
3
  module Upload
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: github-key-upload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-26 00:00:00.000000000 Z
12
+ date: 2012-10-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: github_api