Class: Chef::WorkstationConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/workstation_config_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(explicit_config_file, logger = nil) ⇒ WorkstationConfigLoader

TODO: initialize this with a logger for Chef and Knife



31
32
33
34
35
# File 'lib/chef/workstation_config_loader.rb', line 31

def initialize(explicit_config_file, logger=nil)
  @explicit_config_file = explicit_config_file
  @config_location = nil
  @logger = logger || NullLogger.new
end

Instance Attribute Details

#explicit_config_fileObject

Path to a config file requested by user, (e.g., via command line option). Can be nil



28
29
30
# File 'lib/chef/workstation_config_loader.rb', line 28

def explicit_config_file
  @explicit_config_file
end

Instance Method Details

#chef_config_dirObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chef/workstation_config_loader.rb', line 45

def chef_config_dir
  if @chef_config_dir.nil?
    @chef_config_dir = false
    full_path = working_directory.split(File::SEPARATOR)
    (full_path.length - 1).downto(0) do |i|
      candidate_directory = File.join(full_path[0..i] + [".chef" ])
      if File.exist?(candidate_directory) && File.directory?(candidate_directory)
        @chef_config_dir = candidate_directory
        break
      end
    end
  end
  @chef_config_dir
end

#config_locationObject



41
42
43
# File 'lib/chef/workstation_config_loader.rb', line 41

def config_location
  @config_location ||= (explicit_config_file || locate_local_config)
end

#envObject

(Private API, public for test purposes)



75
76
77
# File 'lib/chef/workstation_config_loader.rb', line 75

def env
  ENV
end

#loadObject



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chef/workstation_config_loader.rb', line 60

def load
  # Ignore it if there's no explicit_config_file and can't find one at a
  # default path.
  return false if config_location.nil?

  if explicit_config_file && !path_exists?(config_location)
    raise Exceptions::ConfigurationError, "Specified config file #{config_location} does not exist"
  end

  # Have to set Chef::Config.config_file b/c other config is derived from it.
  Chef::Config.config_file = config_location
  read_config(IO.read(config_location), config_location)
end

#no_config_found?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/chef/workstation_config_loader.rb', line 37

def no_config_found?
  config_location.nil?
end

#path_exists?(path) ⇒ Boolean

(Private API, public for test purposes)

Returns:

  • (Boolean)


80
81
82
# File 'lib/chef/workstation_config_loader.rb', line 80

def path_exists?(path)
  Pathname.new(path).expand_path.exist?
end