Closes #8.
This commit is contained in:
mockturtl
2015-05-15 01:59:17 -04:00
parent c978a66687
commit acf64e5492
3 changed files with 62 additions and 12 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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