Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ target_link_libraries(clice-core PUBLIC
simdjson::simdjson
)

add_executable(clice "${PROJECT_SOURCE_DIR}/src/clice.cc")
file(GLOB CLICE_DRIVER_SOURCES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/src/driver/*.cc")
add_executable(clice "${PROJECT_SOURCE_DIR}/src/clice.cc" ${CLICE_DRIVER_SOURCES})
target_link_libraries(clice PRIVATE clice::core kota::deco)
add_dependencies(clice generate_version_header)
install(TARGETS clice RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
Expand Down
185 changes: 19 additions & 166 deletions src/clice.cc
Original file line number Diff line number Diff line change
@@ -1,195 +1,48 @@
#include <csignal>
#include <cstdint>
#include <print>
#include <sstream>
#include <string>

#include "version.h"
#include "server/transport/agentic.h"
#include "server/transport/master_server.h"
#include "server/worker/stateful_worker.h"
#include "server/worker/stateless_worker.h"
#include "support/logging.h"
#include "driver/driver.h"

#include "kota/deco/deco.h"

namespace clice {

namespace deco = kota::deco;
using deco::decl::KVStyle;

struct LintOptions {
DecoFlag(names = {"-h", "--help"}, help = "Show help", required = false)
help;
};

struct FormatOptions {
DecoFlag(names = {"-h", "--help"}, help = "Show help", required = false)
help;
};

struct WorkerOptions {
DecoFlag(names = {"-h", "--help"}, help = "Show help", required = false)
help;

DecoFlag(names = {"--stateful"},
help = "Run as stateful worker (default: stateless)",
required = false)
stateful;

DecoKV(style = KVStyle::JoinedOrSeparate,
names = {"--memory-limit", "--memory-limit="},
help = "Memory limit in bytes (stateful worker only)",
required = false)
<std::uint64_t> memory_limit;

DecoKV(style = KVStyle::JoinedOrSeparate,
names = {"--max-documents", "--max-documents="},
help = "Max compiled documents kept before LRU eviction (stateful worker only)",
required = false)
<std::uint64_t> max_documents;

DecoKV(style = KVStyle::JoinedOrSeparate,
names = {"--worker-name", "--worker-name="},
required = false)
<std::string> worker_name;

DecoKV(style = KVStyle::JoinedOrSeparate, names = {"--log-dir", "--log-dir="}, required = false)
<std::string> log_dir;
};

bool apply_log_level(const std::string& level_str) {
auto level = spdlog::level::from_str(level_str);
if(level == spdlog::level::off && level_str != "off") {
std::println(stderr,
"unknown log level '{}', valid: trace, debug, info, warn, error, off",
level_str);
return false;
}
logging::options.level = level;
return true;
}

template <typename T>
void print_usage(T& cmd) {
std::ostringstream ss;
cmd.usage(ss);
std::print("{}", ss.str());
}

} // namespace clice

int main(int argc, const char** argv) {
#ifndef _WIN32
signal(SIGPIPE, SIG_IGN);
#endif

namespace deco = kota::deco;
namespace driver = clice::driver;

auto args = deco::util::argvify(argc, argv);
const char* self_path = argv[0];

int exit_code = 1;

auto serve_cmd = deco::cli::command<clice::ServerOptions>("clice serve [OPTIONS]");
serve_cmd
.matchAll([&](clice::ServerOptions opts) {
if(opts.help) {
clice::print_usage(serve_cmd);
exit_code = 0;
return;
}
if(!clice::apply_log_level(opts.log_level.value_or("info")))
return;
exit_code = clice::run_serve_mode(opts, self_path);
})
.on_error([](auto err) { LOG_ERROR("{}", err.message); });

auto query_cmd = deco::cli::command<clice::QueryOptions>("clice query [OPTIONS]");
query_cmd
.matchAll([&](clice::QueryOptions opts) {
if(opts.help) {
clice::print_usage(query_cmd);
exit_code = 0;
return;
}
auto port = opts.port.value_or(0);
if(port <= 0 || port > 65535) {
LOG_ERROR("--port must be between 1 and 65535");
return;
}
if(!clice::apply_log_level(opts.log_level.value_or("info")))
return;
exit_code = clice::run_agentic_mode(opts);
})
.on_error([](auto err) { LOG_ERROR("{}", err.message); });

auto worker_cmd = deco::cli::command<clice::WorkerOptions>("clice worker [OPTIONS]");
worker_cmd
.matchAll([&](clice::WorkerOptions opts) {
if(opts.help) {
clice::print_usage(worker_cmd);
exit_code = 0;
return;
}
auto name = opts.worker_name.value_or("worker");
auto log_dir = opts.log_dir.value_or("");
if(opts.stateful) {
auto limit = opts.memory_limit.value_or(4ULL * 1024 * 1024 * 1024);
auto max_docs = opts.max_documents.value_or(clice::default_max_documents);
exit_code = clice::run_stateful_worker_mode(limit, name, log_dir, max_docs);
} else {
exit_code = clice::run_stateless_worker_mode(name, log_dir);
}
})
.on_error([](auto err) { LOG_ERROR("{}", err.message); });

auto lint_cmd = deco::cli::command<clice::LintOptions>("clice lint [OPTIONS]");
lint_cmd
.matchAll([&](clice::LintOptions opts) {
if(opts.help) {
clice::print_usage(lint_cmd);
exit_code = 0;
return;
}
LOG_ERROR("lint is not yet implemented");
})
.on_error([](auto err) { LOG_ERROR("{}", err.message); });

auto format_cmd = deco::cli::command<clice::FormatOptions>("clice format [OPTIONS]");
format_cmd
.matchAll([&](clice::FormatOptions opts) {
if(opts.help) {
clice::print_usage(format_cmd);
exit_code = 0;
return;
}
LOG_ERROR("format is not yet implemented");
})
.on_error([](auto err) { LOG_ERROR("{}", err.message); });

deco::cli::SubCommander clice("clice <command> [<args>]",
"A C++ development toolkit built on LLVM/Clang");

auto print_root_usage = [&] {
std::println("usage: clice <command> [<args>]\n");
clice::print_usage(clice);
driver::print_usage(clice);
};

clice.add({.name = "serve", .description = "Start LSP server"}, serve_cmd)
.add({.name = "query", .description = "Query symbol information from a running server"},
query_cmd)
.add({.name = "worker"}, worker_cmd)
.add({.name = "lint", .description = "Lint C++ source files"}, lint_cmd)
.add({.name = "format", .description = "Format C++ source files"}, format_cmd)
.when_err([&](auto err) {
if(err.type == deco::cli::SubCommandError::Type::MissingSubCommand) {
print_root_usage();
exit_code = 0;
} else {
LOG_ERROR("{}", err.message);
}
});
driver::add_serve(clice, exit_code, self_path);
driver::add_query(clice, exit_code);
driver::add_worker(clice, exit_code);
driver::add_index(clice, exit_code);
driver::add_doc(clice, exit_code);
driver::add_lint(clice, exit_code);
driver::add_format(clice, exit_code);

clice.when_err([&](auto err) {
if(err.type == deco::cli::SubCommandError::Type::MissingSubCommand) {
print_root_usage();
exit_code = 0;
} else {
LOG_ERROR("{}", err.message);
}
});

if(!args.empty() && (args[0] == "--version" || args[0] == "-v")) {
std::println("clice version {}", clice::version);
Expand Down
36 changes: 36 additions & 0 deletions src/driver/doc.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "driver/driver.h"

namespace clice::driver {

namespace {

struct DocOptions {
DecoFlag(names = {"-h", "--help"}, help = "Show help", required = false)
help;
};

auto make_command() {
return kota::deco::cli::command<DocOptions>("clice doc [OPTIONS]");
}

} // namespace

void add_doc(kota::deco::cli::SubCommander& root, int& exit_code) {
auto cmd = make_command();
cmd.matchAll([&exit_code](DocOptions opts) {
if(opts.help) {
auto help = make_command();
print_usage(help);
exit_code = 0;
return;
}
std::println(
stderr,
"clice doc is not implemented yet: it will generate documentation data for the workspace.");
})
.on_error([](auto err) { LOG_ERROR("{}", err.message); });

root.add({.name = "doc", .description = "Generate documentation data"}, std::move(cmd));
}

} // namespace clice::driver
47 changes: 47 additions & 0 deletions src/driver/driver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include <print>
#include <sstream>
#include <string>

#include "support/logging.h"

#include "kota/deco/deco.h"

namespace clice::driver {

/// Each subcommand lives in its own translation unit under src/driver/ and
/// registers itself with the root commander through one of these builders.
/// `exit_code` is written by the matched handler when the command runs;
/// clice.cc owns the commander and the final process exit.

void add_serve(kota::deco::cli::SubCommander& root, int& exit_code, const char* self_path);
void add_query(kota::deco::cli::SubCommander& root, int& exit_code);
void add_worker(kota::deco::cli::SubCommander& root, int& exit_code);
void add_index(kota::deco::cli::SubCommander& root, int& exit_code);
void add_doc(kota::deco::cli::SubCommander& root, int& exit_code);
void add_lint(kota::deco::cli::SubCommander& root, int& exit_code);
void add_format(kota::deco::cli::SubCommander& root, int& exit_code);

/// Set the global log level from a user-supplied string; complains and
/// returns false on an unknown level.
inline bool apply_log_level(const std::string& level_str) {
auto level = spdlog::level::from_str(level_str);
if(level == spdlog::level::off && level_str != "off") {
std::println(stderr,
"unknown log level '{}', valid: trace, debug, info, warn, error, off",
level_str);
Comment thread
16bit-ykiko marked this conversation as resolved.
Resolved
return false;
}
logging::options.level = level;
return true;
}

template <typename Command>
void print_usage(Command& cmd) {
std::ostringstream ss;
cmd.usage(ss);
std::print("{}", ss.str());
}

} // namespace clice::driver
35 changes: 35 additions & 0 deletions src/driver/format.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "driver/driver.h"

namespace clice::driver {

namespace {

struct FormatOptions {
DecoFlag(names = {"-h", "--help"}, help = "Show help", required = false)
help;
};

auto make_command() {
return kota::deco::cli::command<FormatOptions>("clice format [OPTIONS]");
}

} // namespace

void add_format(kota::deco::cli::SubCommander& root, int& exit_code) {
auto cmd = make_command();
cmd.matchAll([&exit_code](FormatOptions opts) {
if(opts.help) {
auto help = make_command();
print_usage(help);
exit_code = 0;
return;
}
std::println(stderr,
"clice format is not implemented yet: it will format C++ source files.");
})
.on_error([](auto err) { LOG_ERROR("{}", err.message); });

root.add({.name = "format", .description = "Format C++ source files"}, std::move(cmd));
}

} // namespace clice::driver
36 changes: 36 additions & 0 deletions src/driver/index.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "driver/driver.h"

namespace clice::driver {

namespace {

struct IndexOptions {
DecoFlag(names = {"-h", "--help"}, help = "Show help", required = false)
help;
};

auto make_command() {
return kota::deco::cli::command<IndexOptions>("clice index [OPTIONS]");
}

} // namespace

void add_index(kota::deco::cli::SubCommander& root, int& exit_code) {
auto cmd = make_command();
cmd.matchAll([&exit_code](IndexOptions opts) {
if(opts.help) {
auto help = make_command();
print_usage(help);
exit_code = 0;
return;
}
std::println(
stderr,
"clice index is not implemented yet: it will build the workspace symbol index ahead of time.");
})
.on_error([](auto err) { LOG_ERROR("{}", err.message); });

root.add({.name = "index", .description = "Index a workspace ahead of time"}, std::move(cmd));
}

} // namespace clice::driver
Loading