Class: Lono::Cfn::Current

Inherits:
Object
  • Object
show all
Defined in:
lib/lono/cfn/current.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Current

Returns a new instance of Current.



5
6
7
8
9
10
# File 'lib/lono/cfn/current.rb', line 5

def initialize(options={})
  Lono::ProjectChecker.check_lono_project
  @options = options
  @file = ".lono/current"
  @path = "#{Lono.root}/#{@file}"
end

Class Method Details

.nameObject

reads name, returns nil if not set



73
74
75
# File 'lib/lono/cfn/current.rb', line 73

def self.name
  Current.new.name
end

.name!(name = :current) ⇒ Object

reads name, will exit if current name not set



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lono/cfn/current.rb', line 78

def self.name!(name=:current)
  return name if name != :current

  name = Current.name
  return name if name

  # TODO: don't think it is possible to get here...
  puts "ERROR: stack name must be specified.".colorize(:red)
  puts <<-EOL
Example:
lono cfn #{ARGV[1]} STACK
You can also set a current stack name to be remembered with:
lono cfn current --name STACK
EOL
  exit 1
end

.suffixObject

reads suffix, returns nil if not set



63
64
65
# File 'lib/lono/cfn/current.rb', line 63

def self.suffix
  Current.new.suffix
end

Instance Method Details

#dataObject



53
54
55
# File 'lib/lono/cfn/current.rb', line 53

def data
  YAML.load(IO.read(@path)) rescue {}
end

#nameObject



67
68
69
70
# File 'lib/lono/cfn/current.rb', line 67

def name
  current = data["name"]
  return current unless current&.empty?
end

#rmObject



16
17
18
19
# File 'lib/lono/cfn/current.rb', line 16

def rm
  FileUtils.rm_f(@path)
  puts "Current settings have been removed. Removed #{@file}"
end

#runObject



12
13
14
# File 'lib/lono/cfn/current.rb', line 12

def run
  @options[:rm] ? rm : set
end

#setObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lono/cfn/current.rb', line 21

def set
  if @options.empty?
    show
  else
    d = data # assign data to d to create local variable for merge to work
    d = d.merge(@options).delete_if do |k,v|
      v&.empty? || v == ['']
    end
    text = YAML.dump(d)
    FileUtils.mkdir_p(File.dirname(@path))
    IO.write(@path, text)
    puts "Current settings saved in .lono/current"
    show
  end
end

#showObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lono/cfn/current.rb', line 37

def show
  if data.empty?
    puts <<-EOL
There are no current settings.  To set a current stack run:

lono cfn current --name mystack
lono cfn current -h # for more examples
EOL
    return
  end

  data.each do |key, value|
    puts "Current #{key}: #{value}"
  end
end

#suffixObject



57
58
59
60
# File 'lib/lono/cfn/current.rb', line 57

def suffix
  current = data["suffix"]
  return current unless current&.empty?
end