mirror of
https://github.com/java-james/flutter_dotenv.git
synced 2025-07-14 03:23:52 +08:00
@ -1,9 +1,32 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:args/args.dart';
|
||||
import 'package:dotenv/dotenv.dart' as dotenv;
|
||||
|
||||
final _argPsr = new ArgParser()
|
||||
..addFlag('help', abbr: 'h', negatable: false, help: 'Print this help text.')
|
||||
..addOption('file',
|
||||
abbr: 'f',
|
||||
defaultsTo: '.env',
|
||||
help: 'File to read.\nProvides environment variable definitions, one per line.');
|
||||
|
||||
/// Prints the [env] map.
|
||||
void main() {
|
||||
dotenv.load();
|
||||
///
|
||||
/// ## usage
|
||||
///
|
||||
/// pub global run dotenv --help
|
||||
void main(List<String> argv) {
|
||||
var opts = _argPsr.parse(argv);
|
||||
|
||||
if (opts['help']) return _usage();
|
||||
|
||||
dotenv.load(opts['file']);
|
||||
stdout.writeln(dotenv.env);
|
||||
}
|
||||
|
||||
void _usage() {
|
||||
_p('Parse variable definitions from a file, print the environment and exit.');
|
||||
_p('Usage: pub global run dotenv [-f <file>]\n${_argPsr.usage}');
|
||||
}
|
||||
|
||||
void _p(String msg) => stdout.writeln(msg);
|
||||
|
44
bin/new.dart
44
bin/new.dart
@ -1,25 +1,40 @@
|
||||
import 'dart:io';
|
||||
|
||||
const String envFile = '.env';
|
||||
import 'package:args/args.dart';
|
||||
|
||||
const String gitignore = '.gitignore';
|
||||
|
||||
final _argPsr = new ArgParser()
|
||||
..addFlag('help', abbr: 'h', negatable: false, help: 'Print this help text.')
|
||||
..addOption('file',
|
||||
abbr: 'f',
|
||||
defaultsTo: '.env',
|
||||
help: 'File to create.\nDo not check secrets into version control!');
|
||||
|
||||
/// Creates `.env` if the file does not exist, and adds it to `.gitignore`.
|
||||
void main() {
|
||||
///
|
||||
/// ## usage
|
||||
///
|
||||
/// pub global run dotenv:new --help
|
||||
void main(List<String> args) {
|
||||
var opts = _argPsr.parse(args);
|
||||
|
||||
if (opts['help']) return _usage();
|
||||
|
||||
String envFile = opts['file'];
|
||||
_touch(envFile);
|
||||
|
||||
var g = _touch(gitignore);
|
||||
if (_anyLineContains(envFile, g)) return _handleError();
|
||||
if (_anyLineContains(envFile, g)) return _handleIgnored(envFile);
|
||||
|
||||
g.writeAsStringSync('$envFile*\n', mode: FileMode.APPEND);
|
||||
stdout.writeln('Added \'$envFile*\' to $gitignore.');
|
||||
_appendTo(g, '$envFile*');
|
||||
}
|
||||
|
||||
bool _anyLineContains(String str, File f) =>
|
||||
f.readAsLinesSync().any((line) => line.contains(str));
|
||||
|
||||
void _handleError() {
|
||||
stderr
|
||||
.writeln('Found $gitignore with line containing \'$envFile\'; exiting.');
|
||||
void _handleIgnored(String filename) {
|
||||
_pErr("Found $gitignore with line containing '$filename'; exiting.");
|
||||
exitCode = 1;
|
||||
}
|
||||
|
||||
@ -31,3 +46,16 @@ File _touch(String filename) {
|
||||
stdout.writeln('Created file: $filename');
|
||||
return f;
|
||||
}
|
||||
|
||||
void _appendTo(File f, String line) {
|
||||
f.writeAsStringSync('$line\n', mode: FileMode.APPEND);
|
||||
_p("Added '$line' to ${f.path}.");
|
||||
}
|
||||
|
||||
void _usage() {
|
||||
_p('Create a new file and gitignore it.');
|
||||
_p('Usage: pub global run dotenv:new [-f <file>]\n${_argPsr.usage}');
|
||||
}
|
||||
|
||||
void _p(String msg) => stdout.writeln(msg);
|
||||
void _pErr(String msg) => stderr.writeln(msg);
|
||||
|
@ -1,6 +1,5 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Add to `.git/hooks/pre-commit`:
|
||||
# exec ./tool/fmt.sh
|
||||
# Autoformat code in-place, per style guidelines.
|
||||
|
||||
dartfmt -w bin lib test example
|
||||
|
Reference in New Issue
Block a user