Class: Overcommit::CLI

Inherits:
Object
  • Object
show all
Includes:
ConsoleMethods
Defined in:
lib/overcommit/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConsoleMethods

#bold, #error, #notice, #success, #warning

Constructor Details

#initialize(arguments = []) ⇒ CLI

Returns a new instance of CLI.



8
9
10
11
# File 'lib/overcommit/cli.rb', line 8

def initialize(arguments = [])
  @arguments = arguments
  @options   = {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/overcommit/cli.rb', line 6

def options
  @options
end

Instance Method Details

#parse_argumentsObject



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
74
75
76
77
78
79
# File 'lib/overcommit/cli.rb', line 13

def parse_arguments
  @parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{opts.program_name} [options] target"

    opts.on_tail('-h', '--help', 'Show this message') do
      print_help opts.help
    end

    opts.on_tail('-v', '--version', 'Show version') do
      puts VERSION
      exit 0
    end

    opts.on('-l', '--list-templates', 'List built-in templates') do
      Overcommit.config.templates.each_pair do |name, configuration|
        bold name
        puts YAML.dump(configuration), ''
      end
      exit 0
    end

    opts.on('-a', '--all', 'Include all git hooks') do
      @options[:template] = 'all'
    end

    opts.on('-t', '--template template',
            'Specify a template of hooks') do |template|
      @options[:template] = template
    end

    opts.on('--uninstall', 'Remove overcommit from target') do
      @options[:uninstall] = true
    end

    opts.on('-e', '--exclude hook_name,...', Array,
            'Exclude hooks from installation') do |excludes|
      # Transform from:
      #
      #   pre_commit/test_history,commit_msg/change_id
      #
      # Into:
      #
      #   {
      #     'commit_msg' => ['change_id'],
      #     'pre_commit' => ['test_history']
      #   }
      @options[:excludes] = excludes.inject({}) do |memo, exclude|
        parts = exclude.split(%r{[:/.]})
        next memo unless parts.size == 2

        memo[parts.first] ||= []
        memo[parts.first] << parts.last

        memo
      end
    end
  end

  begin
    @parser.parse!(@arguments)

    # Unconsumed arguments are our targets
    @options[:targets] = @arguments
  rescue OptionParser::InvalidOption => ex
    print_help @parser.help, ex
  end
end

#runObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/overcommit/cli.rb', line 81

def run
  if @options[:targets].nil? || @options[:targets].empty?
    warning 'You must supply at least one directory'
    puts @parser.help
    exit 2
  end

  @options[:targets].each do |target|
    begin
      Installer.new(@options, target).run
    rescue NotAGitRepoError => e
      warning "Skipping #{target}: #{e}"
    end
  end

  success "#{@options[:uninstall] ? 'Removal' : 'Installation'} complete"

rescue ArgumentError => ex
  error "Installation failed: #{ex}"
  exit 3
end