Class: Chef::Knife::Core::ObjectLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/knife/core/object_loader.rb

Defined Under Namespace

Classes: ObjectType

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, ui) ⇒ ObjectLoader

Returns a new instance of ObjectLoader.



36
37
38
39
# File 'lib/chef/knife/core/object_loader.rb', line 36

def initialize(klass, ui)
  @klass = klass
  @ui = ui
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



29
30
31
# File 'lib/chef/knife/core/object_loader.rb', line 29

def klass
  @klass
end

#uiObject (readonly)

Returns the value of attribute ui.



28
29
30
# File 'lib/chef/knife/core/object_loader.rb', line 28

def ui
  @ui
end

Instance Method Details

#file_exists_and_is_readable?(file) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/chef/knife/core/object_loader.rb', line 108

def file_exists_and_is_readable?(file)
  File.exist?(file) && File.readable?(file)
end

#find_all_object_dirs(path) ⇒ Object



80
81
82
83
84
85
# File 'lib/chef/knife/core/object_loader.rb', line 80

def find_all_object_dirs(path)
  path = File.join(Chef::Util::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

#find_all_objects(path) ⇒ Array<String>

Find all objects in the given location If the object type is File it will look for all *.json,rb files, otherwise it will lookup for folders only (useful for data_bags)

Parameters:

  • path (String)
    • base look up location

Returns:

  • (Array<String>)

    basenames of the found objects



73
74
75
76
77
78
# File 'lib/chef/knife/core/object_loader.rb', line 73

def find_all_objects(path)
  path = File.join(Chef::Util::PathHelper.escape_glob_dir(File.expand_path(path)), "*")
  path << ".{json,rb}"
  objects = Dir.glob(path)
  objects.map { |o| File.basename(o) }
end

#find_file(repo_location, *components) ⇒ Object

When someone makes this awesome, please update the above error message.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chef/knife/core/object_loader.rb', line 50

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

#load_from(repo_location, *components) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/chef/knife/core/object_loader.rb', line 41

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

#object_from_file(filename) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef/knife/core/object_loader.rb', line 87

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