hu 0.0.1 → 0.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d8d4e0d3178c2a45526d2483041fa781115a450
4
- data.tar.gz: a8ca6f62f38b06a8c47881bb51b19b9a725e6628
3
+ metadata.gz: 135742548802f1cf65c42791d2c27839da18c4d6
4
+ data.tar.gz: 5873b031208439a1f552d4a1bdedf8864d592b96
5
5
  SHA512:
6
- metadata.gz: 4c3c895b4145911370891b7f7e9274d1226b0ce4439b1080cd9b572a43f91097b0fcbe99a1c45a1669236df903aaab99947e354fba9a4c5fa2e04aa7976f60b7
7
- data.tar.gz: 184285ed9316e0206f25c4d35bec3992a7c528011f3f3988de123e41625e67179be3b91ec4c2cbcfe28fc318de2b1b5a9e5a74cb531254cc9cd99cd738c1d658
6
+ metadata.gz: 362abd3e5beed4cb0ffbfb96610be2663c2afe689ff858417375cf6f06d15e436902a4156faf994820093f16be30cb959d0cbfe39ef4d523c4c8edeba7e0d136
7
+ data.tar.gz: e2b6081abd344ee8f36a2b06bd1095d1594dfb7b675631bdc9ecc78c58849b0e9b41774433fc0897f9b48e56fdcd6dec22aca28ed014b35ae81dd94edfc04b95
data/hu.gemspec CHANGED
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_dependency "optix"
25
25
  spec.add_dependency "platform-api"
26
26
  spec.add_dependency "powerbar"
27
+ spec.add_dependency "hashdiff"
27
28
  end
@@ -4,15 +4,18 @@ require 'powerbar'
4
4
  require 'yaml'
5
5
  require 'platform-api'
6
6
 
7
+ require 'hu/collab'
8
+
7
9
  module Hu
8
10
  class Cli < Optix::Cli
9
11
  API_TOKEN = ENV['HEROKU_API_TOKEN']
10
- cli_root do
12
+ Optix::command do
11
13
  text "Hu v#{Hu::VERSION} - Heroku Utility"
12
14
  if API_TOKEN.nil?
13
15
  text ""
14
16
  text "\e[1mWARNING: Environment variable 'HEROKU_API_TOKEN' must be set.\e[0m"
15
17
  end
18
+ opt :quiet, "Don't show progress bar", :default => false
16
19
  opt :version, "Print version and exit", :short => :none
17
20
  trigger :version do
18
21
  puts "Hu v#{Hu::VERSION}"
@@ -23,37 +26,11 @@ module Hu
23
26
  exit 1
24
27
  end
25
28
  end
26
- end
27
-
28
- desc "Export current collaborator mapping"
29
- opt :format, "yaml|json", :default => 'yaml'
30
- parent "collab", "Application collaborators"
31
- def export(cmd, opts, argv)
32
- #raise Optix::HelpNeeded unless opts.values_at(:capslock, :numlock, :verbose).any?
33
- data = {}
34
- pb = PowerBar.new
35
- app_names = h.app.list.map{|e| e['name']}
36
- app_names.each_with_index do |app_name,i|
37
- pb.show :msg => app_name, :total => app_names.length, :done => i
38
- data[app_name] = { 'collaborators' => [] }
39
- h.collaborator.list(app_name).map{|e|
40
- case e['role']
41
- when 'owner'
42
- data[app_name]['owner'] = e['user']['email']
43
- when nil
44
- data[app_name]['collaborators'] << e['user']['email']
45
- else
46
- raise RuntimeError, "Unknown collaborator role: #{e['role']}"
47
- end
48
- }
29
+ filter do |cmd, opts, argv|
30
+ $quiet = opts[:quiet]
31
+ $quiet = true unless STDOUT.isatty
49
32
  end
50
- pb.wipe
51
- puts data.send("to_#{opts[:format]}".to_sym)
52
33
  end
53
-
54
- def h
55
- @h ||= PlatformAPI.connect_oauth(API_TOKEN)
56
- end
57
-
58
34
  end
59
35
  end
36
+
@@ -0,0 +1,123 @@
1
+ require 'powerbar'
2
+ require 'yaml'
3
+ require 'hashdiff'
4
+ require 'platform-api'
5
+
6
+ module Hu
7
+ class Cli < Optix::Cli
8
+ class Collab < Optix::Cli
9
+
10
+ desc "Print current mapping to stdout"
11
+ opt :format, "yaml|json", :default => 'yaml'
12
+ parent "collab", "Application collaborators"
13
+ def export(cmd, opts, argv)
14
+ puts heroku_state.send("to_#{opts[:format]}".to_sym)
15
+ end
16
+
17
+ OP_COLORS = {
18
+ '+' => "\e[0;1;32m",
19
+ '-' => "\e[0;1;31m",
20
+ '~' => "\e[0;32m",
21
+ }
22
+ desc "Read mapping from stdin and diff to heroku state"
23
+ parent "collab"
24
+ def diff(cmd, opts, argv)
25
+ parsed_state = parse_as_json_or_yaml(STDIN.read)
26
+ plan(HashDiff.diff(heroku_state, parsed_state)).each do |s|
27
+ color = OP_COLORS[s[:op]]
28
+ msg = ''
29
+ if s[:method].nil?
30
+ color = "\e[0;41;33;1m"
31
+ msg = "Can not resolve this."
32
+ end
33
+ printf "%s%6s %-30s %-15s %-30s %s\e[0m\n", color, s[:op_name], s[:app_name], s[:role], s[:value], msg
34
+ end
35
+ end
36
+
37
+ OP_MAP = {
38
+ '+' => 'add',
39
+ '-' => 'remove',
40
+ '~' => 'change',
41
+ }
42
+ def plan(diff)
43
+ plan = []
44
+ diff.each do |op, target, value|
45
+ app_name, role = target.split('.')
46
+ role = role.split('[')[0]
47
+ op_name = OP_MAP[op]
48
+ method_name = "op_#{op_name}_#{role}".to_sym
49
+ plan << {
50
+ app_name: app_name,
51
+ op: op,
52
+ op_name: op_name,
53
+ method: self.respond_to?(method_name) ? method_name : nil,
54
+ value: value,
55
+ role: role,
56
+ }
57
+ end
58
+ plan
59
+ end
60
+
61
+ def op_add_collaborators(name)
62
+ raise NotImplementedError
63
+ end
64
+
65
+ def op_remove_collaborators(name)
66
+ raise NotImplementedError
67
+ end
68
+
69
+ def parse_as_json_or_yaml(input)
70
+ begin
71
+ parsed = JSON.load(input)
72
+ rescue => jex
73
+ begin
74
+ parsed = YAML.load(input)
75
+ if parsed.is_a? String
76
+ raise ArgumentError, "Input parsed as YAML yields a String"
77
+ end
78
+ rescue => yex
79
+ STDERR.puts "Error: Could neither parse stdin as YAML nor as JSON."
80
+ STDERR.puts "-- JSON Error --"
81
+ STDERR.puts jex
82
+ STDERR.puts "-- YAML Error --"
83
+ STDERR.puts yex
84
+ raise ArgumentError
85
+ end
86
+ end
87
+ parsed
88
+ end
89
+
90
+ def heroku_state
91
+ data = {}
92
+ app_names = h.app.list.map{|e| e['name']}
93
+ app_names.each_with_index do |app_name,i|
94
+ pb :msg => app_name, :total => app_names.length, :done => i+1
95
+ data[app_name] = { 'collaborators' => [] }
96
+ h.collaborator.list(app_name).map{|e|
97
+ case e['role']
98
+ when 'owner'
99
+ data[app_name]['owner'] = e['user']['email']
100
+ when nil
101
+ data[app_name]['collaborators'] << e['user']['email']
102
+ else
103
+ raise RuntimeError, "Unknown collaborator role: #{e['role']}"
104
+ end
105
+ }
106
+ end
107
+ pb :wipe
108
+ data
109
+ end
110
+
111
+ def h
112
+ @h ||= PlatformAPI.connect_oauth(Hu::Cli::API_TOKEN)
113
+ end
114
+
115
+ def pb(show_opts)
116
+ return if $quiet
117
+ @pb ||= PowerBar.new
118
+ show_opts == :wipe ? @pb.wipe : @pb.show(show_opts)
119
+ end
120
+
121
+ end
122
+ end
123
+ end
@@ -1,3 +1,3 @@
1
1
  module Hu
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - moe
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: hashdiff
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: Heroku Utility.
84
98
  email:
85
99
  - moe@busyloop.net
@@ -96,6 +110,7 @@ files:
96
110
  - bin/hu
97
111
  - hu.gemspec
98
112
  - lib/hu/cli.rb
113
+ - lib/hu/collab.rb
99
114
  - lib/hu/version.rb
100
115
  homepage: https://github.com/busyloop/hu
101
116
  licenses: