Arkanjo 0.2
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
orchestrator_helper.hpp
Go to the documentation of this file.
1#pragma once
2
5
8
13
15static constexpr const char* DEFAULT_COMMAND = "help";
16
18 {"name", 'n', RequiredArgument, "Assign a name to the cache container; defaults to 'default' if not provided."},
19 {"color", 0, NoArgument, "Enable colored output."},
20 {"no-color", 0, NoArgument, "Disable colored output."},
21 {"json", 0, NoArgument, "Json output."},
22 {"preprocessor", 0, NoArgument, "Forces the preprocessor to execute."},
23 {"similarity", 'S', RequiredArgument, "Changes the similarity threshold to `SIMILARITY` for the current command only."},
24 {"help", 'h', NoArgument, "Show this help message."},
26};
27
29 std::unique_ptr<ICommand>& command,
30 const std::vector<std::pair<std::vector<std::string>, CommandsRegistry::CommandFactory>>& commands
31) {
32 return [&command, &commands](Context& ctx) mutable {
33 command = CommandsRegistry::get_command(ctx.command_name, commands);
34 if (command.get() != nullptr) {
35 return true;
36 }
37
38 std::string external_cmd = Config::config().program_name + "-" + ctx.command_name;
39 std::vector<char*> new_argv;
40 new_argv.push_back(const_cast<char*>(external_cmd.c_str()));
41 for (int i = 2; i < ctx.argc; ++i) {
42 new_argv.push_back(ctx.argv[i]);
43 }
44 new_argv.push_back(nullptr);
45 UtilsOSDependable::run_process(external_cmd.c_str(), new_argv.data());
46
47 throw CommandNotFoundError(ctx.command_name);
48
49 return true;
50 };
51}
52
53inline bool formatter_step(Context& ctx) {
54 bool json = ctx.options.args.count("json") > 0;
55 enum Format format_output = Format::TEXT;
56 if (json)
57 format_output = Format::JSON;
58 FormatterManager::set_format(format_output);
59 FormatterManager::set_formatter(std::make_shared<ConsoleFormatter>(true));
60 bool no_color = ctx.options.args.count("no-color") > 0;
61 if (no_color)
62 FormatterManager::set_formatter(std::make_shared<ConsoleFormatter>(false));
63 return true;
64}
65
67 return [&table](Context& ctx) {
68 table.load();
69 auto it = ctx.options.args.find("similarity");
70 if (it != ctx.options.args.end()) {
71 double sim = std::stod(ctx.options.args["similarity"]);
72 table.update_similarity(sim);
73 }
74 return true;
75 };
76}
77
78inline Step command_run_step(std::shared_ptr<ICommand> command, const OptionsCollector& collector) {
79 return [cmd = std::move(command), &collector](Context& ctx) mutable {
80 return cmd->validate(ctx.options) && cmd->do_run(ctx.command_name, ctx.options, &collector);
81 };
82}
83} // namespace OrchestratorHelper
Format
Definition base.hpp:9
An error is thrown when an unknown command is passed.
Definition cli_error.hpp:32
static Config & config()
Gets the singleton configuration instance.
Definition config.cpp:37
std::string program_name
Name of the program.
Definition config.hpp:41
static void set_formatter(std::shared_ptr< IFormatter > f)
static void set_format(Format f)
Represents a similarity graph between functions (paths).
void update_similarity(double new_similarity_threshold)
Updates similarity threshold.
static int run_process(const char *cmd, char *const argv[])
Executes an external program, replacing the current process (exec-style).
Function abstraction for temporary codebase.
nlohmann::json json
Definition function.hpp:22
std::unique_ptr< ICommand > get_command(const std::string &name, const std::vector< std::pair< std::vector< std::string >, CommandFactory > > &commands)
std::function< std::unique_ptr< ICommand >()> CommandFactory
Step setup_command_step(std::unique_ptr< ICommand > &command, const std::vector< std::pair< std::vector< std::string >, CommandsRegistry::CommandFactory > > &commands)
Step similarity_step(Similarity_Table &table)
constexpr CliOption global_long_opts[]
bool formatter_step(Context &ctx)
Step command_run_step(std::shared_ptr< ICommand > command, const OptionsCollector &collector)
Main command orchestration interface.
std::function< bool(Context &)> Step
#define OPTION_END
This marks the end of a long array of options.
@ RequiredArgument
@ NoArgument
Path abstraction for temporary codebase.
Similarity relationships storage and analysis.
ParsedOptions options
std::map< std::string, std::string > args
Map from option name to value.
Defines utility functions used across all files.