Class: VagrantGitHooks::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-git-hooks/config.rb

Overview

Vagrant configuration for managed Git hooks.

Constant Summary collapse

KNOWN_HOOKS =
%w[
  applypatch-msg pre-applypatch post-applypatch
  pre-commit pre-merge-commit prepare-commit-msg commit-msg post-commit
  pre-rebase post-checkout post-merge pre-push pre-auto-gc
  post-rewrite post-index-change reference-transaction fsmonitor-watchman
  p4-changelist p4-prepare-changelist p4-post-changelist p4-pre-submit
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



28
29
30
31
32
33
34
35
36
# File 'lib/vagrant-git-hooks/config.rb', line 28

def initialize
  @hooks             = UNSET_VALUE
  @hooks_source      = UNSET_VALUE
  @install_on_up     = UNSET_VALUE
  @remove_on_destroy = UNSET_VALUE
  @fail_on_error     = UNSET_VALUE
  @locale            = UNSET_VALUE
  @verbose           = UNSET_VALUE
end

Instance Attribute Details

#fail_on_errorBoolean

Returns Whether generated hooks should stop on the first failing command.

Returns:

  • (Boolean)

    Whether generated hooks should stop on the first failing command.



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
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vagrant-git-hooks/config.rb', line 16

class Config < Vagrant.plugin("2", :config)
  KNOWN_HOOKS = %w[
    applypatch-msg pre-applypatch post-applypatch
    pre-commit pre-merge-commit prepare-commit-msg commit-msg post-commit
    pre-rebase post-checkout post-merge pre-push pre-auto-gc
    post-rewrite post-index-change reference-transaction fsmonitor-watchman
    p4-changelist p4-prepare-changelist p4-post-changelist p4-pre-submit
  ].freeze

  attr_accessor :hooks, :hooks_source, :install_on_up, :remove_on_destroy,
                :fail_on_error, :locale, :verbose

  def initialize
    @hooks             = UNSET_VALUE
    @hooks_source      = UNSET_VALUE
    @install_on_up     = UNSET_VALUE
    @remove_on_destroy = UNSET_VALUE
    @fail_on_error     = UNSET_VALUE
    @locale            = UNSET_VALUE
    @verbose           = UNSET_VALUE
  end

  def finalize!
    @hooks             = {}    if @hooks             == UNSET_VALUE
    @hooks_source      = nil   if @hooks_source      == UNSET_VALUE
    @install_on_up     = true  if @install_on_up     == UNSET_VALUE
    @remove_on_destroy = false if @remove_on_destroy == UNSET_VALUE
    @fail_on_error     = true  if @fail_on_error     == UNSET_VALUE
    @locale            = "en"  if @locale            == UNSET_VALUE
    @verbose           = false if @verbose           == UNSET_VALUE
  end

  def validate(_machine)
    errors = []

    validate_hooks(errors)

    errors << "hooks_source must be a String path" if @hooks_source && !@hooks_source.is_a?(String)

    errors << "install_on_up must be boolean" unless boolean?(@install_on_up)
    errors << "remove_on_destroy must be boolean"  unless boolean?(@remove_on_destroy)
    errors << "fail_on_error must be boolean"      unless boolean?(@fail_on_error)

    unless @locale.is_a?(String) && %w[en fr].include?(@locale.to_s[0, 2].downcase)
      errors << "locale must be 'en' or 'fr'"
    end

    { "vagrant-git-hooks" => errors }
  end

  private

  def validate_hooks(errors)
    unless @hooks.is_a?(Hash)
      errors << 'hooks must be a Hash of { "hook-name" => "command" | [commands] }'
      return
    end

    @hooks.each do |name, cmd|
      errors << "unknown git hook: #{name}" unless KNOWN_HOOKS.include?(name.to_s)

      unless cmd.is_a?(String) || (cmd.is_a?(Array) && cmd.all?(String))
        errors << "hook '#{name}' command must be a String or Array of Strings"
        next
      end

      blank = cmd.is_a?(String) ? cmd.strip.empty? : Array(cmd).all? { |c| c.to_s.strip.empty? }
      errors << "hook '#{name}' command is empty" if blank
    end
  end

  def boolean?(val)
    [true, false].include?(val)
  end
end

#hooksHash{String=>String,Array<String>}

Returns Hook commands keyed by hook name.

Returns:

  • (Hash{String=>String,Array<String>})

    Hook commands keyed by hook name.



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
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vagrant-git-hooks/config.rb', line 16

class Config < Vagrant.plugin("2", :config)
  KNOWN_HOOKS = %w[
    applypatch-msg pre-applypatch post-applypatch
    pre-commit pre-merge-commit prepare-commit-msg commit-msg post-commit
    pre-rebase post-checkout post-merge pre-push pre-auto-gc
    post-rewrite post-index-change reference-transaction fsmonitor-watchman
    p4-changelist p4-prepare-changelist p4-post-changelist p4-pre-submit
  ].freeze

  attr_accessor :hooks, :hooks_source, :install_on_up, :remove_on_destroy,
                :fail_on_error, :locale, :verbose

  def initialize
    @hooks             = UNSET_VALUE
    @hooks_source      = UNSET_VALUE
    @install_on_up     = UNSET_VALUE
    @remove_on_destroy = UNSET_VALUE
    @fail_on_error     = UNSET_VALUE
    @locale            = UNSET_VALUE
    @verbose           = UNSET_VALUE
  end

  def finalize!
    @hooks             = {}    if @hooks             == UNSET_VALUE
    @hooks_source      = nil   if @hooks_source      == UNSET_VALUE
    @install_on_up     = true  if @install_on_up     == UNSET_VALUE
    @remove_on_destroy = false if @remove_on_destroy == UNSET_VALUE
    @fail_on_error     = true  if @fail_on_error     == UNSET_VALUE
    @locale            = "en"  if @locale            == UNSET_VALUE
    @verbose           = false if @verbose           == UNSET_VALUE
  end

  def validate(_machine)
    errors = []

    validate_hooks(errors)

    errors << "hooks_source must be a String path" if @hooks_source && !@hooks_source.is_a?(String)

    errors << "install_on_up must be boolean" unless boolean?(@install_on_up)
    errors << "remove_on_destroy must be boolean"  unless boolean?(@remove_on_destroy)
    errors << "fail_on_error must be boolean"      unless boolean?(@fail_on_error)

    unless @locale.is_a?(String) && %w[en fr].include?(@locale.to_s[0, 2].downcase)
      errors << "locale must be 'en' or 'fr'"
    end

    { "vagrant-git-hooks" => errors }
  end

  private

  def validate_hooks(errors)
    unless @hooks.is_a?(Hash)
      errors << 'hooks must be a Hash of { "hook-name" => "command" | [commands] }'
      return
    end

    @hooks.each do |name, cmd|
      errors << "unknown git hook: #{name}" unless KNOWN_HOOKS.include?(name.to_s)

      unless cmd.is_a?(String) || (cmd.is_a?(Array) && cmd.all?(String))
        errors << "hook '#{name}' command must be a String or Array of Strings"
        next
      end

      blank = cmd.is_a?(String) ? cmd.strip.empty? : Array(cmd).all? { |c| c.to_s.strip.empty? }
      errors << "hook '#{name}' command is empty" if blank
    end
  end

  def boolean?(val)
    [true, false].include?(val)
  end
end

#hooks_sourceString?

Returns Directory containing hook scripts to adopt.

Returns:

  • (String, nil)

    Directory containing hook scripts to adopt.



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
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vagrant-git-hooks/config.rb', line 16

class Config < Vagrant.plugin("2", :config)
  KNOWN_HOOKS = %w[
    applypatch-msg pre-applypatch post-applypatch
    pre-commit pre-merge-commit prepare-commit-msg commit-msg post-commit
    pre-rebase post-checkout post-merge pre-push pre-auto-gc
    post-rewrite post-index-change reference-transaction fsmonitor-watchman
    p4-changelist p4-prepare-changelist p4-post-changelist p4-pre-submit
  ].freeze

  attr_accessor :hooks, :hooks_source, :install_on_up, :remove_on_destroy,
                :fail_on_error, :locale, :verbose

  def initialize
    @hooks             = UNSET_VALUE
    @hooks_source      = UNSET_VALUE
    @install_on_up     = UNSET_VALUE
    @remove_on_destroy = UNSET_VALUE
    @fail_on_error     = UNSET_VALUE
    @locale            = UNSET_VALUE
    @verbose           = UNSET_VALUE
  end

  def finalize!
    @hooks             = {}    if @hooks             == UNSET_VALUE
    @hooks_source      = nil   if @hooks_source      == UNSET_VALUE
    @install_on_up     = true  if @install_on_up     == UNSET_VALUE
    @remove_on_destroy = false if @remove_on_destroy == UNSET_VALUE
    @fail_on_error     = true  if @fail_on_error     == UNSET_VALUE
    @locale            = "en"  if @locale            == UNSET_VALUE
    @verbose           = false if @verbose           == UNSET_VALUE
  end

  def validate(_machine)
    errors = []

    validate_hooks(errors)

    errors << "hooks_source must be a String path" if @hooks_source && !@hooks_source.is_a?(String)

    errors << "install_on_up must be boolean" unless boolean?(@install_on_up)
    errors << "remove_on_destroy must be boolean"  unless boolean?(@remove_on_destroy)
    errors << "fail_on_error must be boolean"      unless boolean?(@fail_on_error)

    unless @locale.is_a?(String) && %w[en fr].include?(@locale.to_s[0, 2].downcase)
      errors << "locale must be 'en' or 'fr'"
    end

    { "vagrant-git-hooks" => errors }
  end

  private

  def validate_hooks(errors)
    unless @hooks.is_a?(Hash)
      errors << 'hooks must be a Hash of { "hook-name" => "command" | [commands] }'
      return
    end

    @hooks.each do |name, cmd|
      errors << "unknown git hook: #{name}" unless KNOWN_HOOKS.include?(name.to_s)

      unless cmd.is_a?(String) || (cmd.is_a?(Array) && cmd.all?(String))
        errors << "hook '#{name}' command must be a String or Array of Strings"
        next
      end

      blank = cmd.is_a?(String) ? cmd.strip.empty? : Array(cmd).all? { |c| c.to_s.strip.empty? }
      errors << "hook '#{name}' command is empty" if blank
    end
  end

  def boolean?(val)
    [true, false].include?(val)
  end
end

#install_on_upBoolean

Returns Whether hooks are installed during vagrant up.

Returns:

  • (Boolean)

    Whether hooks are installed during vagrant up.



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
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vagrant-git-hooks/config.rb', line 16

class Config < Vagrant.plugin("2", :config)
  KNOWN_HOOKS = %w[
    applypatch-msg pre-applypatch post-applypatch
    pre-commit pre-merge-commit prepare-commit-msg commit-msg post-commit
    pre-rebase post-checkout post-merge pre-push pre-auto-gc
    post-rewrite post-index-change reference-transaction fsmonitor-watchman
    p4-changelist p4-prepare-changelist p4-post-changelist p4-pre-submit
  ].freeze

  attr_accessor :hooks, :hooks_source, :install_on_up, :remove_on_destroy,
                :fail_on_error, :locale, :verbose

  def initialize
    @hooks             = UNSET_VALUE
    @hooks_source      = UNSET_VALUE
    @install_on_up     = UNSET_VALUE
    @remove_on_destroy = UNSET_VALUE
    @fail_on_error     = UNSET_VALUE
    @locale            = UNSET_VALUE
    @verbose           = UNSET_VALUE
  end

  def finalize!
    @hooks             = {}    if @hooks             == UNSET_VALUE
    @hooks_source      = nil   if @hooks_source      == UNSET_VALUE
    @install_on_up     = true  if @install_on_up     == UNSET_VALUE
    @remove_on_destroy = false if @remove_on_destroy == UNSET_VALUE
    @fail_on_error     = true  if @fail_on_error     == UNSET_VALUE
    @locale            = "en"  if @locale            == UNSET_VALUE
    @verbose           = false if @verbose           == UNSET_VALUE
  end

  def validate(_machine)
    errors = []

    validate_hooks(errors)

    errors << "hooks_source must be a String path" if @hooks_source && !@hooks_source.is_a?(String)

    errors << "install_on_up must be boolean" unless boolean?(@install_on_up)
    errors << "remove_on_destroy must be boolean"  unless boolean?(@remove_on_destroy)
    errors << "fail_on_error must be boolean"      unless boolean?(@fail_on_error)

    unless @locale.is_a?(String) && %w[en fr].include?(@locale.to_s[0, 2].downcase)
      errors << "locale must be 'en' or 'fr'"
    end

    { "vagrant-git-hooks" => errors }
  end

  private

  def validate_hooks(errors)
    unless @hooks.is_a?(Hash)
      errors << 'hooks must be a Hash of { "hook-name" => "command" | [commands] }'
      return
    end

    @hooks.each do |name, cmd|
      errors << "unknown git hook: #{name}" unless KNOWN_HOOKS.include?(name.to_s)

      unless cmd.is_a?(String) || (cmd.is_a?(Array) && cmd.all?(String))
        errors << "hook '#{name}' command must be a String or Array of Strings"
        next
      end

      blank = cmd.is_a?(String) ? cmd.strip.empty? : Array(cmd).all? { |c| c.to_s.strip.empty? }
      errors << "hook '#{name}' command is empty" if blank
    end
  end

  def boolean?(val)
    [true, false].include?(val)
  end
end

#localeObject

Returns the value of attribute locale.



25
26
27
# File 'lib/vagrant-git-hooks/config.rb', line 25

def locale
  @locale
end

#remove_on_destroyBoolean

Returns Whether managed hooks are removed during vagrant destroy.

Returns:

  • (Boolean)

    Whether managed hooks are removed during vagrant destroy.



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
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vagrant-git-hooks/config.rb', line 16

class Config < Vagrant.plugin("2", :config)
  KNOWN_HOOKS = %w[
    applypatch-msg pre-applypatch post-applypatch
    pre-commit pre-merge-commit prepare-commit-msg commit-msg post-commit
    pre-rebase post-checkout post-merge pre-push pre-auto-gc
    post-rewrite post-index-change reference-transaction fsmonitor-watchman
    p4-changelist p4-prepare-changelist p4-post-changelist p4-pre-submit
  ].freeze

  attr_accessor :hooks, :hooks_source, :install_on_up, :remove_on_destroy,
                :fail_on_error, :locale, :verbose

  def initialize
    @hooks             = UNSET_VALUE
    @hooks_source      = UNSET_VALUE
    @install_on_up     = UNSET_VALUE
    @remove_on_destroy = UNSET_VALUE
    @fail_on_error     = UNSET_VALUE
    @locale            = UNSET_VALUE
    @verbose           = UNSET_VALUE
  end

  def finalize!
    @hooks             = {}    if @hooks             == UNSET_VALUE
    @hooks_source      = nil   if @hooks_source      == UNSET_VALUE
    @install_on_up     = true  if @install_on_up     == UNSET_VALUE
    @remove_on_destroy = false if @remove_on_destroy == UNSET_VALUE
    @fail_on_error     = true  if @fail_on_error     == UNSET_VALUE
    @locale            = "en"  if @locale            == UNSET_VALUE
    @verbose           = false if @verbose           == UNSET_VALUE
  end

  def validate(_machine)
    errors = []

    validate_hooks(errors)

    errors << "hooks_source must be a String path" if @hooks_source && !@hooks_source.is_a?(String)

    errors << "install_on_up must be boolean" unless boolean?(@install_on_up)
    errors << "remove_on_destroy must be boolean"  unless boolean?(@remove_on_destroy)
    errors << "fail_on_error must be boolean"      unless boolean?(@fail_on_error)

    unless @locale.is_a?(String) && %w[en fr].include?(@locale.to_s[0, 2].downcase)
      errors << "locale must be 'en' or 'fr'"
    end

    { "vagrant-git-hooks" => errors }
  end

  private

  def validate_hooks(errors)
    unless @hooks.is_a?(Hash)
      errors << 'hooks must be a Hash of { "hook-name" => "command" | [commands] }'
      return
    end

    @hooks.each do |name, cmd|
      errors << "unknown git hook: #{name}" unless KNOWN_HOOKS.include?(name.to_s)

      unless cmd.is_a?(String) || (cmd.is_a?(Array) && cmd.all?(String))
        errors << "hook '#{name}' command must be a String or Array of Strings"
        next
      end

      blank = cmd.is_a?(String) ? cmd.strip.empty? : Array(cmd).all? { |c| c.to_s.strip.empty? }
      errors << "hook '#{name}' command is empty" if blank
    end
  end

  def boolean?(val)
    [true, false].include?(val)
  end
end

#verboseObject

Returns the value of attribute verbose.



25
26
27
# File 'lib/vagrant-git-hooks/config.rb', line 25

def verbose
  @verbose
end

Instance Method Details

#finalize!Object



38
39
40
41
42
43
44
45
46
# File 'lib/vagrant-git-hooks/config.rb', line 38

def finalize!
  @hooks             = {}    if @hooks             == UNSET_VALUE
  @hooks_source      = nil   if @hooks_source      == UNSET_VALUE
  @install_on_up     = true  if @install_on_up     == UNSET_VALUE
  @remove_on_destroy = false if @remove_on_destroy == UNSET_VALUE
  @fail_on_error     = true  if @fail_on_error     == UNSET_VALUE
  @locale            = "en"  if @locale            == UNSET_VALUE
  @verbose           = false if @verbose           == UNSET_VALUE
end

#validate(_machine) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vagrant-git-hooks/config.rb', line 48

def validate(_machine)
  errors = []

  validate_hooks(errors)

  errors << "hooks_source must be a String path" if @hooks_source && !@hooks_source.is_a?(String)

  errors << "install_on_up must be boolean" unless boolean?(@install_on_up)
  errors << "remove_on_destroy must be boolean"  unless boolean?(@remove_on_destroy)
  errors << "fail_on_error must be boolean"      unless boolean?(@fail_on_error)

  unless @locale.is_a?(String) && %w[en fr].include?(@locale.to_s[0, 2].downcase)
    errors << "locale must be 'en' or 'fr'"
  end

  { "vagrant-git-hooks" => errors }
end