Module: GitPair::Command

Extended by:
Command
Included in:
Command
Defined in:
lib/git-pair/command.rb

Instance Method Summary collapse

Instance Method Details

#abort(error_message, extra = "") ⇒ Object



72
73
74
# File 'lib/git-pair/command.rb', line 72

def abort(error_message, extra = "")
  super red(" Error: #{error_message}\n") + extra
end

#author_listObject



63
64
65
# File 'lib/git-pair/command.rb', line 63

def author_list
  "     #{bold 'Author list:'} #{Author.all.sort.map { |a| a.name }.join "\n                  "}"
end

#bold(string) ⇒ Object



80
81
82
# File 'lib/git-pair/command.rb', line 80

def bold(string)
  "#{C_BOLD}#{string}#{C_RESET}"
end

#current_author_infoObject



67
68
69
70
# File 'lib/git-pair/command.rb', line 67

def current_author_info
  "  #{bold 'Current author:'} #{Config.current_author}\n" +
  "   #{bold 'Current email:'} #{Config.current_email}\n "
end

#highlight(string) ⇒ Object



76
77
78
# File 'lib/git-pair/command.rb', line 76

def highlight(string)
  "#{C_REVERSE}#{string}#{C_RESET}"
end

#red(string) ⇒ Object



84
85
86
# File 'lib/git-pair/command.rb', line 84

def red(string)
  "#{C_RED}#{C_REVERSE}#{string}#{C_RESET}"
end

#run!(args) ⇒ 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/git-pair/command.rb', line 9

def run!(args)
  parser = OptionParser.new do |opts|
    opts.banner = highlight('General Syntax:')
    opts.separator '  git pair [reset | authors | options]'

    opts.separator ' '
    opts.separator highlight('Options:')
    opts.on '-a', '--add AUTHOR',    'Add an author. Format: "Author Name <[email protected]>"' do |author| 
      Config.add_author Author.new(author)
    end
    opts.on '-r', '--remove NAME', 'Remove an author. Use the full name.' do |name| 
      Config.remove_author name
    end
    opts.on '-d', '--reset', 'Reset current author to default (global) config' do
      Config.reset
    end

    opts.separator ' '
    opts.separator highlight('Switching authors:')
    opts.separator '  git pair aa [bb]                   Where AA and BB are any abbreviation of an'
    opts.separator ' '*37 + 'author\'s name. You can specify one or more authors.'

    opts.separator ' '
    opts.separator highlight('Current config:')
    opts.separator author_list.split("\n")
    opts.separator ' '
    opts.separator current_author_info.split("\n")
  end

  authors = parser.parse!(args.dup)

  if args.empty?
    puts parser.help
  elsif authors.empty?
    puts author_list
    puts
    puts current_author_info
  else
    Config.switch Author.find_all(authors)
    puts current_author_info
  end

rescue OptionParser::MissingArgument
  abort "missing required argument", parser.help
rescue OptionParser::InvalidOption, OptionParser::InvalidArgument => e
  abort e.message.sub(':', ''), parser.help
rescue NoMatchingAuthorsError => e
  abort e.message, "\n" + author_list
rescue MissingConfigurationError => e
  abort e.message, parser.help
rescue Author::InvalidAuthorString => e
  abort e.message, parser.help
end