Class: Coral::Command::Shell

Inherits:
Plugin::Command show all
Defined in:
lib/coral/command/shell.rb

Instance Attribute Summary

Attributes inherited from Coral::Core

#ui

Instance Method Summary collapse

Methods inherited from Plugin::Command

#args, #args=, #command, #command=, #data, #data=, #exec, #flags, #flags=, #subcommand=, #to_s

Methods inherited from Plugin::Base

build_info, ensure_plugin_collection, #initialize, #initialized?, #meta, #method_missing, #name, #name=, #plugin_directory, #plugin_file, #plugin_instance_name, #plugin_parent, #plugin_parent=, #plugin_provider, #plugin_type, #set_meta, translate

Methods inherited from Coral::Core

#initialize, #inspect, #logger, logger, logger=, ui

Methods inherited from Coral::Config

#[], #[]=, array, #array, #clear, #defaults, #delete, ensure, #export, #filter, filter, #get, #get_array, #get_hash, #hash, hash, #import, init, #init, init_flat, #initialize, #set, #string, string, string_map, #string_map, #symbol, symbol, symbol_map, #symbol_map, test, #test

Methods included from Mixin::ConfigOptions

#clear_options, #contexts, #get_options, #set_options

Methods included from Mixin::ConfigCollection

#all_properties, #clear_properties, #delete_property, #get_property, #save_properties, #set_property

Methods included from Mixin::Lookup

#hiera, #hiera_config, #initialized?, #lookup, #lookup_array, #lookup_hash

Methods included from Mixin::ConfigOps

#parse

Constructor Details

This class inherits a constructor from Coral::Plugin::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Coral::Plugin::Base

Instance Method Details

#build(components = {}, overrides = nil, override_key = false) ⇒ Object




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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/coral/command/shell.rb', line 16

def build(components = {}, overrides = nil, override_key = false)    
  
  command            = string(components[:command])
  flags              = array( components.has_key?(:flags) ? components[:flags] : [] )
  data               = string_map(hash( components.has_key?(:data) ? components[:data] : {} ))
  args               = array( components.has_key?(:args) ? components[:args] : [] )
  subcommand         = hash( components.has_key?(:subcommand) ? components[:subcommand] : {} )
  
  override_key       = command unless override_key
  override_key       = override_key.to_sym
  
  command_string     = command.dup
  subcommand_string  = ''
  
  escape_characters  = /[\'\"]+/
  escape_replacement = '\"'
  
  dash_pattern       = /^([\-]+)/
  assignment_pattern = /\=$/
  
  # Flags
  if overrides && overrides.has_key?(:flags)
    if overrides[:flags].is_a?(Hash)
      if overrides[:flags].has_key?(override_key)
        flags = array(overrides[:flags][override_key])
      end
    else
      flags = array(overrides[:flags])
    end
  end
  flags.each do |flag|
    flag = string(flag)
    if ! flag.empty?        
      if flag.match(dash_pattern)
        dashes = $1
      else
        dashes = ( flag.size == 1 ? '-' : '--' )  
      end
      command_string << " #{dashes}#{flag}"
    end
  end
  
  # Data
  if overrides && overrides.has_key?(:data)
    if overrides[:data].has_key?(override_key)
      data = hash(overrides[:data][override_key])
    else
      override = true
      overrides[:data].each do |key, value|
        if ! value.is_a?(String)
          override = false
        end
      end
      data = hash(overrides[:data]) if override
    end
  end
  data.each do |key, value|
    key   = string(key)
    value = string(value).strip.sub(escape_characters, escape_replacement)
    
    if key.match(dash_pattern)
      dashes = $1
    else
      dashes = ( key.size == 1 ? '-' : '--' )  
    end      
    space = ( key.match(assignment_pattern) ? '' : ' ' )  
    
    command_string << " #{dashes}#{key}#{space}'#{value}'"
  end
  
  # Arguments
  if overrides && overrides.has_key?(:args)
    if overrides[:args].is_a?(Hash)
      if overrides[:args].has_key?(override_key)
        args = array(overrides[:args][override_key])
      end
    else
      args = array(overrides[:args])
    end
  end
  args.each do |arg|
    arg = string(arg).sub(escape_characters, escape_replacement)
    command_string << " '#{arg}'"
  end
  
  # Subcommand
  subcommand_overrides = ( overrides ? overrides[:subcommand] : nil )
  if subcommand && subcommand.is_a?(Hash) && ! subcommand.empty?
    subcommand_string = build(subcommand, subcommand_overrides)
  end
  
  return (command_string + ' ' + subcommand_string).strip
end

#exec!(options = {}, overrides = nil) ⇒ Object




112
113
114
115
116
117
118
119
120
# File 'lib/coral/command/shell.rb', line 112

def exec!(options = {}, overrides = nil)
  config = Config.ensure(options)
  
  config[:ui] = @ui
  success = Util::Shell.exec!(build(properties, overrides), config) do |line|
    block_given? ? yield(line) : true
  end    
  return success
end

#executable(options) ⇒ Object


Utilities



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/coral/command/shell.rb', line 125

def executable(options)
  config = Config.ensure(options)
  
  if config.get(:coral, false)
    return 'vagrant coral ' + config[:coral]
      
  elsif config.get(:vagrant, false)
    return 'vagrant ' + config[:vagrant]
      
  elsif config.get(:command, false)
    return config[:command]
  end
end

#normalizeObject


Command operations



9
10
11
12
# File 'lib/coral/command/shell.rb', line 9

def normalize
  super
  set(:command, executable(self))
end