From 5fc0b7cae8044848919931ac90ed4ede44ec8806 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Thu, 1 Feb 2018 10:21:47 -0800 Subject: [PATCH] Add --version option that shows the CryFS version and exits. --- ChangeLog.txt | 1 + doc/man/cryfs.1 | 8 +++++++- src/cryfs-cli/program_options/Parser.cpp | 10 ++++++++++ src/cryfs-cli/program_options/Parser.h | 1 + test/cryfs-cli/program_options/ParserTest.cpp | 11 +++++++++++ 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 15a4e6ce..88ee4a56 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -2,6 +2,7 @@ Version 0.9.9 (unreleased) -------------- Improvements: * Add --allow-filesystem-upgrade option which will upgrade old file systems without asking the user. This will be especially helpful for GUI tools. +* Add --version option that shows the CryFS version and exits. Version 0.9.8 -------------- diff --git a/doc/man/cryfs.1 b/doc/man/cryfs.1 index 7e7ea0dd..977d8113 100644 --- a/doc/man/cryfs.1 +++ b/doc/man/cryfs.1 @@ -19,7 +19,7 @@ cryfs \- cryptographic filesystem for the cloud [\fB\-\-\fR \fIfuse-options\fR] .br .\" show-ciphers syntax -.B cryfs \-\-help\fR|\fB\-\-show-ciphers +.B cryfs \-\-help\fR|\fB\-\-version\fR|\fB\-\-show-ciphers . . . @@ -119,6 +119,12 @@ Show a help message containing short descriptions for all options. Show a list of all supported encryption ciphers. . . +.TP +\fB\-\-version\fR +. +Show the CryFS version number. +. +. .SS Encryption parameters . .TP diff --git a/src/cryfs-cli/program_options/Parser.cpp b/src/cryfs-cli/program_options/Parser.cpp index 31eb85ae..9ebf807f 100644 --- a/src/cryfs-cli/program_options/Parser.cpp +++ b/src/cryfs-cli/program_options/Parser.cpp @@ -12,6 +12,7 @@ using cryfs::CryConfigConsole; using std::pair; using std::vector; using std::cerr; +using std::cout; using std::endl; using std::string; using boost::optional; @@ -105,6 +106,9 @@ po::variables_map Parser::_parseOptions(const vector &options, const vec if (vm.count("show-ciphers")) { _showCiphersAndExit(supportedCiphers); } + if (vm.count("version")) { + _showVersionAndExit(); + } po::notify(vm); return vm; @@ -135,6 +139,7 @@ void Parser::_addAllowedOptions(po::options_description *desc) { ("show-ciphers", "Show list of supported ciphers.") ("unmount-idle", po::value(), "Automatically unmount after specified number of idle minutes.") ("logfile", po::value(), "Specify the file to write log messages to. If this is not specified, log messages will go to stdout, or syslog if CryFS is running in the background.") + ("version", "Show CryFS version number") ; desc->add(options); } @@ -174,3 +179,8 @@ void Parser::_addPositionalOptionForBaseDir(po::options_description *desc, po::p << endl; exit(1); } + +[[noreturn]] void Parser::_showVersionAndExit() { + // no need to show version because it was already shown in the CryFS header before parsing program options + exit(1); +} diff --git a/src/cryfs-cli/program_options/Parser.h b/src/cryfs-cli/program_options/Parser.h index 0813e420..97b1982c 100644 --- a/src/cryfs-cli/program_options/Parser.h +++ b/src/cryfs-cli/program_options/Parser.h @@ -21,6 +21,7 @@ namespace cryfs { boost::program_options::positional_options_description *positional); [[noreturn]] static void _showHelpAndExit(); [[noreturn]] static void _showCiphersAndExit(const std::vector &supportedCiphers); + [[noreturn]] static void _showVersionAndExit(); static boost::program_options::variables_map _parseOptionsOrShowHelp(const std::vector &options, const std::vector &supportedCiphers); static boost::program_options::variables_map _parseOptions(const std::vector &options, const std::vector &supportedCiphers); static void _checkValidCipher(const std::string &cipher, const std::vector &supportedCiphers); diff --git a/test/cryfs-cli/program_options/ParserTest.cpp b/test/cryfs-cli/program_options/ParserTest.cpp index fbd9ca3b..45539207 100644 --- a/test/cryfs-cli/program_options/ParserTest.cpp +++ b/test/cryfs-cli/program_options/ParserTest.cpp @@ -2,6 +2,7 @@ #include #include #include +#include using namespace cryfs; using namespace cryfs::program_options; @@ -54,6 +55,16 @@ TEST_F(ProgramOptionsParserTest, ShowCiphers) { ); } + +TEST_F(ProgramOptionsParserTest, Version) { + string expected = "CryFS Version " + gitversion::VersionString(); + EXPECT_EXIT( + parse({"./myExecutable", "--version"}), + ::testing::ExitedWithCode(0), + expected.c_str() + ); +} + TEST_F(ProgramOptionsParserTest, BaseDir_Absolute) { ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir"}); EXPECT_EQ("/home/user/baseDir", options.baseDir());