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



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/stackfu/commands/dump_command.rb', line 6

def default(parameters, options)
  script_name = parameters[0]
  user_name = ''
  
  unless script_name
    puts "You have to tell which script you want to dump."
    return
  end
  
  script = spinner { 
    begin
      if script_name.include?('/')
        user_name, script_name = script_name.split('/')
        user = User.new(:id => user_name)
        user_name = "#{user_name}/"
        Script.new(user.get(script_name))
      else
        Script.find(script_name)
      end
    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 : "",
      "tags" => script.respond_to?(:tags) ? script.tags : ""
    }.to_yaml
    
    create_folder "#{script_name}/config"
    
    if script.respond_to?(:controls)
      controls = map script.controls, "controls" do |c|
        options = {}
        validations = {}
        validation_messages = {}
        
        if c.respond_to?(:options) and !c.options.empty?
          options = { 
            "options" => c.options
          }
        end

        if c.respond_to?(:validations) and !c.validations.empty?
          validations = { 
            "validations" => c.validations.attributes.to_hash
          }
        end
        
        if c.respond_to?(:validation_messages) and !c.validation_messages.empty?
          validation_messages = { 
            "validation_messages" => c.validation_messages.attributes.to_hash
          }
        end
        
        required = 'false'
        if c.respond_to?(:required)
          required = c.required.to_s
        end
        
        { "name"     => c.name,
          "label"    => c.label,
          "type"     => c._type,
          "required" => required }.merge(validations).merge(validation_messages).merge(options)
      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 #{user_name}#{script_name} dumped."
  else
    error "Script '#{user_name}#{script_name}' was not found"
  end
end