entr

[hare] Execute commands on filesystem events
Log | Files | Refs | README | LICENSE

entr.ha (1753B)


      1 use fmt;
      2 use fs;
      3 use getopt;
      4 use io;
      5 use os;
      6 use os::exec;
      7 
      8 export fn main() void = {
      9 	const help: [_]getopt::help = [
     10 		"run abitrary commands when files change",
     11 		('a', "respond to all events while the utility is running"),
     12 		('c', "clear screen. specify twice to erase scrollback buffer"),
     13 		('d', "track directories, exit when a new file is added"),
     14 		('n', "non-interactive mode"),
     15 		('p', "postpone first invocation until a file is modified"),
     16 		('r', "reload a persistent child process"),
     17 		('s', "evaluate the first argument interpreted by $SHELL"),
     18 		('z', "exit after utility completes"),
     19 		"utility [argument [/_] ...] < filenames",
     20 	];
     21 	const cmd = getopt::parse(os::args, help...);
     22 	defer getopt::finish(&cmd);
     23 
     24 	for (let i = 0z; i < len(cmd.opts); i += 1) {
     25 		const opt = cmd.opts[i];
     26 		switch (opt.0) {
     27 		case 'a' =>
     28 			abort("unimplemented option: -a");
     29 		case 'c' =>
     30 			abort("unimplemented option: -c");
     31 		case 'd' =>
     32 			abort("unimplemented option: -d");
     33 		case 'n' =>
     34 			abort("unimplemented option: -n");
     35 		case 'p' =>
     36 			abort("unimplemented option: -p");
     37 		case 'r' =>
     38 			abort("unimplemented option: -r");
     39 		case 's' =>
     40 			abort("unimplemented option: -s");
     41 		case 'z' =>
     42 			abort("unimplemented option: -z");
     43 		};
     44 	};
     45 
     46 	if (len(cmd.args) == 0) {
     47 		usage(help, void);
     48 	};
     49 
     50 	const args = if (len(cmd.args) > 1) cmd.args[1..] else []: []str;
     51 	const util = os::exec::cmd(cmd.args[0], args...)!;
     52 
     53 	const proc = os::exec::start(&util)!;
     54 	const stat = os::exec::wait(&proc);
     55 };
     56 
     57 @noreturn fn usage(help: []getopt::help, flag: (rune | void)) void = {
     58 	if (flag is rune) {
     59 		fmt::errorfln("{}: invalid option parameter for -{}",
     60 			os::args[0], flag: rune,
     61 		)!;
     62 	};
     63 	getopt::printusage(os::stderr, os::args[0], help);
     64 	os::exit(1);
     65 };