This commit is contained in:
83
source/src/commands/hash.cpp
Normal file
83
source/src/commands/hash.cpp
Normal file
@ -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 <unistd.h>
|
||||||
|
#include <cstring>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <libassert/assert.hpp>
|
||||||
|
|
||||||
|
namespace dropshell {
|
||||||
|
|
||||||
|
void hash_autocomplete(const CommandContext& ctx);
|
||||||
|
int hash_handler(const CommandContext& ctx);
|
||||||
|
|
||||||
|
static std::vector<std::string> 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
|
Reference in New Issue
Block a user