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



88
89
90
# File 'lib/git-pair/command.rb', line 88

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

#author_listObject



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

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

#bold(string) ⇒ Object



96
97
98
# File 'lib/git-pair/command.rb', line 96

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

#current_author_infoObject



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

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

#highlight(string) ⇒ Object



92
93
94
# File 'lib/git-pair/command.rb', line 92

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

#pattern_infoObject



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

def pattern_info
  " #{bold '  Email pattern:'} #{Config.pattern}\n"
end

#red(string) ⇒ Object



100
101
102
# File 'lib/git-pair/command.rb', line 100

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
62
63
64
65
66
67
68
69
70
71
72
73
# 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.on '--pattern PATTERN', "Set email pattern. Example: \"dev+%name+%name@%domain\"\n" +
            "                                       %name   - First name\n" +
            "                                       %last   - Last name\n" +
            "                                       %abbr   - Abbreviation\n" +
            "                                       %domain - Use domain from global config\n" do |pattern|
      Config.set_pattern(pattern)
    end
    opts.on'--remove-pattern', 'Reset the current email pattern to default.' do
      Config.remove_pattern
    end

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

    opts.separator ' '
    opts.separator highlight('Current config:')
    opts.separator author_list.split("\n")
    opts.separator pattern_info
    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 pattern_info
    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