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

Instance Method Summary collapse

Methods included from SfnParameters::Utils

#lock_content, #unlock_content

Instance Method Details

#execute!Object

Execute parameters action request



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

def execute!
  action, item = arguments[0,2]
  ui.info "Running parameters action #{ui.color(action.to_s, :bold)}"
  case action.to_sym
  when :lock
    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
  when :unlock
    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
  when :show
    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)
  when :create, :edit
    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')
    content = new_item ? Smash.new : 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}")
    tmp.rewind
    content = load_json(tmp.read).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
  else
    ArgumentError.new "Unsupported action received `#{action}`. " \
      "Allowed: lock, unlock, show, create, edit"
  end
end

#new_item(item) ⇒ String

Expand path for new item if required

Parameters:

  • item (String)

Returns:

  • (String)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/sfn-parameters/command.rb', line 98

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

#validate_item(item) ⇒ String

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

Parameters:

  • item (String)

Returns:

  • (String)


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/sfn-parameters/command.rb', line 124

def validate_item(item)
  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"
    )
  ]
  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