tool: use C++ namespaces

This commit is contained in:
Julie B. 2022-08-16 23:49:12 +02:00
parent dd51122fea
commit edace7d30c
7 changed files with 33 additions and 10 deletions

View File

@ -24,7 +24,7 @@
namespace fs = std::filesystem;
using namespace nix;
Context ContextBuilder::build() {
miniguest::Context miniguest::ContextBuilder::build() {
if (!symlink_path)
fs::create_directory(default_symlinks_dir);
if (!profile_path)
@ -33,19 +33,19 @@ Context ContextBuilder::build() {
profile_path.value_or(default_profiles_dir / guest_name)};
}
void Context::ensure_symlink() {
void miniguest::Context::ensure_symlink() {
auto st = fs::symlink_status(symlink_path);
if (!fs::exists(st))
fs::create_symlink(profile_path, symlink_path);
else
check_symlink(st);
}
void Context::check_symlink(const fs::file_status &st) {
void miniguest::Context::check_symlink(const fs::file_status &st) {
if (!fs::is_symlink(st) || fs::read_symlink(symlink_path) != profile_path)
throw Error(1,
"not touching symlink because it's not in an expected state");
}
void Context::remove_symlink() {
void miniguest::Context::remove_symlink() {
auto st = fs::symlink_status(symlink_path);
if (fs::exists(st))
check_symlink(st);
@ -53,7 +53,7 @@ void Context::remove_symlink() {
fs::remove(symlink_path);
}
void completeGuestName(size_t, std::string_view prefix) {
void miniguest::completeGuestName(size_t, std::string_view prefix) {
// FIXME: constant duplication
fs::path dir = "/etc/miniguests";

View File

@ -19,6 +19,8 @@
#include <filesystem>
#include <optional>
namespace miniguest {
struct ContextBuilder final {
std::string guest_name;
std::optional<std::filesystem::path> symlink_path, profile_path;
@ -45,3 +47,5 @@ private:
};
void completeGuestName(size_t, std::string_view prefix);
} // namespace miniguest

View File

@ -30,6 +30,8 @@
using namespace nix;
namespace fs = std::filesystem;
namespace miniguest {
struct CmdCreate : virtual EvalCommand, virtual MixProfile {
std::string guest_name;
std::optional<std::string> hypervisor;
@ -135,3 +137,5 @@ const std::map<std::string, CmdCreate::HypervisorData> CmdCreate::hypervisors{
};
static auto rCmdCreate = registerCommand<CmdCreate>("create");
} // namespace miniguest

View File

@ -30,6 +30,8 @@
using namespace nix;
namespace fs = std::filesystem;
namespace miniguest {
struct CmdInstall : virtual InstallableCommand, virtual MixProfile {
std::optional<std::string> guest_name;
@ -97,3 +99,5 @@ struct CmdInstall : virtual InstallableCommand, virtual MixProfile {
};
static auto rCmdInstall = registerCommand<CmdInstall>("install");
} // namespace miniguest

View File

@ -24,10 +24,10 @@ using namespace nix;
struct HelpRequested {};
struct MiniguestArgs final : virtual MultiCommand, virtual MixCommonArgs {
bool helpRequested = false;
namespace miniguest {
MiniguestArgs()
struct TopLevelArgs final : virtual MultiCommand, virtual MixCommonArgs {
TopLevelArgs()
: MultiCommand(RegisterCommand::getCommandsFor({})),
MixCommonArgs("miniguest") {
addFlag({
@ -57,7 +57,7 @@ struct MiniguestArgs final : virtual MultiCommand, virtual MixCommonArgs {
void main0(int argc, char **argv) {
initNix();
initGC();
MiniguestArgs args;
TopLevelArgs args;
settings.experimentalFeatures = {Xp::Flakes};
@ -96,6 +96,9 @@ void main0(int argc, char **argv) {
args.command->second->run();
}
} // namespace miniguest
int main(int argc, char **argv) {
return nix::handleExceptions(argv[0], [=]() { main0(argc, argv); });
return nix::handleExceptions(argv[0],
[=]() { miniguest::main0(argc, argv); });
}

View File

@ -30,6 +30,8 @@
using namespace nix;
namespace fs = std::filesystem;
namespace miniguest {
struct CmdRemove : virtual EvalCommand, virtual MixProfile {
std::string guest_name;
@ -73,3 +75,5 @@ struct CmdRemove : virtual EvalCommand, virtual MixProfile {
};
static auto rCmdRemove = registerCommand<CmdRemove>("remove");
} // namespace miniguest

View File

@ -30,6 +30,8 @@
using namespace nix;
namespace fs = std::filesystem;
namespace miniguest {
struct CmdUpgrade : virtual EvalCommand, virtual MixProfile {
std::string guest_name;
@ -88,3 +90,5 @@ struct CmdUpgrade : virtual EvalCommand, virtual MixProfile {
};
static auto rCmdUpgrade = registerCommand<CmdUpgrade>("upgrade");
} // namespace miniguest