Skip to content

Instantly share code, notes, and snippets.

@zzondlo
Forked from fujin/data_bag_common.rb
Created April 4, 2012 04:42
Show Gist options
  • Save zzondlo/2297843 to your computer and use it in GitHub Desktop.
Save zzondlo/2297843 to your computer and use it in GitHub Desktop.
require "chef/config"
require "chef/knife"
require "chef/log"
require "chef/mixin/convert_to_class_name"
module DataBagCommon
def self.included(base)
base.module_eval do
def read_secret
if locate_config_value(:secret)
locate_config_value(:secret)
else
Chef::EncryptedDataBagItem.load_secret(locate_config_value(:secret_file))
end
end
def use_encryption
if locate_config_value(:secret) && locate_config_value(:secret_file)
stdout.puts "please specify only one of --secret, --secret-file"
exit(1)
end
locate_config_value(:secret) || locate_config_value(:secret_file)
end
end
end
def locate_config_value(key)
key = key.to_sym
Chef::Config[:knife][key] || config[key]
end
end
%w[data_bag_create data_bag_show data_bag_edit data_bag_from_file].each do |k|
class_name = Chef::Mixin::ConvertToClassName.convert_to_class_name(k)
super_class = Chef::Knife.const_get(class_name)
eval <<-EOH
class #{super_class}
include DataBagCommon
end
EOH
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment