readConfig method
Implementation
MultiEnvConfig? readConfig() {
final file = File(Constants.pubspecFile);
if (!file.existsSync()) {
throw ConfigException('${Constants.pubspecFile} not found in current directory.');
}
try {
final yamlContent = file.readAsStringSync();
final yaml = loadYaml(yamlContent) as YamlMap;
if (!yaml.containsKey(Constants.configSection)) {
throw ConfigException('Missing "${Constants.configSection}" configuration in ${Constants.pubspecFile}');
}
final config = yaml[Constants.configSection] as YamlMap;
final defaultEnv = config['default'] as String?;
if (defaultEnv == null) {
throw ConfigException('Missing "default" environment in ${Constants.configSection} configuration');
}
final envsMap = config['environments'] as YamlMap?;
if (envsMap == null) {
throw ConfigException('Missing "environments" in ${Constants.configSection} configuration');
}
final environments = <String, FirebaseEnvConfig>{};
for (final entry in envsMap.entries) {
final name = entry.key as String;
final data = entry.value as YamlMap;
try {
_validateEnvData(name, data);
environments[name] = FirebaseEnvConfig.fromYaml(name, data);
} catch (e) {
throw ConfigException('Invalid configuration for environment "$name": $e');
}
}
if (!environments.containsKey(defaultEnv)) {
throw ConfigException('Default environment "$defaultEnv" is not defined in environments list.');
}
return MultiEnvConfig(
defaultEnv: defaultEnv,
environments: environments,
);
} catch (e) {
if (e is ConfigException) rethrow;
throw ConfigException('Failed to parse ${Constants.pubspecFile}: $e');
}
}