Class: Sfn::Command::Parameters

Inherits:
Sfn::Command show all
Includes:
Sfn::CommandModule::Base, SfnParameters::Utils
Defined in:
lib/sfn-parameters/command.rb

Overview

Parameters command

Constant Summary collapse

NEW_ITEM_DEFAULT =

Default contents for new item creation

Smash.new(
  :parameters => {},
  :compile_parameters => {},
  :apply_stacks => [],
  :stacks => {}
)

Instance Method Summary collapse

Methods included from SfnParameters::Utils

#lock_content, #unlock_content

Instance Method Details

#execute!Object

Execute parameters action request



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sfn-parameters/command.rb', line 20

def execute!
  action, item = arguments[0].to_s, arguments[1].to_s
  ui.info "Running parameters action #{ui.color(action.to_s, :bold)}"
  if(respond_to?("run_action_#{action}"))
    send("run_action_#{action}", item)
  else
    allowed_actions = public_methods.grep(/^run_action/).sort.map do |item|
      item.to_s.sub('run_action_', '')
    end
    raise ArgumentError.new "Unsupported action received `#{action}`. " \
      "Allowed: #{allowed_actions.join(', ')}"
  end
end

#new_item(item) ⇒ String

Expand path for new item if required

Parameters:

  • item (String)

Returns:

  • (String)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/sfn-parameters/command.rb', line 137

def new_item(item)
  unless(item.include?(File::SEPARATOR))
    prefixes = [
      config.get(:sfn_parameters, :directory),
      'infrastructure',
      'stacks'
    ].compact
    prefix = prefixes.find_all do |dir|
      File.directory?(dir)
    end
    if(prefix.size > 1)
      raise ArgumentError.new "Unable to auto-determine directory for item! Multiple directories found. " \
        "(detected: #{prefix.join(', ')})"
    elsif(prefix.empty?)
      raise ArgumentError.new "No existing parameter directories found. Please create required directory. " \
        "(checked: #{prefixes.join(', ')})"
    end
    File.join(prefix.first, "#{item}.json")
  end
end

#run_action_create(item) ⇒ Object

Perform create on item

Parameters:

  • item (String)

    item to lock



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
# File 'lib/sfn-parameters/command.rb', line 89

def run_action_create(item)
  unless(ENV['EDITOR'])
    raise ArgumentError.new '$EDITOR must be set for create/edit commands!'
  end
  begin
    item = validate_item(item)
  rescue ArgumentError
    new_item = true
    item = new_item(item)
  end
  FileUtils.mkdir_p(File.dirname(item))
  tmp = Bogo::EphemeralFile.new(['sfn-parameters', '.json'])
  content = new_item ? NEW_ITEM_DEFAULT : load_json(File.read(item)).to_smash
  if(content[:sfn_parameters_lock])
    ui.print ui.color(' *', :bold)
    ui.print " Unlocking #{ui.color(item, :bold)} for edit... "
    content = unlock_content(content)
    ui.puts ui.color('unlocked', :green)
  end
  lock_enabled = content.delete(:sfn_lock_enabled) || new_item
  tmp.write(format_json(content))
  tmp.flush
  system("#{ENV['EDITOR']} #{tmp.path}")
  content = load_json(File.read(tmp.path)).to_smash
  ui.print ui.color(' *', :bold)
  if(lock_enabled)
    ui.print " Locking #{ui.color(item, :bold)} for storage... "
    content = lock_content(content)
    ui.puts ui.color('locked', :blue)
  else
    ui.puts " Storing #{ui.color(item, :bold)} for storage... #{ui.color('unlocked', :yellow)}"
  end
  File.write(item, format_json(content))
  tmp.close
end

#run_action_edit(item) ⇒ Object

Perform edit on item

Parameters:

  • item (String)

    item to lock



128
129
130
131
# File 'lib/sfn-parameters/command.rb', line 128

def run_action_edit(item)
  validate_item(item)
  run_action_create(item)
end

#run_action_lock(item) ⇒ Object

Perform locking on item

Parameters:

  • item (String)

    item to lock



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sfn-parameters/command.rb', line 37

def run_action_lock(item)
  item = validate_item(item)
  ui.print " Locking #{ui.color(item, :bold)}... "
  content = load_json(File.read(item)).to_smash
  if(content[:sfn_parameters_lock])
    ui.puts ui.color('no-op', :yellow)
    ui.warn "Item is already locked! (#{item})"
  else
    thing = lock_content(content)
    val = format_json(thing)
    File.write(item, val)
    ui.puts ui.color('locked', :blue)
  end
end

#run_action_show(item) ⇒ Object

Perform show on item

Parameters:

  • item (String)

    item to lock



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sfn-parameters/command.rb', line 73

def run_action_show(item)
  item = validate_item(item)
  content = load_json(File.read(item)).to_smash
  if(content[:sfn_parameters_lock])
    ui.print ui.color(' *', :bold)
    ui.print " Unlocking #{ui.color(item, :bold)} for display... "
    content = unlock_content(content)
    content.delete(:sfn_lock_enabled)
    ui.puts ui.color('unlocked', :green)
  end
  ui.puts format_json(content)
end

#run_action_unlock(item) ⇒ Object

Perform unlocking on item

Parameters:

  • item (String)

    item to lock



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sfn-parameters/command.rb', line 55

def run_action_unlock(item)
  item = validate_item(item)
  ui.print " Unlocking #{ui.color(item, :bold)}... "
  content = load_json(File.read(item)).to_smash
  if(content[:sfn_parameters_lock])
    content = unlock_content(content)
    content.delete(:sfn_lock_enabled)
    File.write(item, format_json(content))
    ui.puts ui.color('unlocked', :green)
  else
    ui.puts ui.color('no-op', :yellow)
    ui.warn "Item is already unlocked! (#{item})"
  end
end

#validate_item(item) ⇒ String

Validate existence of requested item. Expand path if only name given

Parameters:

  • item (String)

Returns:

  • (String)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/sfn-parameters/command.rb', line 163

def validate_item(item)
  if(item.to_s.empty?)
    raise NameError.new 'Item name is required. No item name provided.'
  end
  items = [
    item,
    File.join(
      config.fetch(
        :sfn_parameters, :directory, 'stacks'
      ),
      item
    ),
    File.join(
      config.fetch(
        :sfn_parameters, :directory, 'stacks'
      ),
      "#{item}.json"
    ),
    File.join(
      config.fetch(
        :sfn_parameters, :directory, 'infrastructure'
      ),
      item
    ),
    File.join(
      config.fetch(
        :sfn_parameters, :directory, 'infrastructure'
      ),
      "#{item}.json"
    )
  ].map{|item| File.expand_path(item) }.uniq
  valid = items.find_all do |file|
    File.exist?(file)
  end
  if(valid.empty?)
    raise ArgumentError.new "Failed to locate item `#{item}`!"
  elsif(valid.size > 1)
    raise ArgumentError.new "Multiple matches detected for item `#{item}`. " \
      "(Matches: #{valid.join(', ')})"
  else
    valid.first
  end
end