Class: StackFu::Commands::DumpCommand

Inherits:
Command
  • Object
show all
Includes:
ApiHooks
Defined in:
lib/stackfu/commands/dump_command.rb

Constant Summary

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 included from ApiHooks

#initialize_api

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



5
6
7
8
9
10
11
12
13
14
15
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
# File 'lib/stackfu/commands/dump_command.rb', line 5

def default(parameters, options)
  script_name = parameters[0]
  
  unless script_name
    puts "You have to tell which script you want to dump."
    return
  end
  
  script = spinner { 
    begin
      Script.find(script_name) 
    rescue ActiveResource::ResourceNotFound 
    end
  }
  
  if script
    if directory?(script_name) 
      unless agree("There is already a folder called '#{script_name}'. Do you want to overwrite its contents?")
        puts "Aborted."
        return false
      end
    end
    
    create_folder(script_name)
    create_file "#{script_name}/script.yml", {
      "type" => "script",
      "name" => script.name,
      "description" => script.respond_to?(:description) ? script.description : ""
    }.to_yaml
    
    create_folder "#{script_name}/config"
    
    if script.respond_to?(:controls)
      controls = map script.controls, "controls" do |c|
        { "name"  => c.name,
          "label" => c.label,
          "type"  => c._type }
      end
    else
      controls = []
    end
    
    if script.respond_to?(:requirements)
      requirements = map script.requirements, "requirements" do |req|
        { "data"   => req.params.attributes["data"],
          "type"   => req._type }
      end
    else
      requirements = []
    end
    
    if script.respond_to?(:executions)
      executions = map script.executions, "executions" do |exec|
        { "description" => exec.description,
          "file"        => exec.description.downcase.gsub(" ", "_") }
      end
    else
      executions = []
    end
    
    
    if script.respond_to?(:validations)
      validations = map script.validations, "validations" do |val|
        { "data"   => val.params.attributes["data"],
          "type"   => val._type }
      end
    else
      validations = []
    end
    
    create_file "#{script_name}/config/01-controls.yml", controls
    create_file "#{script_name}/config/02-requirements.yml", requirements
    create_file "#{script_name}/config/03-executions.yml", executions
    create_file "#{script_name}/config/04-validations.yml", validations
    
    create_folder "#{script_name}/executables"
    script.executions.each do |script|
      create_file "#{script_name}/executables/#{script.description.downcase.gsub(" ", "_")}.sh.erb", script.body
    end
    
    done "Script #{script_name} dumped."
  else
    error "Script '#{script_name}' was not found"
  end
end