pttool 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46bde8c87b5aec081f9f3a0f878ed45256cd2c82
4
- data.tar.gz: b993ffd40473bd7f71886073359e10f48aa3c884
3
+ metadata.gz: 3bf5a304fdae3f4e070674b98371db20a4729107
4
+ data.tar.gz: a218731a02d3ca720b8483f2e60d2692d69d965a
5
5
  SHA512:
6
- metadata.gz: fe3f1cb985008c0daef484bd7816953083414b3158001fc64647b5c3922a3a51a053178bc7afa9db8c26ddd162523cd9160230d2e57b8807e71b051affadea27
7
- data.tar.gz: 6e6a2924e480801c73597d60629f7f796cc08993ca6b84ebe23f5fcf1c4bf2125db1237632c7eb1d41e9c1c7dd1e3d4de36a43885c98d01a735f29d91aac0ebf
6
+ metadata.gz: f0ea3cf189c58660d8ce3572ddc1e8f785fbaaeca6f92da046db9ff275ef746d7086ef6eef8569e37eb99f54f9734e0d5c0acd4dba4ec77b1a87c13cb1870c98
7
+ data.tar.gz: 096cf82fd99f7984e8f430af67c83f48337cb2f9a9c739fb9b88ec2b02d6652390d3ca980b36342c2eaad1cb352a8904c4010004c2fd750dd0e6574c2aada352
@@ -1,5 +1,6 @@
1
1
  require 'docopt'
2
2
  require_relative 'error'
3
+ require_relative 'helper'
3
4
  require_relative 'version'
4
5
 
5
6
  module PTTool
@@ -10,10 +11,12 @@ module PTTool
10
11
 
11
12
  Usage:
12
13
  pttool projects [--sort=<key>] [--members]
14
+ pttool sync PROJECT... [--force]
13
15
  pttool -h | --help
14
16
  pttool --version
15
17
 
16
18
  Options:
19
+ --force Do not prompt for confirmation [default: false].
17
20
  --members Output the number of members [default: false].
18
21
  --sort=<key> Sort output by name or id [default: name].
19
22
  -h --help Show this information.
@@ -25,28 +28,47 @@ module PTTool
25
28
  end
26
29
 
27
30
  def cmd_projects(members, sort)
28
- unless %w(name id).include?(sort)
29
- raise Docopt::Exit, 'invalid sort option'
30
- end
31
+ valid = %w(name id)
32
+ raise Docopt::Exit, 'invalid sort option' unless valid.include?(sort)
31
33
  PTTool.client.projects.sort_by { |k| k.send(sort) }.each do |project|
32
34
  member_extra = " (#{project.memberships.size} members)" if members
33
35
  puts format("%8s: #{project.name}#{member_extra}", project.id)
34
36
  end
35
37
  end
36
38
 
37
- def run
38
- opt = Docopt.docopt(DOC, version: VERSION)
39
- if opt['projects']
40
- cmd_projects(opt['--members'], opt['--sort'])
41
- else
42
- commands = opt.find_all { |x| x[1] == true }.map { |x| x[0] }
43
- puts "Unhandled command(s): #{commands}"
44
- @exit_status = 2
39
+ def cmd_sync(projects, force)
40
+ raise Docopt::Exit, 'must list at least two projects' if projects.size < 2
41
+
42
+ require 'set'
43
+ all_people_ids = Set.new
44
+ by_project = {}
45
+
46
+ PTTool.client.projects.each do |project|
47
+ next unless projects.include?(project.name)
48
+ projects.delete(project.name)
49
+
50
+ all_people_ids.merge(
51
+ # tracker_api doesn't properly compare People objects so we'll only
52
+ # use their id (for now)
53
+ by_project[project] = project.memberships.map(&:person).map(&:id))
45
54
  end
46
- @exit_status
55
+
56
+ puts "Could not match: #{projects.join(', ')}" unless projects.empty?
57
+ raise Error, 'too few matching projects' if by_project.size < 2
58
+
59
+ by_project.each do |project, people_ids|
60
+ to_add = all_people_ids - people_ids
61
+ next if to_add.empty? || (!force && !Helper.prompt(
62
+ "Do you want to add #{to_add.size} people to #{project.name}?"))
63
+ to_add.each { |person_id| Helper.add_membership(project, person_id) }
64
+ end
65
+ end
66
+
67
+ def run
68
+ handle_args(Docopt.docopt(DOC, version: VERSION))
47
69
  rescue Docopt::Exit => exc
48
70
  exit_with_status(exc.message, exc.class.usage != '')
49
- rescue => exc
71
+ rescue Error => exc
50
72
  exit_with_status(exc.message)
51
73
  end
52
74
 
@@ -56,5 +78,18 @@ module PTTool
56
78
  puts msg
57
79
  @exit_status == 0 && condition ? 1 : @exit_status
58
80
  end
81
+
82
+ def handle_args(options)
83
+ if options['projects']
84
+ cmd_projects(options['--members'], options['--sort'])
85
+ elsif options['sync']
86
+ cmd_sync(options['PROJECT'], options['--force'])
87
+ else
88
+ commands = options.find_all { |x| x[1] == true }.map { |x| x[0] }
89
+ puts "Unhandled command(s): #{commands}"
90
+ @exit_status = 2
91
+ end
92
+ @exit_status
93
+ end
59
94
  end
60
95
  end
@@ -0,0 +1,19 @@
1
+ require_relative 'error'
2
+
3
+ module PTTool
4
+ # Module for helper functions.
5
+ module Helper
6
+ def self.prompt(msg = 'Do you want to continue?')
7
+ print("#{msg} [(y)es|(N)o|(a)bort] ")
8
+ response = STDIN.gets.strip.downcase
9
+ raise Error, 'aborted' if %w(a abort).include?(response)
10
+ %w(y yes true 1).include?(response)
11
+ end
12
+
13
+ def self.add_membership(project, person_id, role = 'member')
14
+ data = PTTool.client.post("/projects/#{project.id}/memberships",
15
+ params: { person_id: person_id, role: role })
16
+ puts data.body
17
+ end
18
+ end
19
+ end
@@ -1,4 +1,4 @@
1
1
  # PTTool
2
2
  module PTTool
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pttool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryce Boe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-10 00:00:00.000000000 Z
11
+ date: 2015-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docopt
@@ -52,6 +52,7 @@ files:
52
52
  - lib/pttool/application.rb
53
53
  - lib/pttool/client.rb
54
54
  - lib/pttool/error.rb
55
+ - lib/pttool/helper.rb
55
56
  - lib/pttool/pttool_module.rb
56
57
  - lib/pttool/version.rb
57
58
  homepage: https://github.com/bboe/pttool