Class: Compote::Config
- Inherits:
-
Object
- Object
- Compote::Config
- Defined in:
- lib/compote/config.rb
Constant Summary collapse
- @@configs =
{}
Class Method Summary collapse
Instance Method Summary collapse
- #commands ⇒ Object
- #compose_config ⇒ Object
- #compose_environment ⇒ Object
- #compose_file ⇒ Object
- #compose_version ⇒ Object
- #directory_name ⇒ Object
- #file_name ⇒ Object
- #get_path(path) ⇒ Object
- #get_service_config(name) ⇒ Object
- #has_service_config?(name) ⇒ Boolean
-
#initialize(file_name) ⇒ Config
constructor
A new instance of Config.
- #load_config(path) ⇒ Object
Constructor Details
#initialize(file_name) ⇒ Config
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/compote/config.rb', line 5 def initialize ( file_name ) @file_name = file_name @directory_name = File.dirname file_name data = YAML.load_file file_name data = Schema.normalize self, data @data = apply_extends data @compose_settings = @data.reject { | key, value | %w( compote services ).include? key } @compote_settings = @data.fetch 'compote', {} @services_configs = {} @data.fetch( 'services', {} ).each do | name, data | service_config = ServiceConfig.new self, name, data @services_configs[ name ] = service_config end end |
Class Method Details
.get_path(path, origin_config = nil) ⇒ Object
237 238 239 240 241 242 243 244 245 |
# File 'lib/compote/config.rb', line 237 def self.get_path ( path, origin_config = nil ) path = File.absolute_path path, origin_config&.directory_name path = Pathname.new( path ).cleanpath.to_path path end |
.load_config(path, origin_config = nil) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/compote/config.rb', line 211 def self.load_config ( path, origin_config = nil ) path = get_path path, origin_config return origin_config if path == origin_config&.directory_name config = @@configs[ path ] raise ConfigRecursionError path: path, origin_config: origin_config, configs: configs if config.is_a? String return config if config @@configs[ path ] = origin_config&.file_name || '' config = Config.new path @@configs[ path ] = config config rescue Errno::ENOENT => error raise ConfigOpenError.new error: error, origin_config: origin_config end |
Instance Method Details
#commands ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/compote/config.rb', line 126 def commands commands = {} @compote_settings.fetch( 'commands', {} ).each do | name, command | commands[ name ] = command end @services_configs.each do | service_name, service_config | service_config.commands.each do | command_name, command | commands[ "#{ service_name }:#{ command_name }" ] = command end end commands end |
#compose_config ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/compote/config.rb', line 150 def compose_config compose_config = {} compose_config[ 'version' ] = @compose_settings.fetch 'version' compose_config[ 'volumes' ] = @compose_settings.fetch 'volumes', {} compose_config[ 'networks' ] = @compose_settings.fetch 'networks', {} compose_config[ 'services' ] = {} @services_configs.each do | name, service_config | service_compose_config = service_config.compose_config compose_config[ 'services' ].merge! service_compose_config.fetch( 'services', {} ) compose_config[ 'volumes' ].merge! service_compose_config.fetch( 'volumes', {} ) compose_config[ 'networks' ].merge! service_compose_config.fetch( 'networks', {} ) end compose_config end |
#compose_environment ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/compote/config.rb', line 54 def compose_environment environment = {} environment_setting = @compote_settings.fetch 'environment', {} environment_setting.each do | key, value | environment[ key ] = ENV.fetch key, value end env_file_setting = @compote_settings.fetch 'env_file' do env_file_setting = [] env_file_setting.push '.env' if File.exist? get_path '.env' env_file_setting end env_file_setting.each do | env_file_path | begin env_file_path = get_path env_file_path env_file_content = File.read env_file_path env_file_variables = Dotenv::Parser.call env_file_content env_file_variables.each do | key, value | environment[ key ] = ENV.fetch key, value end rescue Errno::ENOENT => error raise EnvFileOpenError.new error: error, config: self rescue Dotenv::FormatError => error raise EnvFileFormatError.new error: error, path: env_file_path, config: self end end raise ProjectNameNotProvidedError.new unless environment[ 'COMPOSE_PROJECT_NAME' ] || ENV[ 'COMPOSE_PROJECT_NAME' ] environment end |
#compose_file ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/compote/config.rb', line 114 def compose_file file = Tempfile.new 'docker-compose.yml' file.write YAML.dump compose_config file.close file end |
#compose_version ⇒ Object
48 49 50 51 52 |
# File 'lib/compote/config.rb', line 48 def compose_version @compose_settings.fetch 'version' end |
#directory_name ⇒ Object
42 43 44 45 46 |
# File 'lib/compote/config.rb', line 42 def directory_name @directory_name end |
#file_name ⇒ Object
36 37 38 39 40 |
# File 'lib/compote/config.rb', line 36 def file_name @file_name end |
#get_path(path) ⇒ Object
202 203 204 205 206 |
# File 'lib/compote/config.rb', line 202 def get_path ( path ) Config.get_path path, self end |
#get_service_config(name) ⇒ Object
180 181 182 183 184 185 186 187 188 |
# File 'lib/compote/config.rb', line 180 def get_service_config ( name ) @services_configs.fetch name rescue KeyError => error raise ServiceNotFoundError.new service: name, config: self end |
#has_service_config?(name) ⇒ Boolean
190 191 192 193 194 |
# File 'lib/compote/config.rb', line 190 def has_service_config? ( name ) @services_configs.has_key? name end |
#load_config(path) ⇒ Object
196 197 198 199 200 |
# File 'lib/compote/config.rb', line 196 def load_config ( path ) Config.load_config path, self end |