Class: VagrantPlugins::Exec::Config

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

Constant Summary collapse

DEFAULT_SETTINGS =
{
  cmd: '*',
  opts: {
    directory: '/vagrant'
  }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



14
15
16
17
# File 'lib/vagrant-exec/config.rb', line 14

def initialize
  @binstubs_path = UNSET_VALUE
  @commands = UNSET_VALUE
end

Instance Attribute Details

#binstubs_pathObject

Returns the value of attribute binstubs_path.



12
13
14
# File 'lib/vagrant-exec/config.rb', line 12

def binstubs_path
  @binstubs_path
end

Instance Method Details

#commandsObject #commands(command, options) ⇒ Object

Retrieve or add command.

Overloads:

  • #commandsObject

    Returns array of commands and all their options

  • #commands(command, options) ⇒ Object

    Configures command with options.

    Parameters:

    • command (String, Array<String>)
    • opts (Hash)


33
34
35
36
37
38
39
40
41
# File 'lib/vagrant-exec/config.rb', line 33

def commands(*args)
  @commands = [] if @commands == UNSET_VALUE

  if args.empty?
    @commands
  else
    @commands << {cmd: args[0], opts: args[1] || {}}
  end
end

#finalize!Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vagrant-exec/config.rb', line 70

def finalize!
  @binstubs_path = 'bin' if @binstubs_path == UNSET_VALUE

  if @commands == UNSET_VALUE
    @commands = [DEFAULT_SETTINGS.dup]
  else
    # add default settings and merge options for splat
    splats, commands = @commands.partition { |command| command[:cmd] == '*' }
    commands.unshift(DEFAULT_SETTINGS.dup)
    splats.each { |splat| commands.first[:opts].merge!(splat[:opts]) }
    @commands = commands
  end
end

#validate(_) ⇒ Object



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
# File 'lib/vagrant-exec/config.rb', line 43

def validate(_)
  finalize!
  errors = _detected_errors

  @commands.each do |command|
    cmd, opts = command[:cmd], command[:opts]

    if !cmd.is_a?(String) && !array_of_strings?(cmd)
      errors << "Commands should be String or Array<String>, received: #{cmd.inspect}"
    end

    if opts.has_key?(:directory) && !opts[:directory].is_a?(String)
      errors << ":directory should be String, received: #{opts[:directory].inspect}"
    end

    if opts.has_key?(:prepend) && !opts[:prepend].is_a?(String)
      errors << ":prepend should be String, received: #{opts[:prepend].inspect}"
    end

    if opts.has_key?(:env) && !opts[:env].is_a?(Hash)
      errors << ":env should be Hash, received: #{opts[:env].inspect}"
    end
  end

  {'exec' => errors}
end