configuration: Apply pedantic fixes
This commit is contained in:
parent
c0ab4b84a5
commit
0bd135c9cd
3 changed files with 39 additions and 30 deletions
|
@ -1,9 +1,11 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:angel_configuration/angel_configuration.dart';
|
import 'package:angel_configuration/angel_configuration.dart';
|
||||||
import 'package:angel_framework/angel_framework.dart';
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
import 'package:file/local.dart';
|
import 'package:file/local.dart';
|
||||||
|
|
||||||
main() async {
|
Future<void> main() async {
|
||||||
var app = new Angel();
|
var app = Angel();
|
||||||
var fs = const LocalFileSystem();
|
var fs = const LocalFileSystem();
|
||||||
await app.configure(configuration(fs));
|
await app.configure(configuration(fs));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,15 @@
|
||||||
library angel_configuration;
|
library angel_configuration;
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:angel_framework/angel_framework.dart';
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
import 'package:dotenv/dotenv.dart' as dotenv;
|
import 'package:dotenv/dotenv.dart' as dotenv;
|
||||||
import 'package:file/file.dart';
|
import 'package:file/file.dart';
|
||||||
import 'package:merge_map/merge_map.dart';
|
import 'package:merge_map/merge_map.dart';
|
||||||
import 'package:yaml/yaml.dart';
|
import 'package:yaml/yaml.dart';
|
||||||
|
|
||||||
_loadYamlFile(Map map, File yamlFile, Map<String, String> env,
|
Future<void> _loadYamlFile(Map map, File yamlFile, Map<String, String> env,
|
||||||
void warn(String msg)) async {
|
void Function(String msg) warn) async {
|
||||||
if (await yamlFile.exists()) {
|
if (await yamlFile.exists()) {
|
||||||
var config = loadYaml(await yamlFile.readAsString());
|
var config = loadYaml(await yamlFile.readAsString());
|
||||||
|
|
||||||
|
@ -17,7 +19,7 @@ _loadYamlFile(Map map, File yamlFile, Map<String, String> env,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> out = {};
|
var out = {};
|
||||||
|
|
||||||
for (String key in config.keys) {
|
for (String key in config.keys) {
|
||||||
out[key] = _applyEnv(config[key], env ?? {}, warn);
|
out[key] = _applyEnv(config[key], env ?? {}, warn);
|
||||||
|
@ -33,37 +35,40 @@ _loadYamlFile(Map map, File yamlFile, Map<String, String> env,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_applyEnv(var v, Map<String, String> env, void warn(String msg)) {
|
Object _applyEnv(
|
||||||
|
var v, Map<String, String> env, void Function(String msg) warn) {
|
||||||
if (v is String) {
|
if (v is String) {
|
||||||
if (v.startsWith(r'$') && v.length > 1) {
|
if (v.startsWith(r'$') && v.length > 1) {
|
||||||
var key = v.substring(1);
|
var key = v.substring(1);
|
||||||
if (env.containsKey(key))
|
if (env.containsKey(key)) {
|
||||||
return env[key];
|
return env[key];
|
||||||
else {
|
} else {
|
||||||
warn(
|
warn(
|
||||||
'Your configuration calls for loading the value of "$key" from the system environment, but it is not defined. Defaulting to `null`.');
|
'Your configuration calls for loading the value of "$key" from the system environment, but it is not defined. Defaulting to `null`.');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
} else
|
} else {
|
||||||
return v;
|
return v;
|
||||||
|
}
|
||||||
} else if (v is Iterable) {
|
} else if (v is Iterable) {
|
||||||
return v.map((x) => _applyEnv(x, env ?? {}, warn)).toList();
|
return v.map((x) => _applyEnv(x, env ?? {}, warn)).toList();
|
||||||
} else if (v is Map) {
|
} else if (v is Map) {
|
||||||
return v.keys
|
return v.keys
|
||||||
.fold<Map>({}, (out, k) => out..[k] = _applyEnv(v[k], env ?? {}, warn));
|
.fold<Map>({}, (out, k) => out..[k] = _applyEnv(v[k], env ?? {}, warn));
|
||||||
} else
|
} else {
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Loads [configuration], and returns a [Map].
|
/// Loads [configuration], and returns a [Map].
|
||||||
///
|
///
|
||||||
/// You can override [onWarning]; otherwise, configuration errors will throw.
|
/// You can override [onWarning]; otherwise, configuration errors will throw.
|
||||||
Future<Map> loadStandaloneConfiguration(FileSystem fileSystem,
|
Future<Map> loadStandaloneConfiguration(FileSystem fileSystem,
|
||||||
{String directoryPath: "./config",
|
{String directoryPath = './config',
|
||||||
String overrideEnvironmentName,
|
String overrideEnvironmentName,
|
||||||
String envPath,
|
String envPath,
|
||||||
void onWarning(String message)}) async {
|
void Function(String message) onWarning}) async {
|
||||||
Directory sourceDirectory = fileSystem.directory(directoryPath);
|
var sourceDirectory = fileSystem.directory(directoryPath);
|
||||||
var env = dotenv.env;
|
var env = dotenv.env;
|
||||||
var envFile = sourceDirectory.childFile(envPath ?? '.env');
|
var envFile = sourceDirectory.childFile(envPath ?? '.env');
|
||||||
|
|
||||||
|
@ -71,19 +76,19 @@ Future<Map> loadStandaloneConfiguration(FileSystem fileSystem,
|
||||||
dotenv.load(envFile.absolute.uri.toFilePath());
|
dotenv.load(envFile.absolute.uri.toFilePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
String environmentName = env['ANGEL_ENV'] ?? 'development';
|
var environmentName = env['ANGEL_ENV'] ?? 'development';
|
||||||
|
|
||||||
if (overrideEnvironmentName != null) {
|
if (overrideEnvironmentName != null) {
|
||||||
environmentName = overrideEnvironmentName;
|
environmentName = overrideEnvironmentName;
|
||||||
}
|
}
|
||||||
|
|
||||||
onWarning ??= (String message) => throw new StateError(message);
|
onWarning ??= (String message) => throw StateError(message);
|
||||||
var out = {};
|
var out = {};
|
||||||
|
|
||||||
var defaultYaml = sourceDirectory.childFile('default.yaml');
|
var defaultYaml = sourceDirectory.childFile('default.yaml');
|
||||||
await _loadYamlFile(out, defaultYaml, env, onWarning);
|
await _loadYamlFile(out, defaultYaml, env, onWarning);
|
||||||
|
|
||||||
String configFilePath = "$environmentName.yaml";
|
var configFilePath = '$environmentName.yaml';
|
||||||
var configFile = sourceDirectory.childFile(configFilePath);
|
var configFile = sourceDirectory.childFile(configFilePath);
|
||||||
|
|
||||||
await _loadYamlFile(out, configFile, env, onWarning);
|
await _loadYamlFile(out, configFile, env, onWarning);
|
||||||
|
@ -98,11 +103,11 @@ Future<Map> loadStandaloneConfiguration(FileSystem fileSystem,
|
||||||
///
|
///
|
||||||
/// You can also specify a custom [envPath] to load system configuration from.
|
/// You can also specify a custom [envPath] to load system configuration from.
|
||||||
AngelConfigurer configuration(FileSystem fileSystem,
|
AngelConfigurer configuration(FileSystem fileSystem,
|
||||||
{String directoryPath: "./config",
|
{String directoryPath = './config',
|
||||||
String overrideEnvironmentName,
|
String overrideEnvironmentName,
|
||||||
String envPath}) {
|
String envPath}) {
|
||||||
return (Angel app) async {
|
return (Angel app) async {
|
||||||
Directory sourceDirectory = fileSystem.directory(directoryPath);
|
var sourceDirectory = fileSystem.directory(directoryPath);
|
||||||
var env = dotenv.env;
|
var env = dotenv.env;
|
||||||
var envFile = sourceDirectory.childFile(envPath ?? '.env');
|
var envFile = sourceDirectory.childFile(envPath ?? '.env');
|
||||||
|
|
||||||
|
@ -115,7 +120,7 @@ AngelConfigurer configuration(FileSystem fileSystem,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String environmentName = env['ANGEL_ENV'] ?? 'development';
|
var environmentName = env['ANGEL_ENV'] ?? 'development';
|
||||||
|
|
||||||
if (overrideEnvironmentName != null) {
|
if (overrideEnvironmentName != null) {
|
||||||
environmentName = overrideEnvironmentName;
|
environmentName = overrideEnvironmentName;
|
||||||
|
@ -128,7 +133,7 @@ AngelConfigurer configuration(FileSystem fileSystem,
|
||||||
var defaultYaml = sourceDirectory.childFile('default.yaml');
|
var defaultYaml = sourceDirectory.childFile('default.yaml');
|
||||||
await _loadYamlFile(app.configuration, defaultYaml, env, warn);
|
await _loadYamlFile(app.configuration, defaultYaml, env, warn);
|
||||||
|
|
||||||
String configFilePath = "$environmentName.yaml";
|
var configFilePath = '$environmentName.yaml';
|
||||||
var configFile = sourceDirectory.childFile(configFilePath);
|
var configFile = sourceDirectory.childFile(configFilePath);
|
||||||
|
|
||||||
await _loadYamlFile(app.configuration, configFile, env, warn);
|
await _loadYamlFile(app.configuration, configFile, env, warn);
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:angel_framework/angel_framework.dart';
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
import 'package:angel_configuration/angel_configuration.dart';
|
import 'package:angel_configuration/angel_configuration.dart';
|
||||||
import 'package:file/local.dart';
|
import 'package:file/local.dart';
|
||||||
import 'package:io/ansi.dart';
|
import 'package:io/ansi.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
main() async {
|
Future<void> main() async {
|
||||||
// Note: Set ANGEL_ENV to 'development'
|
// Note: Set ANGEL_ENV to 'development'
|
||||||
var app = new Angel();
|
var app = Angel();
|
||||||
var fileSystem = const LocalFileSystem();
|
var fileSystem = const LocalFileSystem();
|
||||||
|
|
||||||
await app.configure(configuration(
|
await app.configure(configuration(
|
||||||
|
@ -24,13 +26,13 @@ main() async {
|
||||||
);
|
);
|
||||||
print('Standalone: $config');
|
print('Standalone: $config');
|
||||||
expect(config, {
|
expect(config, {
|
||||||
"angel": {"framework": "cool"},
|
'angel': {'framework': 'cool'},
|
||||||
"must_be_null": null,
|
'must_be_null': null,
|
||||||
"artist": "Timberlake",
|
'artist': 'Timberlake',
|
||||||
"merge": {"map": true, "hello": "world"},
|
'merge': {'map': true, 'hello': 'world'},
|
||||||
"set_via": "default",
|
'set_via': 'default',
|
||||||
"hello": "world",
|
'hello': 'world',
|
||||||
"foo": {"version": "bar"}
|
'foo': {'version': 'bar'}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -40,7 +42,7 @@ main() async {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('will load default.yaml if exists', () {
|
test('will load default.yaml if exists', () {
|
||||||
expect(app.configuration["set_via"], equals("default"));
|
expect(app.configuration['set_via'], equals('default'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('will load .env if exists', () {
|
test('will load .env if exists', () {
|
||||||
|
|
Loading…
Reference in a new issue