2022-07-20 20:30:16 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2022-12-08 17:53:05 +00:00
|
|
|
#include <Common/TerminalSize.h>
|
2024-05-27 11:44:45 +00:00
|
|
|
#include "DisksApp.h"
|
|
|
|
#include "DisksClient.h"
|
|
|
|
#include "ICommand.h"
|
2022-04-08 07:52:16 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-12-08 17:20:54 +00:00
|
|
|
class CommandList final : public ICommand
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
|
|
|
public:
|
2024-05-27 11:44:45 +00:00
|
|
|
explicit CommandList() : ICommand()
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
|
|
|
command_name = "list";
|
2023-08-29 12:25:04 +00:00
|
|
|
description = "List files at path[s]";
|
2024-05-27 11:44:45 +00:00
|
|
|
options_description.add_options()("recursive", "recursively list the directory")("all", "show hidden files")(
|
|
|
|
"path", po::value<String>(), "the path of listing (mandatory, positional)");
|
|
|
|
positional_options_description.add("path", 1);
|
2022-06-23 18:34:59 +00:00
|
|
|
}
|
2022-04-08 07:52:16 +00:00
|
|
|
|
2024-05-27 11:44:45 +00:00
|
|
|
void executeImpl(const CommandLineOptions & options, DisksClient & client) override
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
2024-05-27 11:44:45 +00:00
|
|
|
bool recursive = options.count("recursive");
|
|
|
|
bool show_hidden = options.count("all");
|
2024-10-15 16:24:06 +00:00
|
|
|
const auto & disk = client.getCurrentDiskWithPath();
|
2024-05-30 14:07:25 +00:00
|
|
|
String path = getValueFromCommandLineOptionsWithDefault<String>(options, "path", ".");
|
2022-06-23 18:34:59 +00:00
|
|
|
|
|
|
|
if (recursive)
|
2024-05-30 14:07:25 +00:00
|
|
|
listRecursive(disk, path, show_hidden);
|
2022-06-23 18:34:59 +00:00
|
|
|
else
|
2024-05-27 11:44:45 +00:00
|
|
|
list(disk, path, show_hidden);
|
2022-06-23 18:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2024-05-27 11:44:45 +00:00
|
|
|
static void list(const DiskWithPath & disk, const std::string & path, bool show_hidden)
|
2022-06-23 18:34:59 +00:00
|
|
|
{
|
2024-05-27 11:44:45 +00:00
|
|
|
std::vector<String> file_names = disk.listAllFilesByPath(path);
|
|
|
|
std::vector<String> selected_and_sorted_file_names{};
|
2022-06-23 18:34:59 +00:00
|
|
|
|
|
|
|
for (const auto & file_name : file_names)
|
2024-05-27 11:44:45 +00:00
|
|
|
if (show_hidden || (!file_name.starts_with('.')))
|
|
|
|
selected_and_sorted_file_names.push_back(file_name);
|
|
|
|
|
|
|
|
std::sort(selected_and_sorted_file_names.begin(), selected_and_sorted_file_names.end());
|
|
|
|
for (const auto & file_name : selected_and_sorted_file_names)
|
|
|
|
{
|
|
|
|
std::cout << file_name << "\n";
|
|
|
|
}
|
2022-06-23 18:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-05-30 14:07:25 +00:00
|
|
|
static void listRecursive(const DiskWithPath & disk, const std::string & relative_path, bool show_hidden)
|
2022-06-23 18:34:59 +00:00
|
|
|
{
|
2024-05-30 14:07:25 +00:00
|
|
|
std::vector<String> file_names = disk.listAllFilesByPath(relative_path);
|
2024-05-27 11:44:45 +00:00
|
|
|
std::vector<String> selected_and_sorted_file_names{};
|
2022-04-08 07:52:16 +00:00
|
|
|
|
2024-05-30 14:07:25 +00:00
|
|
|
std::cout << relative_path << ":\n";
|
2022-12-08 17:20:54 +00:00
|
|
|
|
2024-06-17 17:40:52 +00:00
|
|
|
for (const auto & file_name : file_names)
|
|
|
|
if (show_hidden || (!file_name.starts_with('.')))
|
|
|
|
selected_and_sorted_file_names.push_back(file_name);
|
2022-06-23 18:34:59 +00:00
|
|
|
|
2024-05-27 11:44:45 +00:00
|
|
|
std::sort(selected_and_sorted_file_names.begin(), selected_and_sorted_file_names.end());
|
|
|
|
for (const auto & file_name : selected_and_sorted_file_names)
|
|
|
|
{
|
|
|
|
std::cout << file_name << "\n";
|
|
|
|
}
|
|
|
|
std::cout << "\n";
|
|
|
|
|
2024-05-27 11:49:59 +00:00
|
|
|
for (const auto & file_name : selected_and_sorted_file_names)
|
2022-06-23 18:34:59 +00:00
|
|
|
{
|
2024-05-30 14:07:25 +00:00
|
|
|
auto path = [&]() -> String
|
|
|
|
{
|
|
|
|
if (relative_path.ends_with("/"))
|
|
|
|
return relative_path + file_name;
|
2024-09-19 11:51:02 +00:00
|
|
|
|
|
|
|
return relative_path + "/" + file_name;
|
2024-05-30 14:07:25 +00:00
|
|
|
}();
|
2024-05-27 11:44:45 +00:00
|
|
|
if (disk.isDirectory(path))
|
2024-06-17 17:40:52 +00:00
|
|
|
{
|
2024-05-27 11:49:59 +00:00
|
|
|
listRecursive(disk, path, show_hidden);
|
2024-06-17 17:40:52 +00:00
|
|
|
}
|
2022-06-23 18:34:59 +00:00
|
|
|
}
|
2022-04-08 07:52:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-27 11:44:45 +00:00
|
|
|
CommandPtr makeCommandList()
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
2024-05-27 11:44:45 +00:00
|
|
|
return std::make_shared<DB::CommandList>();
|
|
|
|
}
|
2022-04-08 07:52:16 +00:00
|
|
|
}
|