import snag; import commandr; import std.file; import std.stdio : writeln; import core.stdc.stdlib : EXIT_SUCCESS, EXIT_FAILURE; private string programName = "snag"; int main(string[] args) { if (!checkGit()) { "Using snag requires Git to be installed".writeln; return EXIT_FAILURE; } auto argumets = new Program(programName, snagVersion) .add(new Command("init", "Initializing the repository for storing snapshots")) .add(new Command("status", "Checking the status of tracked files")) .add(new Command("diff", "Show changed data")) .add(new Command("import", "Import snapshot from a tar.gz archive") .add(new Argument("archive", "The path to the tar.gz archive file").required) .add(new Option("c", "comment", "Specify comment") .optional .validateEachWith( opt => opt.length > 0, "cannot be empty" ) ) .add(new Option("a", "author", "Specify author") .optional .validateEachWith( opt => opt.length > 0, "cannot be empty" ) ) .add(new Option("e", "email", "Specify email") .optional .validateEachWith( opt => opt.isValidEmail, "must contain an email address" ) ) ) .add(new Command("export", "Export snapshot to a tar.gz archive") .add(new Argument("path", "Output directory path for the archive").required) .add(new Option("s", "snapshot", "Specify snapshot hash") .optional .validateEachWith( opt => opt.isValidHash, "must contain snapshot hash provided" ) ) ) .add(new Command("create", "Create a new snapshot") .add(new Option("c", "comment", "Specify comment") .optional .validateEachWith( opt => opt.length > 0, "cannot be empty" ) ) .add(new Option("a", "author", "Specify author") .optional .validateEachWith( opt => opt.length > 0, "cannot be empty" ) ) .add(new Option("e", "email", "Specify email") .optional .validateEachWith( opt => opt.isValidEmail, "must contain an email address" ) ) ) .add(new Command("list", "List of snapshots") .add(new Flag("c", "comment", "Show comment") .name("comment") .optional ) .add(new Flag("a", "author", "Show author") .name("author") .optional ) .add(new Flag("e", "email", "Show email") .name("email") .optional ) ) .add(new Command("restore", "Restore to the specified snapshot state") .add(new Argument("snapshot", "Specify snapshot hash").required) ) .add(new Option("c", "config", "Сonfiguration file path") .optional .validateEachWith( opt => opt.exists && opt.isFile, "must specify the path to the JSON file" ) ) .parse(args); string configFile = argumets.option("config", "snag.json"); SnagConfig config; try { config = new SnagConfig(configFile); } catch (SnagConfigException e) { e.print(); return EXIT_FAILURE; } auto snag = new Snag(config); try { argumets .on("init", init => snag.initialize() ) .on("diff", diff => snag.diff() ) .on("status", status => snag.status() ) .on("create", create => snag.create( create.option("comment", ""), create.option("author", ""), create.option("email", "") ) ) .on("list", list => snag.list( list.flag("comment"), list.flag("author"), list.flag("email") ) ) .on("restore", restore => snag.restore(restore.arg("snapshot")) ) .on("export", e => snag.exportSnapshot( e.arg("path"), e.option("snapshot", ""), ) ) .on("import", i => snag.importSnapshot( i.arg("archive"), i.option("comment", ""), i.option("author", ""), i.option("email", "") ) ); } catch (SnagException e) { e.print(); return EXIT_FAILURE; } return EXIT_SUCCESS; }