From e851b359549a90e81c63403aee737ee8efb100b6 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Mon, 9 Jun 2014 13:26:55 +0100 Subject: [PATCH] [thin_delta] read in the snaps and dev --- features/thin_delta.feature | 25 +++++++++- thin-provisioning/thin_delta.cc | 81 +++++++++++++++++++++++++++++---- 2 files changed, 95 insertions(+), 11 deletions(-) diff --git a/features/thin_delta.feature b/features/thin_delta.feature index 2bfe0db..4bab632 100644 --- a/features/thin_delta.feature +++ b/features/thin_delta.feature @@ -12,7 +12,7 @@ Feature: thin_delta Then it should pass with: """ - Usage: thin_delta [options] + Usage: thin_delta [options] --snap1 --snap2 Options: {-h|--help} {-V|--version} @@ -22,7 +22,7 @@ Feature: thin_delta When I run `thin_delta -h` Then it should pass with: """ - Usage: thin_delta [options] + Usage: thin_delta [options] --snap1 --snap2 Options: {-h|--help} {-V|--version} @@ -31,3 +31,24 @@ Feature: thin_delta Scenario: Unrecognised option should cause failure When I run `thin_delta --unleash-the-hedeghogs` Then it should fail + + Scenario: --snap1 must be specified + When I run `thin_delta --snap2 45 foo` + Then it should fail with: + """ + --snap1 not specified. + """ + + Scenario: --snap2 must be specified + When I run `thin_delta --snap1 45 foo` + Then it should fail with: + """ + --snap2 not specified. + """ + + Scenario: device must be specified + When I run `thin_delta --snap1 45 --snap2 50` + Then it should fail with: + """ + No input device provided. + """ diff --git a/thin-provisioning/thin_delta.cc b/thin-provisioning/thin_delta.cc index 0c4299e..bbd1ec2 100644 --- a/thin-provisioning/thin_delta.cc +++ b/thin-provisioning/thin_delta.cc @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -9,41 +11,102 @@ using namespace std; //---------------------------------------------------------------- namespace { - void usage(ostream &out, string const &cmd) { - out << "Usage: " << cmd << " [options]" << endl - << "Options:" << endl - << " {-h|--help}" << endl - << " {-V|--version}" << endl; - } + class application { + public: + application(string const &cmd) + : cmd_(cmd) { + } + + void usage(ostream &out) { + out << "Usage: " << cmd_ << " [options] --snap1 --snap2 " << endl + << "Options:" << endl + << " {-h|--help}" << endl + << " {-V|--version}" << endl; + } + + void die(string const &msg) { + cerr << msg << endl; + usage(cerr); + exit(1); + } + + unsigned parse_snap(string const &str) { + try { + return boost::lexical_cast(str); + + } catch (...) { + ostringstream out; + out << "Couldn't parse snapshot designator: '" << str << "'"; + die(out.str()); + } + + return 0; // never get here + } + + private: + string cmd_; + }; + + struct flags { + boost::optional dev; + boost::optional snap1; + boost::optional snap2; + }; } //---------------------------------------------------------------- +// FIXME: add metadata snap switch + int main(int argc, char **argv) { int c; + flags fs; + application app(basename(argv[0])); + char const shortopts[] = "hV"; option const longopts[] = { { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'V' } + { "version", no_argument, NULL, 'V' }, + { "snap1", required_argument, NULL, 1 }, + { "snap2", required_argument, NULL, 2 }, }; while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { switch (c) { case 'h': - usage(cout, basename(argv[0])); + app.usage(cout); return 0; case 'V': cout << THIN_PROVISIONING_TOOLS_VERSION << endl; return 0; + case 1: + fs.snap1 = app.parse_snap(optarg); + break; + + case 2: + fs.snap2 = app.parse_snap(optarg); + break; + default: - usage(cerr, basename(argv[0])); + app.usage(cerr); return 1; } } + if (argc == optind) + app.die("No input device provided."); + else + fs.dev = argv[optind]; + + if (!fs.snap1) + app.die("--snap1 not specified."); + + if (!fs.snap2) + app.die("--snap2 not specified."); + return 0; }