diff --git a/bin/dotenv.dart b/bin/dotenv.dart index 0cb5ca4..29a76e9 100644 --- a/bin/dotenv.dart +++ b/bin/dotenv.dart @@ -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 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 ]\n${_argPsr.usage}'); +} + +void _p(String msg) => stdout.writeln(msg); diff --git a/bin/new.dart b/bin/new.dart index fea8486..78bf542 100644 --- a/bin/new.dart +++ b/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 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 ]\n${_argPsr.usage}'); +} + +void _p(String msg) => stdout.writeln(msg); +void _pErr(String msg) => stderr.writeln(msg); diff --git a/tool/fmt.sh b/tool/fmt.sh index 852bc25..c541237 100755 --- a/tool/fmt.sh +++ b/tool/fmt.sh @@ -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