Class: Ufo::Current

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Current

Returns a new instance of Current.



6
7
8
9
10
11
# File 'lib/ufo/current.rb', line 6

def initialize(options={})
  Ufo.check_ufo_project!
  @options = options
  @file = ".ufo/current"
  @path = "#{Ufo.root}/#{@file}"
end

Class Method Details

.env_extraObject



62
63
64
# File 'lib/ufo/current.rb', line 62

def self.env_extra
  Current.new.env_extra
end

.serviceObject

reads service, returns nil if not set



81
82
83
# File 'lib/ufo/current.rb', line 81

def self.service
  Current.new.service
end

.service!(service = :current) ⇒ Object

reads service, will exit if current service not set



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ufo/current.rb', line 86

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

  service = Current.service
  return service if service

  puts "ERROR: service must be specified.".color(:red)
  puts <<-EOL
Example:
ufo #{ARGV.first} SERVICE
You can also set a current service to be remembered with:
ufo current --service SERVICE
EOL
  exit 1
  # if want to display full help menu:
  # Ufo::CLI.start(ARGV + ["-h"])
end

.servicesObject

reads services, returns [] if not set



71
72
73
# File 'lib/ufo/current.rb', line 71

def self.services
  Current.new.services
end

Instance Method Details

#dataObject



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

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

#env_extraObject



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

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

#rmObject



17
18
19
20
# File 'lib/ufo/current.rb', line 17

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

#runObject



13
14
15
# File 'lib/ufo/current.rb', line 13

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

#serviceObject



75
76
77
78
# File 'lib/ufo/current.rb', line 75

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

#servicesObject



66
67
68
# File 'lib/ufo/current.rb', line 66

def services
  return data["services"] || []
end

#setObject



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

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 |_,v|
      v&.empty? || v == ['']
    end
    text = YAML.dump(d)
    IO.write(@path, text)
    puts "Current settings saved in .ufo/current"
    show
  end
end

#showObject



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

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

ufo current --service my-service
ufo current -h # for more examples
EOL
    return
  end

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