class PTTool::Application

The command line interface to PTTool

Constants

DOC

Public Class Methods

new() click to toggle source
# File lib/pttool/application.rb, line 29
def initialize
  @exit_status = 0
end

Public Instance Methods

cmd_projects(num_members, sort) click to toggle source
# File lib/pttool/application.rb, line 33
def cmd_projects(num_members, sort)
  valid = %w(name id)
  raise Docopt::Exit, 'invalid sort option' unless valid.include?(sort)
  PTTool.client.projects.sort_by { |k| k.send(sort) }.each do |project|
    member_extra = " (#{project.memberships.size} members)" if num_members
    puts format("%8s: #{project.name}#{member_extra}", project.id)
  end
end
cmd_sync(projects, dryrun, force) click to toggle source
# File lib/pttool/application.rb, line 42
def cmd_sync(projects, dryrun, force)
  if projects.size < 2
    raise Docopt::Exit, 'must list at least two projects'
  end

  require 'set'
  all_people = Set.new
  by_project = {}

  PTTool.client.projects.each do |project|
    next unless projects.include?(project.name)
    projects.delete(project.name)
    all_people.merge(
      by_project[project] = project.memberships.map(&:person))
  end

  puts "Could not match: #{projects.join(', ')}" unless projects.empty?
  raise Error, 'too few matching projects' if by_project.size < 2

  if dryrun
    puts "\nThe following would become members on all projects:"
    all_people.sort_by(&:name).each { |person| display_person(person) }

    by_project.each do |project, people|
      next unless new = all_people - people
      puts "\nNew members for #{project.name}:"
      new.sort_by(&:name).each { |person| display_person(person) }
    end
    return
  end

  by_project.each do |project, people|
    to_add = all_people - people
    next if to_add.empty? || (!force && !Helper.prompt(
      "Do you want to add #{to_add.size} people to #{project.name}?"))
    to_add.each { |person_id| Helper.add_membership(project, person_id) }
  end
end
run() click to toggle source
# File lib/pttool/application.rb, line 81
def run
  handle_args(Docopt.docopt(DOC, version: VERSION))
rescue Docopt::Exit => exc
  exit_with_status(exc.message, exc.class.usage != '')
rescue Error => exc
  exit_with_status(exc.message)
end

Private Instance Methods

display_person(person) click to toggle source
# File lib/pttool/application.rb, line 91
def display_person(person)
  puts person.email ? "#{person.name} (#{person.email})" : person.name
end
exit_with_status(msg, condition = true) click to toggle source
# File lib/pttool/application.rb, line 95
def exit_with_status(msg, condition = true)
  puts msg
  @exit_status == 0 && condition ? 1 : @exit_status
end
handle_args(options) click to toggle source
# File lib/pttool/application.rb, line 100
def handle_args(options)
  if options['projects']
    cmd_projects(options['--num_members'], options['--sort'])
  elsif options['sync']
    cmd_sync(options['PROJECT'], options['--dryrun'], options['--force'])
  else
    commands = options.find_all { |x| x[1] == true }.map { |x| x[0] }
    puts "Unhandled command(s): #{commands}"
    @exit_status = 2
  end
  @exit_status
end