commit 57f3cd483ece4db8d0d2b6638b6d1122265ac470 Author: Sambo Chea Date: Wed Mar 24 13:24:40 2021 +0700 Add configuration provider and simple provider functions diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3d64647 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# Files and directories created by pub +.dart_tool/ +.packages + +# Conventional directory for build outputs +build/ + +# Directory created by dartdoc +doc/api/ diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..2f525a7 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,4 @@ +## 1.0.0 + +- Initial version, created by Stagehand +- Add configurable interface, created by @SomboChea diff --git a/README.md b/README.md new file mode 100644 index 0000000..e9aabf3 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Dart Configurable Environment +- [ ] Allow to get property from env file +- [ ] Cache property for runtime \ No newline at end of file diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..a686c1b --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,14 @@ +# Defines a default set of lint rules enforced for +# projects at Google. For details and rationale, +# see https://github.com/dart-lang/pedantic#enabled-lints. +include: package:pedantic/analysis_options.yaml + +# For lint rules and documentation, see http://dart-lang.github.io/linter/lints. +# Uncomment to specify additional rules. +# linter: +# rules: +# - camel_case_types + +analyzer: +# exclude: +# - path/to/excluded/files/** diff --git a/bin/configurable.dart b/bin/configurable.dart new file mode 100644 index 0000000..bddbea6 --- /dev/null +++ b/bin/configurable.dart @@ -0,0 +1,3 @@ +void main(List arguments) { + print('Just, say Hello to You!'); +} diff --git a/lib/configuration_provider.dart b/lib/configuration_provider.dart new file mode 100644 index 0000000..5e7121c --- /dev/null +++ b/lib/configuration_provider.dart @@ -0,0 +1,4 @@ +abstract class ConfigurationProvider { + bool containsKey(String key); + String? getOrNull(String key, {String? defaultValue}); +} \ No newline at end of file diff --git a/lib/simple_configuration_provider.dart b/lib/simple_configuration_provider.dart new file mode 100644 index 0000000..2a9104d --- /dev/null +++ b/lib/simple_configuration_provider.dart @@ -0,0 +1,22 @@ +import 'package:configurable/configuration_provider.dart'; + +class SimpleConfigurationProvider implements ConfigurationProvider { + Map configs = {}; + + @override + String? getOrNull(String key, {String? defaultValue}) { + var value = configs[key]; + + if (value == null) { + configs[key] = defaultValue; + return defaultValue; + } + + return value; + } + + @override + bool containsKey(String key) { + return configs.containsKey(key); + } +} diff --git a/lib/system_config.dart b/lib/system_config.dart new file mode 100644 index 0000000..ee79d07 --- /dev/null +++ b/lib/system_config.dart @@ -0,0 +1,26 @@ +import 'package:configurable/configuration_provider.dart'; +import 'package:configurable/simple_configuration_provider.dart'; + +class SystemConfig { + static ConfigurationProvider? provider; + + static void setProvider(ConfigurationProvider provider) { + SystemConfig.provider = provider; + } + + static ConfigurationProvider getProvider() { + if (provider == null) { + setProvider(SimpleConfigurationProvider()); + } + + return provider!; + } + + static String get(String key, {String? defaultValue}) { + return getOrNull(key, defaultValue: defaultValue)!; + } + + static String? getOrNull(String key, {String? defaultValue}) { + return getProvider().getOrNull(key); + } +} diff --git a/pubspec.lock b/pubspec.lock new file mode 100644 index 0000000..0fb5504 --- /dev/null +++ b/pubspec.lock @@ -0,0 +1,12 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: + pedantic: + dependency: "direct dev" + description: + name: pedantic + url: "https://pub.dartlang.org" + source: hosted + version: "1.11.0" +sdks: + dart: ">=2.12.0 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 0000000..99155de --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,13 @@ +name: configurable +description: A simple command-line application. +# version: 1.0.0 +# homepage: https://www.example.com + +environment: + sdk: '>=2.12.0 <3.0.0' + +#dependencies: +# path: ^1.7.0 + +dev_dependencies: + pedantic: ^1.9.0 diff --git a/test/config_test.dart b/test/config_test.dart new file mode 100644 index 0000000..e69de29