Skip to content

Commit

Permalink
Prerelease Env Reader v1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Nialixus committed Aug 29, 2023
1 parent 4f32b3d commit 758d475
Show file tree
Hide file tree
Showing 14 changed files with 263 additions and 75 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
* Fix description too long

## 1.3.0
* Update readme.md
* Update readme.md

## 1.4.0
* Add loadExposed (raw) .env
* Password no longer mandatory
34 changes: 19 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ Enhance the rock-solid integrity of your .env configuration by seamlessly encryp
- **Data Diversity Unleashed:** Instantly harness the power of automatic data type conversion. From numbers to booleans, your app is empowered. 🎮
- **Versatile Sourcing**: Unleash your encrypted .env from various sources—assets, files, memory, network, and strings. It's all about choice. 🔄

Step into a realm where automation, security, and flexibility converge to elevate your coding experience. Discover how effortless and impactful .env management can be! 💎🔗

## Install 🚀
Get started on the path to effortless configuration management with these quick commands:
Get started with these quick commands:

🔥 Add `env_reader` to your `pubspec.yaml` with a single line:
```bash
Expand All @@ -39,7 +37,7 @@ DATABASE_URL=postgresql://user:password@localhost:5432/mydb
```

### 2. Run the command
After laying down the foundation, it's time to secure your .env masterpiece. Execute this command in your terminal:
After laying down the foundation, it's time to secure your .env by executing this command in your terminal:
```bash
dart run env_reader --input=".env" --password="MyStrongPassword" --model="lib/src/env_model.dart" --null-safety
```
Expand All @@ -54,7 +52,7 @@ Built env_reader:env_reader.
```

### 3. Loading your encrypted .env
Load the env_reader instance, after ensuring your WidgetsFlutterBinding is initialized:
Load the env_reader instance, after ensuring your WidgetsFlutterBinding was initialized:
```dart
import 'package:env_reader/env_reader.dart';
Expand All @@ -65,11 +63,19 @@ Future<void> main(List<String> arguments) async {
source: EnvLoader.asset('assets/env/.env'),
password: "MyStrongPassword");
// Or you can load raw .env by calling this function
await Env.loadExposed(
source: EnvLoader.network(
Uri.parse('https://my.repo.dir/sub/.env')
)
)
runApp(...);
}
```
### 4. Access your configuration
And now, the moment of truth—access your configuration values with ease:
And now, the moment of truth. Aaccess your configuration values with ease:
```dart
import 'package:env_reader/env_reader.dart';
import 'package:my_package/src/env_model.dart';
Expand All @@ -80,15 +86,13 @@ bool debug = Env.read<bool>("DEBUG") ?? false;
Text(
text:
debug ? "🤫 pssst, this is my api key y'all \n\n $api" : "Nothing to see here 🤪",
),
);
// Or you can access the value directly from env generated model earlier
Text(
text:
EnvModel.debug ? "🤫 pssst, this is my api key y'all \n\n ${EnvModel.apiKey}" : "Nothing to see here 🤪",
),
);
```

Expand All @@ -97,16 +101,16 @@ Available commands:

| Flag | Description |
|--------------------------|--------------------------------------------------------------|
| -i, --input (mandatory) | Input path of the .env file |
| -p, --password (mandatory)| Password for encryption & decryption |
| -o, --output | Custom output path for the encrypted .env file |
| -i, --input (mandatory) | Input path of the .env file |
| -p, --password | Password for encryption & decryption |
| -o, --output | Custom output path for the encrypted .env file |
| | (defaults to "assets/env/") |
| --model | Generate model.dart file to your desired path |
| -h, --[no-]help | Print usage information |
| --model | Generate model.dart file to your desired path |
| -h, --[no-]help | Print usage information |
| --null-safety | Make the model null safety |
| --[no-]pubspec | Insert asset path to pubspec.yaml |
| --[no-]pubspec | Insert asset path to pubspec.yaml |
| | (defaults to on) |
| --[no-]gitignore | Insert .env input file into .gitignore |
| --[no-]gitignore | Insert .env input file into .gitignore |
| | (defaults to on) |

Example usage:
Expand Down
4 changes: 1 addition & 3 deletions bin/env_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ void main(List<String> arguments) async {
..addOption('input',
abbr: 'i', help: 'Input path of the .env file', mandatory: true)
..addOption('password',
abbr: 'p',
help: 'Password for encryption & decryption',
mandatory: true)
abbr: 'p', help: 'Password for encryption & decryption')
..addOption('output',
abbr: 'o',
help: 'Custom output path for the encrypted .env file',
Expand Down
21 changes: 16 additions & 5 deletions bin/src/file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,27 @@ void insertFile({required ArgResults from}) {
String target = output.contains("/")
? output.replaceAll(RegExp(r'/[^/]+$'), "/")
: Directory.current.path;
String password = from['password']!.toString();
String data = File(input).readAsStringSync();
Directory directory = Directory(target)..createSync(recursive: true);
String name = output.endsWith("/")
? (input.contains("/") ? input.split("/").last : input)
: output.contains("/")
? output.split("/").last
: output;
File asset = File(directory.path + name)
..writeAsStringSync(Encryptor.encrypt(password, data));
print(
"\x1B[32m$input\x1B[0m successfully encrypted into \x1B[34m${asset.path}\x1B[0m 🚀");

String? password = from['password']?.toString();
if (password != null) {
File asset = File(directory.path + name)
..writeAsStringSync(Encryptor.encrypt(password, data));
print(
"\x1B[32m$input\x1B[0m successfully encrypted into \x1B[34m${asset.path}\x1B[0m 🚀");
} else {
File asset = File(directory.path + name)..writeAsStringSync(data);
print(
"\x1B[32m$input\x1B[0m successfully duplicated into \x1B[34m${asset.path}\x1B[0m 🚀");
if (asset.path.startsWith("assets/")) {
print(
"\x1B[31mWarning:\x1B[0m Avoid openly placing exposed .env variables in assets 🔥");
}
}
}
6 changes: 3 additions & 3 deletions bin/src/json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void insertJson({required ArgResults from}) {
Type type = e.value.runtimeType;
String name = dartNamed(e.key);
String variable = nullSafety
? "Env.read<$type>('${e.key}') ?? ${type == bool ? 'false' : type == int ? '0' : type == double ? '0.0' : '"${e.key}"'}"
? "Env.read<$type>('${e.key}') ?? ${type == bool ? 'false' : type == int ? '0' : type == double ? '0.0' : "'${e.key}'"}"
: "Env.read<$type>('${e.key}')";

return """
Expand All @@ -39,8 +39,8 @@ void insertJson({required ArgResults from}) {
""";
}).join("\n");
String write = """
// Auto-generated by Env Reader
// Generated @ ${DateTime.now()}
// Env Reader Auto-Generated Model File
// Created at ${DateTime.now()}
// 🍔 [Buy me a coffee](https://www.buymeacoffee.com/nialixus) 🚀
import 'package:env_reader/env_reader.dart';
Expand Down
28 changes: 19 additions & 9 deletions example/.env
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
# Default Sample
MY_STRING=lorem ipsum dolor sit
MY_INT=12
MY_BOOL=false
MY_DOUBLE=3.14

# Custom Sample
API_KEY=my-secret-api-key
DATABASE_URL=mysql://username:password@localhost/dbname
API_KEY=8c7b13a4-1fb9-4f08-9eef-2459c4b07f25
DEBUG=true
PORT=8080
DATABASE_URL=postgresql://user:password@localhost:5432/mydb
SECRET_KEY=2d8efc6e92d74eacab45f47bc4d35e89
JWT_SECRET=7a1d86b5-e86f-4ee1-aa72-93d1fe98b02b
PORT=3000
APP_NAME=MyAwesomeApp
AWS_ACCESS_KEY=7b0d12ea-4a08-42cf-843f-6bf7e35a6f0e
AWS_SECRET_KEY=2f6a089e-19c7-4b8b-9b1d-74e7f0104390
REDIS_URL=redis://localhost:6379
MAIL_USERNAME=[email protected]
MAIL_PASSWORD=your-email-password
GOOGLE_CLIENT_ID=5d6f5a42-9c5e-4a23-82d1-14e4b7380b87
GOOGLE_CLIENT_SECRET=19be5ebd-6f64-4d64-8925-8a39a2077c25
FACEBOOK_APP_ID=3c74e7a0-3af1-4c8f-bc7a-1c8f2d9c0e79
FACEBOOK_APP_SECRET=c4ae5722-912c-47f5-b1a4-960e9e12a12a
STRIPE_SECRET_KEY=90a8c5df-9f6d-4e68-9c91-c5dd4e46ca95
S3_BUCKET_NAME=my-unique-s3-bucket
MONGO_URI=mongodb://username:password@localhost:27017/dbname
SENDGRID_API_KEY=6d4185ef-9a05-4f1c-af7b-78209b2c6c82
2 changes: 1 addition & 1 deletion example/assets/env/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GDE6V1uW1u0Z+LmxdgzW/vHLg1p/CXnYW08aWH4bOlyRtveyynXJfi9f56zos8FRbkCaBlve/4sitifqzpJ1+wpuVLyyFNY18D5RDlo3C2pWloP/61nGyPyf+kNq8zvUgCQzRGl6Db4NdbZcwL/sHX95V9ew81ezuLUXFmCMxMQPol8jS3mgJAf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Pl+zEHHF2/GIzWterBpYaR5Gtf4/oUb2X6v7sGeaAfyqSRhuI0OXLRTRQrPGTqItmAoUJF5Y+G/ylaoVPLt75DrkqJWRpt4N0M36M9OXV11S1FmklSHciJukFZ4P/0N2NDUpcDJOIwVZWOaK+Q4nB0gWAXmgKvkaFiBdD90tRGObrwEUVbpLfOidIH5TLyCH3h3Ax7ySF8kR7M227ibYjGdPvDQmwwFMNGJnpc=
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MyApp extends StatelessWidget {
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(Env.read<String>("MY_STRING") ?? "Oops"),
Text(Env.read<String>("DATABASE_URL") ?? "Oops"),
Text(EnvModel.apiKey),
],
),
Expand Down
137 changes: 110 additions & 27 deletions example/lib/src/env_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Auto-generated by Env Reader
// Generated @ 2023-08-28 14:40:30.402903
// Env Reader Auto-Generated Model File
// Created at 2023-08-29 10:08:02.009503
// 🍔 [Buy me a coffee](https://www.buymeacoffee.com/nialixus) 🚀
import 'package:env_reader/env_reader.dart';

Expand All @@ -10,52 +10,135 @@ import 'package:env_reader/env_reader.dart';
/// with default values provided for safety
/// `false` for [bool], `0` for [int], `0.0` for [double] and `VARIABLE_NAME` for [String].
class EnvModel {
/// Value of `MY_STRING` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('MY_STRING') ?? "MY_STRING";
/// ```
static String myString = Env.read<String>('MY_STRING') ?? "MY_STRING";

/// Value of `MY_INT` in environment variable. This is equal to
/// Value of `DATABASE_URL` in environment variable. This is equal to
/// ```dart
/// Env.read<int>('MY_INT') ?? 0;
/// Env.read<String>('DATABASE_URL') ?? 'DATABASE_URL';
/// ```
static int myInt = Env.read<int>('MY_INT') ?? 0;
static String databaseUrl =
Env.read<String>('DATABASE_URL') ?? 'DATABASE_URL';

/// Value of `MY_BOOL` in environment variable. This is equal to
/// Value of `API_KEY` in environment variable. This is equal to
/// ```dart
/// Env.read<bool>('MY_BOOL') ?? false;
/// Env.read<String>('API_KEY') ?? 'API_KEY';
/// ```
static bool myBool = Env.read<bool>('MY_BOOL') ?? false;
static String apiKey = Env.read<String>('API_KEY') ?? 'API_KEY';

/// Value of `MY_DOUBLE` in environment variable. This is equal to
/// Value of `DEBUG` in environment variable. This is equal to
/// ```dart
/// Env.read<double>('MY_DOUBLE') ?? 0.0;
/// Env.read<bool>('DEBUG') ?? false;
/// ```
static double myDouble = Env.read<double>('MY_DOUBLE') ?? 0.0;
static bool debug = Env.read<bool>('DEBUG') ?? false;

/// Value of `API_KEY` in environment variable. This is equal to
/// Value of `SECRET_KEY` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('API_KEY') ?? "API_KEY";
/// Env.read<String>('SECRET_KEY') ?? 'SECRET_KEY';
/// ```
static String apiKey = Env.read<String>('API_KEY') ?? "API_KEY";
static String secretKey = Env.read<String>('SECRET_KEY') ?? 'SECRET_KEY';

/// Value of `DEBUG` in environment variable. This is equal to
/// Value of `JWT_SECRET` in environment variable. This is equal to
/// ```dart
/// Env.read<bool>('DEBUG') ?? false;
/// Env.read<String>('JWT_SECRET') ?? 'JWT_SECRET';
/// ```
static bool debug = Env.read<bool>('DEBUG') ?? false;
static String jwtSecret = Env.read<String>('JWT_SECRET') ?? 'JWT_SECRET';

/// Value of `PORT` in environment variable. This is equal to
/// ```dart
/// Env.read<int>('PORT') ?? 0;
/// ```
static int port = Env.read<int>('PORT') ?? 0;

/// Value of `DATABASE_URL` in environment variable. This is equal to
/// Value of `APP_NAME` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('DATABASE_URL') ?? "DATABASE_URL";
/// Env.read<String>('APP_NAME') ?? 'APP_NAME';
/// ```
static String databaseUrl =
Env.read<String>('DATABASE_URL') ?? "DATABASE_URL";
static String appName = Env.read<String>('APP_NAME') ?? 'APP_NAME';

/// Value of `AWS_ACCESS_KEY` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('AWS_ACCESS_KEY') ?? 'AWS_ACCESS_KEY';
/// ```
static String awsAccessKey =
Env.read<String>('AWS_ACCESS_KEY') ?? 'AWS_ACCESS_KEY';

/// Value of `AWS_SECRET_KEY` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('AWS_SECRET_KEY') ?? 'AWS_SECRET_KEY';
/// ```
static String awsSecretKey =
Env.read<String>('AWS_SECRET_KEY') ?? 'AWS_SECRET_KEY';

/// Value of `REDIS_URL` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('REDIS_URL') ?? 'REDIS_URL';
/// ```
static String redisUrl = Env.read<String>('REDIS_URL') ?? 'REDIS_URL';

/// Value of `MAIL_USERNAME` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('MAIL_USERNAME') ?? 'MAIL_USERNAME';
/// ```
static String mailUsername =
Env.read<String>('MAIL_USERNAME') ?? 'MAIL_USERNAME';

/// Value of `MAIL_PASSWORD` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('MAIL_PASSWORD') ?? 'MAIL_PASSWORD';
/// ```
static String mailPassword =
Env.read<String>('MAIL_PASSWORD') ?? 'MAIL_PASSWORD';

/// Value of `GOOGLE_CLIENT_ID` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('GOOGLE_CLIENT_ID') ?? 'GOOGLE_CLIENT_ID';
/// ```
static String googleClientId =
Env.read<String>('GOOGLE_CLIENT_ID') ?? 'GOOGLE_CLIENT_ID';

/// Value of `GOOGLE_CLIENT_SECRET` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('GOOGLE_CLIENT_SECRET') ?? 'GOOGLE_CLIENT_SECRET';
/// ```
static String googleClientSecret =
Env.read<String>('GOOGLE_CLIENT_SECRET') ?? 'GOOGLE_CLIENT_SECRET';

/// Value of `FACEBOOK_APP_ID` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('FACEBOOK_APP_ID') ?? 'FACEBOOK_APP_ID';
/// ```
static String facebookAppId =
Env.read<String>('FACEBOOK_APP_ID') ?? 'FACEBOOK_APP_ID';

/// Value of `FACEBOOK_APP_SECRET` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('FACEBOOK_APP_SECRET') ?? 'FACEBOOK_APP_SECRET';
/// ```
static String facebookAppSecret =
Env.read<String>('FACEBOOK_APP_SECRET') ?? 'FACEBOOK_APP_SECRET';

/// Value of `STRIPE_SECRET_KEY` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('STRIPE_SECRET_KEY') ?? 'STRIPE_SECRET_KEY';
/// ```
static String stripeSecretKey =
Env.read<String>('STRIPE_SECRET_KEY') ?? 'STRIPE_SECRET_KEY';

/// Value of `S3_BUCKET_NAME` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('S3_BUCKET_NAME') ?? 'S3_BUCKET_NAME';
/// ```
static String s3BucketName =
Env.read<String>('S3_BUCKET_NAME') ?? 'S3_BUCKET_NAME';

/// Value of `MONGO_URI` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('MONGO_URI') ?? 'MONGO_URI';
/// ```
static String mongoUri = Env.read<String>('MONGO_URI') ?? 'MONGO_URI';

/// Value of `SENDGRID_API_KEY` in environment variable. This is equal to
/// ```dart
/// Env.read<String>('SENDGRID_API_KEY') ?? 'SENDGRID_API_KEY';
/// ```
static String sendgridApiKey =
Env.read<String>('SENDGRID_API_KEY') ?? 'SENDGRID_API_KEY';
}
9 changes: 4 additions & 5 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,10 @@ packages:
env_reader:
dependency: "direct main"
description:
name: env_reader
sha256: f5b8c74f257a7489c1197cb0b81974af78067896347fb5a593be0ee48ad364ee
url: "https://pub.dev"
source: hosted
version: "1.1.0"
path: ".."
relative: true
source: path
version: "1.4.0"
fake_async:
dependency: transitive
description:
Expand Down
Loading

0 comments on commit 758d475

Please sign in to comment.