Class: FitCommit::Runner

Inherits:
Object
  • Object
show all
Includes:
HasErrors
Defined in:
lib/fit-commit/runner.rb

Constant Summary collapse

EXIT_CODE_ALLOW_COMMIT =
0
EXIT_CODE_REJECT_COMMIT =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasErrors

#add_error, #add_warning, #errors, #merge_errors, #merge_warnings, #warnings

Constructor Details

#initialize(message_path, branch_name, stdout = $stdout, stdin = $stdin) ⇒ Runner

Returns a new instance of Runner.



12
13
14
15
16
17
# File 'lib/fit-commit/runner.rb', line 12

def initialize(message_path, branch_name, stdout = $stdout, stdin = $stdin)
  self.message_path = message_path
  self.branch_name = branch_name
  self.stdout = stdout
  self.stdin = stdin
end

Instance Attribute Details

#branch_nameObject

Returns the value of attribute branch_name.



11
12
13
# File 'lib/fit-commit/runner.rb', line 11

def branch_name
  @branch_name
end

#message_pathObject

Returns the value of attribute message_path.



11
12
13
# File 'lib/fit-commit/runner.rb', line 11

def message_path
  @message_path
end

#stdinObject

Returns the value of attribute stdin.



11
12
13
# File 'lib/fit-commit/runner.rb', line 11

def stdin
  @stdin
end

#stdoutObject

Returns the value of attribute stdout.



11
12
13
# File 'lib/fit-commit/runner.rb', line 11

def stdout
  @stdout
end

Instance Method Details

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fit-commit/runner.rb', line 19

def run
  return EXIT_CODE_ALLOW_COMMIT if empty_commit?
  run_validators
  return EXIT_CODE_ALLOW_COMMIT if [errors, warnings].all?(&:empty?)
  print_results

  allow_commit = errors.empty?
  unless allow_commit
    stdout.print "\nForce commit? [y/n] "
    return EXIT_CODE_REJECT_COMMIT unless stdin.gets =~ /y/i
    allow_commit = true
  end

  stdout.print "\n"
  allow_commit ? EXIT_CODE_ALLOW_COMMIT : EXIT_CODE_REJECT_COMMIT
rescue Interrupt # Ctrl-c
  EXIT_CODE_REJECT_COMMIT
end