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
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2024-05-27 11:44:45 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2022-04-08 07:52:16 +00:00
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
auto disk = client.getCurrentDiskWithPath();
|
|
|
|
String path = getValueFromCommandLineOptionsWithDefault<String>(options, "path", "");
|
2022-06-23 18:34:59 +00:00
|
|
|
|
|
|
|
if (recursive)
|
2024-05-27 11:44:45 +00:00
|
|
|
listRecursive(disk, disk.getAbsolutePath(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-27 11:44:45 +00:00
|
|
|
static void listRecursive(const DiskWithPath & disk, const std::string & absolute_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(absolute_path);
|
|
|
|
std::vector<String> selected_and_sorted_file_names{};
|
2022-04-08 07:52:16 +00:00
|
|
|
|
2024-05-27 11:44:45 +00:00
|
|
|
std::cout << absolute_path << ":\n";
|
2022-12-08 17:20:54 +00:00
|
|
|
|
|
|
|
if (!file_names.empty())
|
|
|
|
{
|
|
|
|
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);
|
2022-12-08 17:20:54 +00:00
|
|
|
}
|
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";
|
|
|
|
|
2022-06-23 18:34:59 +00:00
|
|
|
for (const auto & file_name : file_names)
|
|
|
|
{
|
2024-05-27 11:44:45 +00:00
|
|
|
auto path = absolute_path + "/" + file_name;
|
|
|
|
if (disk.isDirectory(path))
|
|
|
|
if (show_hidden || (!file_name.starts_with('.')))
|
|
|
|
listRecursive(disk, path, show_hidden);
|
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
|
|
|
}
|