diff --git a/source/src/commands/hash.cpp b/source/src/commands/hash.cpp new file mode 100644 index 0000000..584dc3f --- /dev/null +++ b/source/src/commands/hash.cpp @@ -0,0 +1,83 @@ +#include "command_registry.hpp" +#include "config.hpp" +#include "utils/utils.hpp" +#include "utils/directories.hpp" +#include "shared_commands.hpp" +#include "version.hpp" +#include "hash.hpp" + +#include +#include +#include +#include +#include +#include + +namespace dropshell { + +void hash_autocomplete(const CommandContext& ctx); +int hash_handler(const CommandContext& ctx); + +static std::vector hash_name_list={"hash"}; + +// Static registration +struct HashCommandRegister { + HashCommandRegister() { + CommandRegistry::instance().register_command({ + hash_name_list, + hash_handler, + hash_autocomplete, + false, // hidden + false, // requires_config + false, // requires_install + 0, // min_args (after command) + 1, // max_args (after command) + "hash [FILE|DIRECTORY]", + "Hash a file or directory.", + // heredoc + R"( + Hash a file or directory recursively. + )" + }); + } +} hash_command_register; + + +void hash_autocomplete(const CommandContext& ctx) { + if (ctx.args.size() == 0) { + // list all files and directories in the current directory + for (const auto& entry : std::filesystem::directory_iterator(".")) { + rawout << entry.path().string() << std::endl; + } + } + return; +} + +int hash_handler(const CommandContext& ctx) { + std::filesystem::path path = safearg(ctx.args, 0); + if (path.empty()) + path=std::filesystem::current_path(); + + if (!std::filesystem::exists(path)) + { + error << "Does not exist: " << path.string() << std::endl; + return 1; + } + + if (std::filesystem::is_directory(path)) + { + // hash the directory recursively + uint64_t hash = hash_directory_recursive(path.string()); + std::cout << hash << std::endl; + } + else + { + // hash the file + uint64_t hash = hash_file(path.string()); + std::cout << hash << std::endl; + } + return 0; +} + + +} // namespace dropshell