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



34
35
36
37
# File 'lib/chef/knife/core/object_loader.rb', line 34

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

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

#uiObject (readonly)

Returns the value of attribute ui.



26
27
28
# File 'lib/chef/knife/core/object_loader.rb', line 26

def ui
  @ui
end

Instance Method Details

#file_exists_and_is_readable?(file) ⇒ Boolean



106
107
108
# File 'lib/chef/knife/core/object_loader.rb', line 106

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

#find_all_object_dirs(path) ⇒ Object



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

def find_all_object_dirs(path)
  path = File.join(path, '*')
  objects = Dir.glob(File.expand_path(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)



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

def find_all_objects(path)
  path = File.join(path, '*')
  path << '.{json,rb}'
  objects = Dir.glob(File.expand_path(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.



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

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



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

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



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

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.json_create(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