Module: Chef::Knife::SwapBase

Included in:
Chef::Knife
Defined in:
lib/chef/knife/swap_base.rb

Overview

base module

Instance Method Summary collapse

Instance Method Details

#available_configsObject

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_dirObject

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.expand_path('~'), '.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_configsObject

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_configObject

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



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 configs currently being used



54
55
56
57
58
59
60
61
62
63
64
# 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
  if configs.empty? && File.exist?(knife_config)
    puts Chef::Knife.ui.color("Available configs do not match current knife.rb.\nDid you modify knife.rb or matching knife-<config>.rb?", :red)
  else
    puts "#{Chef::Knife.ui.color('current[', :cyan)}#{configs.join(',')}#{Chef::Knife.ui.color(']', :cyan)}"
  end
end

#use_config(config) ⇒ Object

copy config to knife.rb



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef/knife/swap_base.rb', line 67

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