class Chef::Knife::Core::ObjectLoader
def file_exists_and_is_readable?(file)
def file_exists_and_is_readable?(file) File.exist?(file) && File.readable?(file) end
def find_all_object_dirs(path)
def find_all_object_dirs(path) path = File.join(ChefConfig::PathHelper.escape_glob_dir(File.expand_path(path)), "*") objects = Dir.glob(path) objects.delete_if { |o| !File.directory?(o) } objects.map { |o| File.basename(o) } end
def find_all_objects(path)
- Api: - public
Returns:
-
(Array
- basenames of the found objects)
Parameters:
-
path
(String
) -- - base look up location
def find_all_objects(path) path = File.join(ChefConfig::PathHelper.escape_glob_dir(File.expand_path(path)), "*") path << ".{json,rb}" objects = Dir.glob(path) objects.map { |o| File.basename(o) } end
def find_file(repo_location, *components)
def find_file(repo_location, *components) if file_exists_and_is_readable?(File.expand_path( components.last )) File.expand_path( components.last ) else relative_path = File.join(Dir.pwd, repo_location, *components) if file_exists_and_is_readable?(relative_path) relative_path else nil end end end
def initialize(klass, ui)
def initialize(klass, ui) @klass = klass @ui = ui end
def load_from(repo_location, *components)
def load_from(repo_location, *components) unless object_file = find_file(repo_location, *components) ui.error "Could not find or open file '#{components.last}' in current directory or in '#{repo_location}/#{components.join("/")}'" exit 1 end object_from_file(object_file) end
def object_from_file(filename)
def object_from_file(filename) case filename when /\.(js|json)$/ r = FFI_Yajl::Parser.parse(IO.read(filename)) # Chef::DataBagItem doesn't work well with the json_create method if @klass == Chef::DataBagItem r else @klass.from_hash(r) end when /\.rb$/ r = klass.new r.from_file(filename) r else ui.fatal("File must end in .js, .json, or .rb") exit 30 end end