Module: VagrantPlugins::DataBags::FileUtil

Included in:
DataBagsContainer
Defined in:
lib/vagrant-databags/file_util.rb

Instance Method Summary collapse

Instance Method Details

#escape_glob_dir(*parts) ⇒ Object



7
8
9
10
# File 'lib/vagrant-databags/file_util.rb', line 7

def escape_glob_dir(*parts)
  path = ::Pathname.new(::File.join(*parts)).cleanpath.to_s
  path.gsub(/[\\\{\}\[\]\*\?]/) { |x| "\\" + x }
end

#find_all_object_dirs(path) ⇒ Object



12
13
14
15
16
17
# File 'lib/vagrant-databags/file_util.rb', line 12

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



19
20
21
22
23
# File 'lib/vagrant-databags/file_util.rb', line 19

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

#object_from_json_file(filename) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/vagrant-databags/file_util.rb', line 25

def object_from_json_file(filename)
  r = ::JSON.parse(IO.read(filename))
  unless r.key?('id')
    r['id'] = ::File.basename(filename, ".*")
  end
  r
end