From 04906c5355f860089c5f4fc9844ed728f16f1812 Mon Sep 17 00:00:00 2001 From: Andrew Ayer Date: Sun, 14 Sep 2014 14:52:36 -0700 Subject: [PATCH] Write a usage message for every command You can run 'git-crypt help COMMAND' to see it. --- commands.cpp | 109 +++++++++++++++++++++++++++++++++--- commands.hpp | 15 ++++- git-crypt.cpp | 149 ++++++++++++++++++++++++++++++++------------------ 3 files changed, 211 insertions(+), 62 deletions(-) diff --git a/commands.cpp b/commands.cpp index 083d033..47fda6f 100644 --- a/commands.cpp +++ b/commands.cpp @@ -693,6 +693,15 @@ int diff (int argc, const char** argv) return decrypt_file_to_stdout(key_file, header, in); } +void help_init (std::ostream& out) +{ + // |--------------------------------------------------------------------------------| 80 chars + out << "Usage: git-crypt init [OPTIONS]" << std::endl; + out << std::endl; + out << " -k, --key-name KEYNAME Initialize the given key, instead of the default" << std::endl; + out << std::endl; +} + int init (int argc, const char** argv) { const char* key_name = 0; @@ -709,7 +718,8 @@ int init (int argc, const char** argv) return unlock(argc, argv); } if (argc - argi != 0) { - std::clog << "Usage: git-crypt init [-k KEYNAME]" << std::endl; + std::clog << "Error: git-crypt init takes no arguments" << std::endl; + help_init(std::clog); return 2; } @@ -743,6 +753,12 @@ int init (int argc, const char** argv) return 0; } +void help_unlock (std::ostream& out) +{ + // |--------------------------------------------------------------------------------| 80 chars + out << "Usage: git-crypt unlock" << std::endl; + out << " or: git-crypt unlock KEY_FILE ..." << std::endl; +} int unlock (int argc, const char** argv) { // 0. Make sure working directory is clean (ignoring untracked files) @@ -848,6 +864,15 @@ int unlock (int argc, const char** argv) return 0; } +void help_lock (std::ostream& out) +{ + // |--------------------------------------------------------------------------------| 80 chars + out << "Usage: git-crypt lock [OPTIONS]" << std::endl; + out << std::endl; + out << " -a, --all Lock all keys, instead of just the default" << std::endl; + out << " -k, --key-name KEYNAME Lock the given key, instead of the default" << std::endl; + out << std::endl; +} int lock (int argc, const char** argv) { const char* key_name = 0; @@ -861,7 +886,8 @@ int lock (int argc, const char** argv) int argi = parse_options(options, argc, argv); if (argc - argi != 0) { - std::clog << "Usage: git-crypt lock [-k KEYNAME] [--all]" << std::endl; + std::clog << "Error: git-crypt lock takes no arguments" << std::endl; + help_lock(std::clog); return 2; } @@ -938,6 +964,15 @@ int lock (int argc, const char** argv) return 0; } +void help_add_gpg_key (std::ostream& out) +{ + // |--------------------------------------------------------------------------------| 80 chars + out << "Usage: git-crypt add-gpg-key [OPTIONS] GPG_USER_ID ..." << std::endl; + out << std::endl; + out << " -k, --key-name KEYNAME Add GPG user to given key, instead of default" << std::endl; + out << " -n, --no-commit Don't automatically commit" << std::endl; + out << std::endl; +} int add_gpg_key (int argc, const char** argv) { const char* key_name = 0; @@ -950,7 +985,8 @@ int add_gpg_key (int argc, const char** argv) int argi = parse_options(options, argc, argv); if (argc - argi == 0) { - std::clog << "Usage: git-crypt add-collab [-k KEYNAME] GPG_USER_ID [...]" << std::endl; + std::clog << "Error: no GPG user ID specified" << std::endl; + help_add_gpg_key(std::clog); return 2; } @@ -1025,12 +1061,26 @@ int add_gpg_key (int argc, const char** argv) return 0; } +void help_rm_gpg_key (std::ostream& out) +{ + // |--------------------------------------------------------------------------------| 80 chars + out << "Usage: git-crypt rm-gpg-key [OPTIONS] GPG_USER_ID ..." << std::endl; + out << std::endl; + out << " -k, --key-name KEYNAME Remove user from given key, instead of default" << std::endl; + out << " -n, --no-commit Don't automatically commit" << std::endl; + out << std::endl; +} int rm_gpg_key (int argc, const char** argv) // TODO { std::clog << "Error: rm-gpg-key is not yet implemented." << std::endl; return 1; } +void help_ls_gpg_keys (std::ostream& out) +{ + // |--------------------------------------------------------------------------------| 80 chars + out << "Usage: git-crypt ls-gpg-keys" << std::endl; +} int ls_gpg_keys (int argc, const char** argv) // TODO { // Sketch: @@ -1051,6 +1101,15 @@ int ls_gpg_keys (int argc, const char** argv) // TODO return 1; } +void help_export_key (std::ostream& out) +{ + // |--------------------------------------------------------------------------------| 80 chars + out << "Usage: git-crypt export-key [OPTIONS] FILENAME" << std::endl; + out << std::endl; + out << " -k, --key-name KEYNAME Export the given key, instead of the default" << std::endl; + out << std::endl; + out << "When FILENAME is -, export to standard out." << std::endl; +} int export_key (int argc, const char** argv) { // TODO: provide options to export only certain key versions @@ -1062,7 +1121,8 @@ int export_key (int argc, const char** argv) int argi = parse_options(options, argc, argv); if (argc - argi != 1) { - std::clog << "Usage: git-crypt export-key [-k KEYNAME] FILENAME" << std::endl; + std::clog << "Error: no filename specified" << std::endl; + help_export_key(std::clog); return 2; } @@ -1083,10 +1143,18 @@ int export_key (int argc, const char** argv) return 0; } +void help_keygen (std::ostream& out) +{ + // |--------------------------------------------------------------------------------| 80 chars + out << "Usage: git-crypt keygen FILENAME" << std::endl; + out << std::endl; + out << "When FILENAME is -, write to standard out." << std::endl; +} int keygen (int argc, const char** argv) { if (argc != 1) { - std::clog << "Usage: git-crypt keygen KEYFILE" << std::endl; + std::clog << "Error: no filename specified" << std::endl; + help_keygen(std::clog); return 2; } @@ -1112,10 +1180,18 @@ int keygen (int argc, const char** argv) return 0; } +void help_migrate_key (std::ostream& out) +{ + // |--------------------------------------------------------------------------------| 80 chars + out << "Usage: git-crypt migrate-key FILENAME" << std::endl; + out << std::endl; + out << "When FILENAME is -, read from standard in and write to standard out." << std::endl; +} int migrate_key (int argc, const char** argv) { if (argc != 1) { - std::clog << "Usage: git-crypt migrate-key KEYFILE" << std::endl; + std::clog << "Error: no filename specified" << std::endl; + help_migrate_key(std::clog); return 2; } @@ -1162,12 +1238,31 @@ int migrate_key (int argc, const char** argv) return 0; } +void help_refresh (std::ostream& out) +{ + // |--------------------------------------------------------------------------------| 80 chars + out << "Usage: git-crypt refresh" << std::endl; +} int refresh (int argc, const char** argv) // TODO: do a force checkout, much like in unlock { std::clog << "Error: refresh is not yet implemented." << std::endl; return 1; } +void help_status (std::ostream& out) +{ + // |--------------------------------------------------------------------------------| 80 chars + out << "Usage: git-crypt status [OPTIONS] [FILE ...]" << std::endl; + //out << " or: git-crypt status -r [OPTIONS]" << std::endl; + //out << " or: git-crypt status -f" << std::endl; + out << std::endl; + out << " -e Show encrypted files only" << std::endl; + out << " -u Show unencrypted files only" << std::endl; + //out << " -r Show repository status only" << std::endl; + out << " -f, --fix Fix problems with the repository" << std::endl; + //out << " -z Machine-parseable output" << std::endl; + out << std::endl; +} int status (int argc, const char** argv) { // Usage: @@ -1175,8 +1270,6 @@ int status (int argc, const char** argv) // git-crypt status [-e | -u] [-z] [FILE ...] Show encrypted status of files // git-crypt status -f Fix unencrypted blobs - // TODO: help option / usage output - bool repo_status_only = false; // -r show repo status only bool show_encrypted_only = false; // -e show encrypted files only bool show_unencrypted_only = false; // -u show unencrypted files only diff --git a/commands.hpp b/commands.hpp index abb936d..2bc603c 100644 --- a/commands.hpp +++ b/commands.hpp @@ -32,6 +32,7 @@ #define GIT_CRYPT_COMMANDS_HPP #include +#include struct Error { std::string message; @@ -56,5 +57,17 @@ int migrate_key (int argc, const char** argv); int refresh (int argc, const char** argv); int status (int argc, const char** argv); -#endif +// Help messages: +void help_init (std::ostream&); +void help_unlock (std::ostream&); +void help_lock (std::ostream&); +void help_add_gpg_key (std::ostream&); +void help_rm_gpg_key (std::ostream&); +void help_ls_gpg_keys (std::ostream&); +void help_export_key (std::ostream&); +void help_keygen (std::ostream&); +void help_migrate_key (std::ostream&); +void help_refresh (std::ostream&); +void help_status (std::ostream&); +#endif diff --git a/git-crypt.cpp b/git-crypt.cpp index c69bef7..ab107af 100644 --- a/git-crypt.cpp +++ b/git-crypt.cpp @@ -74,10 +74,51 @@ static void print_usage (std::ostream& out) out << " smudge [LEGACY-KEYFILE]" << std::endl; out << " diff [LEGACY-KEYFILE] FILE" << std::endl; */ - /* out << std::endl; out << "See 'git-crypt help COMMAND' for more information on a specific command." << std::endl; - */ +} + +static bool help_for_command (const char* command, std::ostream& out) +{ + if (std::strcmp(command, "init") == 0) { + help_init(out); + } else if (std::strcmp(command, "unlock") == 0) { + help_unlock(out); + } else if (std::strcmp(command, "lock") == 0) { + help_lock(out); + } else if (std::strcmp(command, "add-gpg-key") == 0) { + help_add_gpg_key(out); + } else if (std::strcmp(command, "rm-gpg-key") == 0) { + help_rm_gpg_key(out); + } else if (std::strcmp(command, "ls-gpg-keys") == 0) { + help_ls_gpg_keys(out); + } else if (std::strcmp(command, "export-key") == 0) { + help_export_key(out); + } else if (std::strcmp(command, "keygen") == 0) { + help_keygen(out); + } else if (std::strcmp(command, "migrate-key") == 0) { + help_migrate_key(out); + } else if (std::strcmp(command, "refresh") == 0) { + help_refresh(out); + } else if (std::strcmp(command, "status") == 0) { + help_status(out); + } else { + return false; + } + return true; +} + +static int help (int argc, const char** argv) +{ + if (argc == 0) { + print_usage(std::cout); + } else { + if (!help_for_command(argv[0], std::cout)) { + std::clog << "Error: '" << argv[0] << "' is not a git-crypt command. See 'git-crypt help'." << std::endl; + return 1; + } + } + return 0; } @@ -125,56 +166,61 @@ try { --argc; ++argv; - // Public commands: - if (std::strcmp(command, "help") == 0) { - print_usage(std::clog); - return 0; - } - if (std::strcmp(command, "init") == 0) { - return init(argc, argv); - } - if (std::strcmp(command, "unlock") == 0) { - return unlock(argc, argv); - } - if (std::strcmp(command, "lock") == 0) { - return lock(argc, argv); - } - if (std::strcmp(command, "add-gpg-key") == 0) { - return add_gpg_key(argc, argv); - } - if (std::strcmp(command, "rm-gpg-key") == 0) { - return rm_gpg_key(argc, argv); - } - if (std::strcmp(command, "ls-gpg-keys") == 0) { - return ls_gpg_keys(argc, argv); - } - if (std::strcmp(command, "export-key") == 0) { - return export_key(argc, argv); - } - if (std::strcmp(command, "keygen") == 0) { - return keygen(argc, argv); - } - if (std::strcmp(command, "migrate-key") == 0) { - return migrate_key(argc, argv); - } - if (std::strcmp(command, "refresh") == 0) { - return refresh(argc, argv); - } - if (std::strcmp(command, "status") == 0) { - return status(argc, argv); - } - // Plumbing commands (executed by git, not by user): - if (std::strcmp(command, "clean") == 0) { - return clean(argc, argv); - } - if (std::strcmp(command, "smudge") == 0) { - return smudge(argc, argv); - } - if (std::strcmp(command, "diff") == 0) { - return diff(argc, argv); + try { + // Public commands: + if (std::strcmp(command, "help") == 0) { + return help(argc, argv); + } + if (std::strcmp(command, "init") == 0) { + return init(argc, argv); + } + if (std::strcmp(command, "unlock") == 0) { + return unlock(argc, argv); + } + if (std::strcmp(command, "lock") == 0) { + return lock(argc, argv); + } + if (std::strcmp(command, "add-gpg-key") == 0) { + return add_gpg_key(argc, argv); + } + if (std::strcmp(command, "rm-gpg-key") == 0) { + return rm_gpg_key(argc, argv); + } + if (std::strcmp(command, "ls-gpg-keys") == 0) { + return ls_gpg_keys(argc, argv); + } + if (std::strcmp(command, "export-key") == 0) { + return export_key(argc, argv); + } + if (std::strcmp(command, "keygen") == 0) { + return keygen(argc, argv); + } + if (std::strcmp(command, "migrate-key") == 0) { + return migrate_key(argc, argv); + } + if (std::strcmp(command, "refresh") == 0) { + return refresh(argc, argv); + } + if (std::strcmp(command, "status") == 0) { + return status(argc, argv); + } + // Plumbing commands (executed by git, not by user): + if (std::strcmp(command, "clean") == 0) { + return clean(argc, argv); + } + if (std::strcmp(command, "smudge") == 0) { + return smudge(argc, argv); + } + if (std::strcmp(command, "diff") == 0) { + return diff(argc, argv); + } + } catch (const Option_error& e) { + std::clog << "git-crypt: Error: " << e.option_name << ": " << e.message << std::endl; + help_for_command(command, std::clog); + return 2; } - print_usage(std::clog); + std::clog << "Error: '" << command << "' is not a git-crypt command. See 'git-crypt help'." << std::endl; return 2; } catch (const Error& e) { @@ -189,9 +235,6 @@ try { } catch (const Crypto_error& e) { std::cerr << "git-crypt: Crypto error: " << e.where << ": " << e.message << std::endl; return 1; -} catch (const Option_error& e) { - std::cerr << "git-crypt: Error: " << e.option_name << ": " << e.message << std::endl; - return 1; } catch (Key_file::Incompatible) { std::cerr << "git-crypt: This repository contains a incompatible key file. Please upgrade git-crypt." << std::endl; return 1;