Module: Chef::Knife::SwapBase
- Included in:
- Chef::Knife
- Defined in:
- lib/chef/knife/swap_base.rb
Overview
base module
Instance Method Summary collapse
-
#available_configs ⇒ Object
get available configs.
-
#chef_dir ⇒ Object
get .chef dir but default to homedir/.chef if config cant be found.
-
#compare_configs(c1, c2) ⇒ Object
compare file contents.
-
#current_configs ⇒ Object
returns matching configuration file(s) matching the current knife.rb.
-
#knife_config ⇒ Object
get knife config if exists otherwise return ”.
-
#print_available_configs ⇒ Object
print available configs.
-
#print_current_configs ⇒ Object
print configs currently being used.
-
#use_config(config) ⇒ Object
copy config to knife.rb.
Instance Method Details
#available_configs ⇒ Object
get available configs
20 21 22 |
# File 'lib/chef/knife/swap_base.rb', line 20 def available_configs Dir.glob(File.join(chef_dir, 'knife-*.rb')).sort end |
#chef_dir ⇒ Object
get .chef dir but default to homedir/.chef if config cant be found
15 16 17 |
# File 'lib/chef/knife/swap_base.rb', line 15 def chef_dir knife_config == '' ? File.join(File.('~'), '.chef') : File.dirname(knife_config) end |
#compare_configs(c1, c2) ⇒ Object
compare file contents
33 34 35 |
# File 'lib/chef/knife/swap_base.rb', line 33 def compare_configs(c1, c2) File.read(c1) == File.read(c2) end |
#current_configs ⇒ Object
returns matching configuration file(s) matching the current knife.rb. it is possible that there are multiple knife-*.rb files that match. in this case, it will return all matches.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/chef/knife/swap_base.rb', line 40 def current_configs matches = [] unless File.exist?(knife_config) puts 'knife swap CONFIG' print_available_configs exit 1 end available_configs.each do |config| matches << config if compare_configs(knife_config, config) end matches end |
#knife_config ⇒ Object
get knife config if exists otherwise return ”
10 11 12 |
# File 'lib/chef/knife/swap_base.rb', line 10 def knife_config ::Chef::Knife.config_loader.config_location.nil? ? '' : ::Chef::Knife.config_loader.config_location end |
#print_available_configs ⇒ Object
print available configs
25 26 27 28 29 30 |
# File 'lib/chef/knife/swap_base.rb', line 25 def print_available_configs puts Chef::Knife.ui.color('available configs:', :cyan).to_s available_configs.each do |cfg| puts ' * ' + File.basename(cfg, '.rb').split('-')[1..-1].join('-') end end |
#print_current_configs ⇒ Object
print configs currently being used
54 55 56 57 58 59 60 |
# File 'lib/chef/knife/swap_base.rb', line 54 def print_current_configs configs = [] current_configs.each do |cfg| configs << File.basename(cfg, '.rb').split('-')[1..-1].join('-') end puts "#{Chef::Knife.ui.color('current[', :cyan)}#{configs.join(',')}#{Chef::Knife.ui.color(']', :cyan)}" end |
#use_config(config) ⇒ Object
copy config to knife.rb
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/chef/knife/swap_base.rb', line 63 def use_config(config) found = '' # look for matching knife-config.rb available_configs.each do |file| if config == File.basename(file, '.rb').split('-')[1..-1].join('-') found = file break end end # evaluate if found.empty? puts Chef::Knife.ui.color("'#{config}' is not a valid config.", :red, :bold).to_s print_current_configs print_available_configs else knife_path = (knife_config.empty? ? File.join(chef_dir, 'knife.rb') : knife_config) FileUtils.copy(found, knife_path) print_current_configs end end |