From e0020a6544dcb29540f886ce356fd228ff676523 Mon Sep 17 00:00:00 2001 From: Nialixus Date: Mon, 16 Oct 2023 18:31:44 +0700 Subject: [PATCH] =?UTF-8?q?##=202.0.1=20*=20Update=20pubspec=20*=20Remove?= =?UTF-8?q?=20asset=20loader=20*=20Remove=20=20flag=20in=20=1B[31mInvalid?= =?UTF-8?q?=20argument(s):=20Option=20input=20is=20mandatory.=1B[0m?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -i, --input (mandatory) Input path of the .env file -o, --output Output path for the encrypted .env file -s, --key Secret key for encryption & decryption --model Generate dart model to your desired file path --null-safety Make the model null safety --[no-]obfuscate Obfuscating generated values of model (defaults to on) --[no-]pubspec Inserting asset path to pubspec.yaml (defaults to on) --[no-]gitignore Inserting .env input & output file into .gitignore (defaults to on) -h, --help Print this usage information dart run env_reader --input=".env" --password="MyStrongPassword" --model="lib/src/env_model.dart" --null-safety cli --- CHANGELOG.md | 7 +- README.md | 22 ++--- bin/env_reader.dart | 7 +- bin/src/json.dart | 3 +- example/lib/main.dart | 3 +- lib/env_reader.dart | 109 +++++++++++++++++++++-- lib/env_reader_core.dart | 118 ------------------------- lib/src/{dart => }/env_encryption.dart | 2 +- lib/src/{dart => }/env_loader.dart | 2 +- lib/src/{dart => }/env_parser.dart | 2 +- lib/src/flutter/env_loader.dart | 27 ------ pubspec.yaml | 19 ++-- 12 files changed, 138 insertions(+), 183 deletions(-) delete mode 100644 lib/env_reader_core.dart rename lib/src/{dart => }/env_encryption.dart (97%) rename lib/src/{dart => }/env_loader.dart (98%) rename lib/src/{dart => }/env_parser.dart (98%) delete mode 100644 lib/src/flutter/env_loader.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f8df49..0a086a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,4 +45,9 @@ ## 2.0.0 * Split between reader for dart as `env_reader_core` and `env_reader` for flutter * Add `sdk` flag option in `env_reader` cli -* Change `encryptor` to `cryptography` \ No newline at end of file +* Change `encryptor` to `cryptography` + +## 2.0.1 +* Update pubspec +* Remove asset loader +* Remove `sdk` flag in `env_reader` cli \ No newline at end of file diff --git a/README.md b/README.md index ac12567..038eb80 100644 --- a/README.md +++ b/README.md @@ -38,46 +38,47 @@ DEBUG=true PORT=8080 DATABASE_URL=postgresql://user:password@localhost:5432/mydb ``` +--- ### 2. Run the command (Optional) Now, if you want to generate encrypted env file, run this command in your terminal: ```bash -dart run env_reader --input=".env" --output="assets/env/" --key="MyOptionalSecretKey" +env_reader --input=".env" --output="assets/env/" --key="MyOptionalSecretKey" ``` > [!NOTE] > **`output:`** .env successfully encrypted into assets/env/.env 🚀 also if you want to generate dart model from this env file, use tihs: ```bash -dart run env_reader --input-".env" --model="lib/src/env_model.dart" --null-safety +env_reader --input-".env" --model="lib/src/env_model.dart" --null-safety ``` > [!NOTE] > **`output:`** .env successfully generated into lib/src/env_model.dart 🎉 +--- ### 3. Loading your .env Load the env_reader instance: ```dart import 'package:env_reader/env_reader.dart'; -// If you want to use env_reader for dart projects only, call this instead ↓↓ -import 'package:env_reader/env_reader_core.dart'; +import 'package:flutter/services.dart'; await Env.load( - EnvAssetLoader('assets/env/.env'), + EnvStringLoader(await rootBundle.loadString('assets/env/.env')), "MyOptionalSecretKey"); // Or you can load by creating your own `EnvReader` instance. EnvReader production = EnvReader(); await production.load( - EnvAssetLoader(asset), + EnvStringLoader(await rootBundle.loadString('assets/env/.env')), "MyOptionalSecretKey"); ``` +--- ### 4. Access your configuration To get and read the value of your env: ```dart -import 'package:env_reader/env_reader.dart'; // for flutter project -import 'package:env_reader/env_reader_core.dart'; // for dart project +import 'package:env_reader/env_reader.dart'; import 'package:my_package/src/env_model.dart'; String api = Env.read("API_KEY") ?? "Got'cha 😎"; @@ -100,6 +101,7 @@ Text( EnvModel.debug ? "ðŸĪŦ pssst, this is my api key y'all \n\n ${EnvModel.apiKey}" : "Nothing to see here ðŸĪŠ", ); ``` +--- ## Env Reader Command 🚀 Available commands: @@ -113,8 +115,6 @@ Available commands: | --null-safety | Make the model null safety | | --[no-]obfuscate | Obfuscating generated values of model | | | (defaults to on) | -| --sdk | Choose between generating model for flutter or dart project | -| | [dart, flutter (default)] | | --[no-]pubspec | Insert asset path to pubspec.yaml | | | (defaults to on) | | --[no-]gitignore | Insert .env input & output file into .gitignore | @@ -123,7 +123,7 @@ Available commands: Example usage: ```bash -dart run env_reader -i ".env" -o "assets/env/" -s "MyOptionalSecretKey" --model="lib/src/env_model.dart" --null-safety --sdk flutter +env_reader -i ".env" -o "assets/env/" -s "MyOptionalSecretKey" --model="lib/src/env_model.dart" --null-safety ``` diff --git a/bin/env_reader.dart b/bin/env_reader.dart index 2c53a2b..a854453 100644 --- a/bin/env_reader.dart +++ b/bin/env_reader.dart @@ -1,8 +1,7 @@ // ignore_for_file: avoid_print import 'dart:io'; - import 'package:args/args.dart'; -import 'package:env_reader/env_reader_core.dart' show EnvEncryption; +import 'package:env_reader/env_reader.dart' show EnvEncryption; part 'src/file.dart'; part 'src/pubspec.dart'; @@ -23,10 +22,6 @@ void main(List arguments) async { defaultsTo: false, negatable: false, help: 'Make the model null safety') ..addFlag('obfuscate', defaultsTo: true, help: 'Obfuscating generated values of model') - ..addOption('sdk', - help: 'Choose between generating model for flutter or dart project', - allowed: ['dart', 'flutter'], - defaultsTo: 'flutter') ..addFlag('pubspec', defaultsTo: true, help: 'Inserting asset path to pubspec.yaml') ..addFlag('gitignore', diff --git a/bin/src/json.dart b/bin/src/json.dart index d114ca5..4128722 100644 --- a/bin/src/json.dart +++ b/bin/src/json.dart @@ -4,7 +4,6 @@ part of '../env_reader.dart'; /// A function to generate dart model out of .env file void insertJson({required ArgResults from}) { String? model = from["model"]?.toString(); - bool isFlutter = from["sdk"] == 'flutter'; if (model != null) { // Fetching arguments String path = model.replaceAll(RegExp(r'/[^/]+$'), "/"); @@ -55,7 +54,7 @@ void insertJson({required ArgResults from}) { // Env Reader Auto-Generated Model File // Created at ${DateTime.now()} // 🍔 [Buy me a coffee](https://www.buymeacoffee.com/nialixus) 🚀 -${obfuscate ? "import 'package:env_reader/env_reader${isFlutter ? '' : '_core'}.dart';\n" : ''} +${obfuscate ? "import 'package:env_reader/env_reader.dart';\n" : ''} ${obfuscate ? "/// This class represents environment variables parsed from the .env file.\n/// Each static variable corresponds to an environment variable,${nullSafety ? "\n/// with default values provided for safety\n/// `false` for [bool], `0` for [int], `0.0` for [double] and `VARIABLE_NAME` for [String]." : ""}" : "/// Class wrapper for duplicated values copied directly from env file"} class $name { $cast diff --git a/example/lib/main.dart b/example/lib/main.dart index 948a20f..9f3dcb8 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,11 +1,12 @@ import 'package:env_reader/env_reader.dart'; import 'package:example/src/env_model.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; Future main(List arguments) async { WidgetsFlutterBinding.ensureInitialized(); await Env.load( - const EnvAssetLoader('assets/env/.env'), + EnvStringLoader(await rootBundle.loadString('assets/env/.env')), "MyOptionalSecretKey", ); runApp( diff --git a/lib/env_reader.dart b/lib/env_reader.dart index c857ebf..28264cd 100644 --- a/lib/env_reader.dart +++ b/lib/env_reader.dart @@ -1,15 +1,15 @@ -/// A simple utility for reading and parsing environment variables from a .env file. +/// A simple utility for reading and parsing environment variables from a .env file /// /// This library provides a way to load environment variables from a .env file /// and parse them into a Map, allowing you to easily access environment values -/// in your Flutter application. +/// in your Dart application. /// /// Example: /// ```dart /// // Loading env data /// await Env.load( -/// EnvAssetLoader('assets/env/.env'), -/// 'MyOptionalSecretKey'); +/// EnvFileLoader(File('.env')) +/// ); /// /// // Using env data /// String? api = Env.read("API_KEY"); @@ -19,7 +19,100 @@ library env_reader; import 'dart:async'; -import 'package:flutter/services.dart'; -import 'env_reader_core.dart'; -export 'env_reader_core.dart' hide EnvParser, EnvEncryption; -part 'src/flutter/env_loader.dart'; +import 'dart:convert'; +import 'dart:developer'; +import 'dart:io'; +import 'dart:typed_data'; +import 'package:cryptography/cryptography.dart'; +import 'package:http/http.dart'; + +export 'env_reader.dart' hide EnvParser, EnvEncryption; + +part 'package:env_reader/src/env_loader.dart'; +part 'package:env_reader/src/env_parser.dart'; +part 'package:env_reader/src/env_encryption.dart'; + +/// A utility class for reading environment variables from .env source into your dart project. +class Env { + /// Default instance of [EnvReader] used to read environment variables. + static EnvReader instance = EnvReader(); + + /// Loads environment variables from .env source. + /// + /// The [source] parameter specifies where's the .env took place. + /// + /// ```dart + /// await Env.load( + /// EnvFileLoader(File('.env')), + /// 'MyOptionalSecretKey' + /// ); + /// ``` + static Future load(EnvLoader source, [String? key]) => + instance.load(source, key); + + /// Reads an environment variable value of type [T] from the loaded environment. + /// + /// Returns the value associated with the specified [key], or `null` if the key + /// is not found or there was an error while reading the environment. + /// + /// ```dart + /// // example + /// String? api = Env.read("API_KEY"); + /// int port = Env.read("PORT") ?? 8080; + /// ``` + static T? read(String key) => instance.read(key); +} + +/// An instance to load and parse environment variables from .env into dart object. +class EnvReader { + /// Loaded value of .env + /// + /// ```dart + /// String? rawValue = EnvReader().value; + /// ``` + String? value; + + /// Loads environment variables from .env source. + /// + /// The [source] parameter specifies where's the .env file took place. + /// + /// ```dart + /// await Env.load( + /// EnvFileLoader(File('.env')), + /// 'MyOptionalSecretKey' + /// ); + /// ``` + Future load(EnvLoader source, [String? key]) async { + try { + value = await source.data(key); + } catch (e) { + log("\n\n\u001b[1m[ENV_READER]\u001b[31m ðŸ’Ĩ Unable to load data\u001b[0m ðŸ’Ĩ\n$e\n\n"); + } + } + + /// Parse environment variables into a json structured map. + /// + /// ```dart + /// Map json = EnvReader().toJson; + /// ``` + Map get toJson => EnvParser(input: value ?? '').output; + + /// Reads an environment variable value of type [T] from the loaded environment. + /// + /// Returns the value associated with the specified [key], or `null` if the key + /// is not found or there was an error while reading the environment. + /// + /// ```dart + /// // example + /// final env = EnvReader(); + /// String? api = env.read("API_KEY"); + /// int port = env.read("PORT") ?? 8080; + /// ``` + T? read(String key) { + try { + return toJson[key]; + } catch (e) { + return null; + } + } +} diff --git a/lib/env_reader_core.dart b/lib/env_reader_core.dart deleted file mode 100644 index d6a3661..0000000 --- a/lib/env_reader_core.dart +++ /dev/null @@ -1,118 +0,0 @@ -/// A simple utility for reading and parsing environment variables from a .env file -/// -/// This library provides a way to load environment variables from a .env file -/// and parse them into a Map, allowing you to easily access environment values -/// in your Dart application. -/// -/// Example: -/// ```dart -/// // Loading env data -/// await Env.load( -/// EnvFileLoader(File('.env')) -/// ); -/// -/// // Using env data -/// String? api = Env.read("API_KEY"); -/// int port = Env.read("PORT") ?? 8080; -/// bool isDebug = Env.read("DEBUG") ?? false; -/// ``` -library env_reader_core; - -import 'dart:async'; -import 'dart:convert'; -import 'dart:developer'; -import 'dart:io'; -import 'dart:typed_data'; -import 'package:cryptography/cryptography.dart'; -import 'package:http/http.dart'; - -export 'env_reader_core.dart' hide EnvParser, EnvEncryption; - -part 'src/dart/env_loader.dart'; -part 'src/dart/env_parser.dart'; -part 'src/dart/env_encryption.dart'; - -/// A utility class for reading environment variables from .env source into your dart project. -class Env { - /// Default instance of [EnvReader] used to read environment variables. - static EnvReader instance = EnvReader(); - - /// Loads environment variables from .env source. - /// - /// The [source] parameter specifies where's the .env took place. - /// - /// ```dart - /// await Env.load( - /// EnvFileLoader(File('.env')), - /// 'MyOptionalSecretKey' - /// ); - /// ``` - static Future load(EnvLoader source, [String? key]) => - instance.load(source, key); - - /// Reads an environment variable value of type [T] from the loaded environment. - /// - /// Returns the value associated with the specified [key], or `null` if the key - /// is not found or there was an error while reading the environment. - /// - /// ```dart - /// // example - /// String? api = Env.read("API_KEY"); - /// int port = Env.read("PORT") ?? 8080; - /// ``` - static T? read(String key) => instance.read(key); -} - -/// An instance to load and parse environment variables from .env into dart object. -class EnvReader { - /// Loaded value of .env - /// - /// ```dart - /// String? rawValue = EnvReader().value; - /// ``` - String? value; - - /// Loads environment variables from .env source. - /// - /// The [source] parameter specifies where's the .env file took place. - /// - /// ```dart - /// await Env.load( - /// EnvFileLoader(File('.env')), - /// 'MyOptionalSecretKey' - /// ); - /// ``` - Future load(EnvLoader source, [String? key]) async { - try { - value = await source.data(key); - } catch (e) { - log("\n\n\u001b[1m[ENV_READER]\u001b[31m ðŸ’Ĩ Unable to load data\u001b[0m ðŸ’Ĩ\n$e\n\n"); - } - } - - /// Parse environment variables into a json structured map. - /// - /// ```dart - /// Map json = EnvReader().toJson; - /// ``` - Map get toJson => EnvParser(input: value ?? '').output; - - /// Reads an environment variable value of type [T] from the loaded environment. - /// - /// Returns the value associated with the specified [key], or `null` if the key - /// is not found or there was an error while reading the environment. - /// - /// ```dart - /// // example - /// final env = EnvReader(); - /// String? api = env.read("API_KEY"); - /// int port = env.read("PORT") ?? 8080; - /// ``` - T? read(String key) { - try { - return toJson[key]; - } catch (e) { - return null; - } - } -} diff --git a/lib/src/dart/env_encryption.dart b/lib/src/env_encryption.dart similarity index 97% rename from lib/src/dart/env_encryption.dart rename to lib/src/env_encryption.dart index 73d0908..4fa3d31 100644 --- a/lib/src/dart/env_encryption.dart +++ b/lib/src/env_encryption.dart @@ -1,4 +1,4 @@ -part of '../../env_reader_core.dart'; +part of 'package:env_reader/env_reader.dart'; /// A utility class for encrypting and decrypting data using AES-GCM encryption. /// diff --git a/lib/src/dart/env_loader.dart b/lib/src/env_loader.dart similarity index 98% rename from lib/src/dart/env_loader.dart rename to lib/src/env_loader.dart index 599eecc..340155d 100644 --- a/lib/src/dart/env_loader.dart +++ b/lib/src/env_loader.dart @@ -1,4 +1,4 @@ -part of '../../env_reader_core.dart'; +part of 'package:env_reader/env_reader.dart'; /// Used to refering where's the .env file came from. abstract class EnvLoader { diff --git a/lib/src/dart/env_parser.dart b/lib/src/env_parser.dart similarity index 98% rename from lib/src/dart/env_parser.dart rename to lib/src/env_parser.dart index 8c1ed6c..208f250 100644 --- a/lib/src/dart/env_parser.dart +++ b/lib/src/env_parser.dart @@ -1,4 +1,4 @@ -part of '../../env_reader_core.dart'; +part of 'package:env_reader/env_reader.dart'; /// A utility class for parsing environment variables from a given input string. /// diff --git a/lib/src/flutter/env_loader.dart b/lib/src/flutter/env_loader.dart deleted file mode 100644 index 12b1205..0000000 --- a/lib/src/flutter/env_loader.dart +++ /dev/null @@ -1,27 +0,0 @@ -part of '../../env_reader.dart'; - -/// A class to help user load their .env data from asset. -/// -/// ```dart -/// await Env.load( -/// EnvAssetLoader('assets/env/.env'), -/// 'MyOptionalSecretKey' -/// ); -/// ``` -class EnvAssetLoader extends EnvLoader { - /// Loading .env data from asset. - /// - /// ```dart - /// await Env.load( - /// EnvAssetLoader('assets/env/.env'), - /// 'MyOptionalSecretKey' - /// ); - /// ``` - const EnvAssetLoader(super.source); - - @override - Future data([String? key]) async { - String data = await rootBundle.loadString(source); - return key != null ? await EnvEncryption(key).decrypt(data) : data; - } -} diff --git a/pubspec.yaml b/pubspec.yaml index b442b1c..ce917bc 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,8 +1,12 @@ name: env_reader description: Read, encrypt, or generate environment variables from a .env file into an obfuscated Dart model. -version: 2.0.0 -homepage: https://github.com/Nialixus/env_reader.git +version: 2.0.1 +homepage: https://github.com/Nialixus/env_reader +repository: https://github.com/Nialixus/env_reader.git +issue_tracker: https://github.com/Nialixus/env_reader/issues +executables: + env_reader: env_reader funding: - https://www.buymeacoffee.com/nialixus screenshots: @@ -11,20 +15,23 @@ screenshots: topics: - utility - env +platforms: + android: + ios: + linux: + macos: + web: + windows: environment: sdk: '>=3.1.0 <4.0.0' - flutter: '>=1.17.0' dependencies: args: ^2.4.2 cryptography: ^2.7.0 - flutter: - sdk: flutter http: ^1.1.0 dev_dependencies: flutter_lints: ^2.0.0 flutter_test: sdk: flutter -flutter: null