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 Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



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

def initialize
  @commands = UNSET_VALUE
end

Instance Method Details

#_parsed_commandsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



70
71
72
# File 'lib/vagrant-exec/config.rb', line 70

def _parsed_commands
  @commands
end

#commands(cmd, opts = {}) ⇒ Object

Configures commands.

Parameters:

  • cmd (String, Array<String>)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :directory (String)

    Directory to execute commands in

  • :prepend (String)

    Command to prepend with

  • :env (Hash)

    Environmental variables to export



25
26
27
28
# File 'lib/vagrant-exec/config.rb', line 25

def commands(cmd, opts = {})
  @commands = [] if @commands == UNSET_VALUE
  @commands << { cmd: cmd, opts: opts }
end

#finalize!Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vagrant-exec/config.rb', line 57

def finalize!
  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



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

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