Class: Ufo::Current
- Inherits:
-
Object
- Object
- Ufo::Current
- Defined in:
- lib/ufo/current.rb
Class Method Summary collapse
- .env_extra ⇒ Object
-
.service ⇒ Object
reads service, returns nil if not set.
-
.service!(service = :current) ⇒ Object
reads service, will exit if current service not set.
-
.services ⇒ Object
reads services, returns [] if not set.
Instance Method Summary collapse
- #data ⇒ Object
- #env_extra ⇒ Object
-
#initialize(options = {}) ⇒ Current
constructor
A new instance of Current.
- #rm ⇒ Object
- #run ⇒ Object
- #service ⇒ Object
- #services ⇒ Object
- #set ⇒ Object
- #show ⇒ Object
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(={}) Ufo.check_ufo_project! @options = @file = ".ufo/current" @path = "#{Ufo.root}/#{@file}" end |
Class Method Details
.env_extra ⇒ Object
62 63 64 |
# File 'lib/ufo/current.rb', line 62 def self.env_extra Current.new.env_extra end |
.service ⇒ Object
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 |
Instance Method Details
#data ⇒ Object
53 54 55 |
# File 'lib/ufo/current.rb', line 53 def data YAML.load(IO.read(@path)) rescue {} end |
#env_extra ⇒ Object
57 58 59 60 |
# File 'lib/ufo/current.rb', line 57 def env_extra current = data["env_extra"] return current unless current&.empty? end |
#rm ⇒ Object
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 |
#run ⇒ Object
13 14 15 |
# File 'lib/ufo/current.rb', line 13 def run @options[:rm] ? rm : set end |
#service ⇒ Object
75 76 77 78 |
# File 'lib/ufo/current.rb', line 75 def service current = data["service"] return current unless current&.empty? end |
#services ⇒ Object
66 67 68 |
# File 'lib/ufo/current.rb', line 66 def services return data["services"] || [] end |
#set ⇒ Object
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 |
#show ⇒ Object
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 |