Class: Utilrb::ConfigurationFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/utilrb/configsearch/configuration_finder.rb

Overview

Find configuration files within the pathes given by ROCK_CONFIG_PATH environment variable

Class Method Summary collapse

Class Method Details

.find(filename, pkg_name) ⇒ Object

Search for a file within [ $ROCK_CONFIG_PATH ]/<packagename>/ Will not perform a recursive search

Returns the path to the file on success, otherwise nil



36
37
38
# File 'lib/utilrb/configsearch/configuration_finder.rb', line 36

def self.find(filename, pkg_name)
    findWithEnv(filename, pkg_name, 'ROCK_CONFIG_PATH')
end

.findSystemConfig(filename, system_id) ⇒ Object

Search for a file using the system id (<basename>_<id>)

returns the configuration found in [ $ROCK_CONFIG_PATH ]/<basename>/<id>/, performs a fallback search in <basename> and returns nil if no config could be found



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/utilrb/configsearch/configuration_finder.rb', line 58

def self.findSystemConfig(filename, system_id)
    id_components = system_id.split('_')

    if(id_components.size != 2)
        raise "ConfigurationFinder: Invalid system identifier #{system_id} provided. " +
              "Use <basename>_<id>"
    end

    base_pkg_name = id_components[0]
    id_pkg_name = File.join(base_pkg_name, id_components[1])
    system_config = find(filename, id_pkg_name)

    if !system_config
        system_config = find(filename, base_pkg_name)
    end

    system_config
end

.findWithEnv(filename, pkg_name, environment_search_path) ⇒ Object

Find a file by searching through paths defined by an environment variable and a given package directory. Package name is appended to all pathes found in the environment

Returns the path to the file on success, otherwise nil



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/utilrb/configsearch/configuration_finder.rb', line 12

def self.findWithEnv(filename, pkg_name, environment_search_path)
	    env_var = ENV[environment_search_path]
    if env_var
        # Extract search path from environment variable
        configuration_path = Array.new
        env_var.split(':').each do | path |
            # Extract path and append package name folder
            configuration_path << File.join(path.gsub(/:$/,''), pkg_name)
        end
    end

    if configuration_path == nil
        raise "ConfigurationFinder: Environment variable #{environment_search_path} is not set!\n" 
    else 
        configuration = search(filename, configuration_path)
    end

    configuration
end

.search(filename, search_dirs) ⇒ Object

Search for a file only in the given search directories

Returns the path to the file on success, otherwise nil



43
44
45
46
47
48
49
50
51
# File 'lib/utilrb/configsearch/configuration_finder.rb', line 43

def self.search(filename, search_dirs)
    search_dirs.each do |path|
        file = File.join(path,filename)
        if File.exist?(file)
            return file
        end
    end
    return 
end