ClickHouse/programs/disks/CommandList.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

93 lines
2.9 KiB
C++
Raw Normal View History

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"
namespace DB
{
namespace ErrorCodes
{
2024-05-27 11:44:45 +00:00
extern const int BAD_ARGUMENTS;
}
class CommandList final : public ICommand
{
public:
2024-05-27 11:44:45 +00:00
explicit CommandList() : ICommand()
{
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);
}
2024-05-27 11:44:45 +00:00
void executeImpl(const CommandLineOptions & options, DisksClient & client) override
{
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", "");
if (recursive)
2024-05-27 11:44:45 +00:00
listRecursive(disk, disk.getAbsolutePath(path), show_hidden);
else
2024-05-27 11:44:45 +00:00
list(disk, path, show_hidden);
}
private:
2024-05-27 11:44:45 +00:00
static void list(const DiskWithPath & disk, const std::string & path, bool show_hidden)
{
2024-05-27 11:44:45 +00:00
std::vector<String> file_names = disk.listAllFilesByPath(path);
std::vector<String> selected_and_sorted_file_names{};
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";
}
}
2024-05-27 11:44:45 +00:00
static void listRecursive(const DiskWithPath & disk, const std::string & absolute_path, bool show_hidden)
{
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{};
2024-05-27 11:44:45 +00:00
std::cout << absolute_path << ":\n";
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);
}
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";
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);
}
}
};
2024-05-27 11:44:45 +00:00
CommandPtr makeCommandList()
{
2024-05-27 11:44:45 +00:00
return std::make_shared<DB::CommandList>();
}
}