Module: Befog::Commands::Mixins::Configurable

Included in:
Add, Configure, List, Remove, Run, Start, Stop
Defined in:
lib/befog/commands/mixins/configurable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(target) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/befog/commands/mixins/configurable.rb', line 6

def self.included(target)

  target.module_eval do

    option :path, 
      :short => :p, :default => "~/.befog",
      :description => "Path to the configuration file"

    option :name,
      :short => :n, :default => "default",
      :description => "The name of this configuration"

  end
end

Instance Method Details

#_configurationObject



25
26
27
# File 'lib/befog/commands/mixins/configurable.rb', line 25

def _configuration
  @configuration ||= (YAML.load_file(configuration_path) rescue {})
end

#configurationObject



29
30
31
# File 'lib/befog/commands/mixins/configurable.rb', line 29

def configuration
  _configuration[configuration_name] ||= {}
end

#configuration_nameObject



33
34
35
# File 'lib/befog/commands/mixins/configurable.rb', line 33

def configuration_name
  options[:name]
end

#configuration_pathObject



21
22
23
# File 'lib/befog/commands/mixins/configurable.rb', line 21

def configuration_path 
  @configuration_path = File.expand_path(options[:path])
end

#saveObject



37
38
39
40
41
42
# File 'lib/befog/commands/mixins/configurable.rb', line 37

def save
  # Make sure we load the configuration before opening the file for writing
  configuration_for_writing = _configuration
  File.open(configuration_path,"w") { |f| YAML.dump(configuration_for_writing,f) }
  $stdout.puts "Configuration written to: #{configuration_path}"
end