Class: StackFu::Commands::GenerateCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/stackfu/commands/generate_command.rb

Constant Summary collapse

Types =
{ 
  [:checkbox, :numericbox, :combobox, :password, :radio, :textbox] => :control
}

Constants included from Rendering

Rendering::LEFT_MARGIN

Constants included from OperatingSystems

OperatingSystems::FriendlyNames, OperatingSystems::OperatingSystems

Instance Attribute Summary

Attributes inherited from Command

#errors, #options, #parameters, #subcommand

Instance Method Summary collapse

Methods inherited from Command

#command, command_for, create, inherited, #initialize, #params?, #run, #valid?

Methods included from Rendering

#done, #error, #fill_values_from_options, #menu_for, #render_target, #spinner, #table, #warning

Methods included from OperatingSystems

#convert_os, #os_name

Constructor Details

This class inherits a constructor from StackFu::Commands::Command

Instance Method Details

#default(parameters, options) ⇒ 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
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
# File 'lib/stackfu/commands/generate_command.rb', line 30

def default(parameters, options)
  what = 'script'
  begin
    item_name = parameters.shift

    unless item_name
      error "Missing script name.",
        "Try again using 'stackfu generate [script_name]'."
      return false
    end
    
    
    items = {}
    while (p = parameters.shift)
      name, type = p.split(":")
    
      case type(type)
      when :execution, :script
        (items["executions"]||=[]) << [name, template("script.sh.erb", {
          "filename" => name,
          "description" => name.titleize
        })]
      
      when :control
        (items["controls"]||=[]) << [name, type]
      
      else
        raise Exceptions::InvalidParameter, 
          "Don't know how to generate a #{what} with #{type ? "#{type} " : ""}#{name}. Use 'stackfu help generate' for more information."
      
      end
    end
  
    stack = template("stack.yml.erb", {
      "name" => item_name,
      "description" => "Enter a description for this script here"
    })
  
    create("#{item_name}", "#{what}.yml", stack)

    i = 1
    %w[controls requirements executions validations].each do |item|
      template_name = "0#{i}-#{item}.yml"
      create "#{item_name}/config", template_name, template("#{template_name}.erb", {
        item => items[item]
      })
      i += 1
    end

    items["executions"].try(:each) do |item|
      create("#{item_name}/executables", "#{item.first}.sh.erb", item.last)
    end or create("#{item_name}/executables")
  
    done "#{what.titleize} #{item_name} created successfully"
  rescue Exceptions::InvalidParameter
    error $!.message
  rescue IOError, Errno::EEXIST
    error "#{$!.message}"      
  end
end

#plugin(parameters, options) ⇒ Object



22
23
24
# File 'lib/stackfu/commands/generate_command.rb', line 22

def plugin(parameters, options)
  generate("plugin", parameters, options)
end

#stack(parameters, options) ⇒ Object



26
27
28
# File 'lib/stackfu/commands/generate_command.rb', line 26

def stack(parameters, options)
  generate("stack", parameters, options)
end

#type(t) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/stackfu/commands/generate_command.rb', line 12

def type(t)
  return :unknown unless t

  ctrl = t.to_sym
  Types.each_pair do |key, value|
    ctrl = value if key.include?(ctrl)
  end
  ctrl
end