diff options
author | elijah <elijah@riseup.net> | 2012-10-09 00:05:44 -0700 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2012-10-09 00:05:44 -0700 |
commit | 73b126976ad7843eb47a84944cf191bf05b14216 (patch) | |
tree | 918656f8d7c637e8c7a8f0c010eff55bfd98ae1b /lib/leap_cli/path.rb | |
parent | 578ac2f5dc7432317d7a022bed9d869ab89ee45c (diff) |
fixed paths
Diffstat (limited to 'lib/leap_cli/path.rb')
-rw-r--r-- | lib/leap_cli/path.rb | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/leap_cli/path.rb b/lib/leap_cli/path.rb new file mode 100644 index 0000000..5dc8fe8 --- /dev/null +++ b/lib/leap_cli/path.rb @@ -0,0 +1,79 @@ +require 'fileutils' + +module LeapCli + module Path + + def self.root + @root ||= File.expand_path("#{provider}/..") + end + + def self.platform + @platform ||= File.expand_path("#{root}/leap_platform") + end + + def self.provider + @provider ||= if @root + File.expand_path("#{root}/provider") + else + find_in_directory_tree('provider.json') + end + end + + def self.hiera + @hiera ||= "#{provider}/hiera" + end + + def self.files + @files ||= "#{provider}/files" + end + + def self.ok? + provider != '/' + end + + def self.set_root(root_path) + @root = File.expand_path(root_path) + raise "No such directory '#{@root}'" unless File.directory?(@root) + end + + def self.ensure_dir(dir) + unless File.directory?(dir) + if File.exists?(dir) + raise 'Unable to create directory "%s", file already exists.' % dir + else + FileUtils.mkdir_p(dir) + end + end + end + + def self.find_file(name, filename) + path = [Path.files, filename].join('/') + return path if File.exists?(path) + path = [Path.files, name, filename].join('/') + return path if File.exists?(path) + path = [Path.files, 'nodes', name, filename].join('/') + return path if File.exists?(path) + path = [Path.files, 'services', name, filename].join('/') + return path if File.exists?(path) + path = [Path.files, 'tags', name, filename].join('/') + return path if File.exists?(path) + + # give up + return nil + end + + private + + def self.find_in_directory_tree(filename) + search_dir = Dir.pwd + while search_dir != "/" + Dir.foreach(search_dir) do |f| + return search_dir if f == filename + end + search_dir = File.dirname(search_dir) + end + return search_dir + end + + end +end |