aboutsummaryrefslogtreecommitdiff
path: root/src/zen/cmds/admin_cmd.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2026-04-23 18:16:57 +0200
committerStefan Boberg <[email protected]>2026-04-23 18:16:57 +0200
commit0232b991cd7d8e3a2114ea30e4591dd3e7b65c36 (patch)
tree94730e7594fd09ae1fa820391ce311f6daf13905 /src/zen/cmds/admin_cmd.cpp
parentFix forward declaration order for s_GotSigWinch and SigWinchHandler (diff)
parenttrace: declare Region event name fields as AnsiString (#1012) (diff)
downloadarchived-zen-sb/zen-help.tar.xz
archived-zen-sb/zen-help.zip
Merge branch 'main' into sb/zen-helpsb/zen-help
- Combine HelpCommand (this branch) with HistoryCommand (main) in zen CLI dispatcher - Keep filter-aware TuiPickOne rewrite; adopt main's ASCII arrow glyphs in doc comment
Diffstat (limited to 'src/zen/cmds/admin_cmd.cpp')
-rw-r--r--src/zen/cmds/admin_cmd.cpp86
1 files changed, 26 insertions, 60 deletions
diff --git a/src/zen/cmds/admin_cmd.cpp b/src/zen/cmds/admin_cmd.cpp
index 034d430fd..2580517fa 100644
--- a/src/zen/cmds/admin_cmd.cpp
+++ b/src/zen/cmds/admin_cmd.cpp
@@ -2,6 +2,8 @@
#include "admin_cmd.h"
+#include "zenserviceclient.h"
+
#include <zencore/basicfile.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
@@ -41,14 +43,8 @@ ScrubCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
return;
}
- m_HostName = ResolveTargetHostSpec(m_HostName);
-
- if (m_HostName.empty())
- {
- throw OptionParseException("Unable to resolve server specification", m_Options.help());
- }
-
- HttpClient Http = CreateHttpClient(m_HostName);
+ ZenServiceClient Service({.HostSpec = m_HostName, .CommandName = Name});
+ HttpClient& Http = Service.Http();
HttpClient::KeyValueMap Params{{"skipdelete", ToString(m_DryRun)},
{"skipgc", ToString(m_NoGc)},
@@ -168,12 +164,7 @@ GcCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
return;
}
- m_HostName = ResolveTargetHostSpec(m_HostName);
-
- if (m_HostName.empty())
- {
- throw OptionParseException("Unable to resolve server specification", m_Options.help());
- }
+ ZenServiceClient Service({.HostSpec = m_HostName, .CommandName = Name});
HttpClient::KeyValueMap Params;
Params.Entries.insert({"smallobjects", m_SmallObjects ? "true" : "false"});
@@ -258,7 +249,7 @@ GcCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
}
Params.Entries.insert({"enablevalidation", m_EnableValidation ? "true" : "false"});
- HttpClient Http = CreateHttpClient(m_HostName);
+ HttpClient& Http = Service.Http();
if (HttpClient::Response Response = Http.Post("/admin/gc"sv, HttpClient::Accept(HttpContentType::kJSON), Params))
{
ZEN_CONSOLE("OK: {}", Response.ToText());
@@ -290,14 +281,8 @@ GcStatusCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
return;
}
- m_HostName = ResolveTargetHostSpec(m_HostName);
-
- if (m_HostName.empty())
- {
- throw OptionParseException("Unable to resolve server specification", m_Options.help());
- }
-
- HttpClient Http = CreateHttpClient(m_HostName);
+ ZenServiceClient Service({.HostSpec = m_HostName, .CommandName = Name});
+ HttpClient& Http = Service.Http();
if (HttpClient::Response Response = Http.Get("/admin/gc"sv, HttpClient::Accept(HttpContentType::kJSON)))
{
ZEN_CONSOLE("OK: {}", Response.ToText());
@@ -328,14 +313,8 @@ GcStopCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
return;
}
- m_HostName = ResolveTargetHostSpec(m_HostName);
-
- if (m_HostName.empty())
- {
- throw OptionParseException("Unable to resolve server specification", m_Options.help());
- }
-
- HttpClient Http = CreateHttpClient(m_HostName);
+ ZenServiceClient Service({.HostSpec = m_HostName, .CommandName = Name});
+ HttpClient& Http = Service.Http();
if (HttpClient::Response Response = Http.Post("/admin/gc-stop"sv, HttpClient::Accept(HttpContentType::kJSON)))
{
if (Response.StatusCode == HttpResponseCode::Accepted)
@@ -377,14 +356,8 @@ JobCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
return;
}
- m_HostName = ResolveTargetHostSpec(m_HostName);
-
- if (m_HostName.empty())
- {
- throw OptionParseException("Unable to resolve server specification", m_Options.help());
- }
-
- HttpClient Http = CreateHttpClient(m_HostName);
+ ZenServiceClient Service({.HostSpec = m_HostName, .CommandName = Name});
+ HttpClient& Http = Service.Http();
if (m_Cancel)
{
@@ -460,14 +433,8 @@ LoggingCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
return;
}
- m_HostName = ResolveTargetHostSpec(m_HostName);
-
- if (m_HostName.empty())
- {
- throw OptionParseException("Unable to resolve server specification", m_Options.help());
- }
-
- HttpClient Http = CreateHttpClient(m_HostName);
+ ZenServiceClient Service({.HostSpec = m_HostName, .CommandName = Name});
+ HttpClient& Http = Service.Http();
HttpClient::KeyValueMap Parameters;
@@ -522,9 +489,10 @@ LoggingCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
throw std::runtime_error(fmt::format("Failed to retrieve {} log path", SourceName));
}
- if (!CopyFile(SourcePath, TargetPath, {}))
+ if (std::error_code Ec = CopyFile(SourcePath, TargetPath, {}); Ec)
{
- throw std::runtime_error(
+ throw std::system_error(
+ Ec,
fmt::format("Failed to copy {} log file {} to output file '{}'", SourceName, SourcePath, TargetPath));
}
};
@@ -579,16 +547,10 @@ FlushCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
return;
}
- m_HostName = ResolveTargetHostSpec(m_HostName);
+ ZenServiceClient Service({.HostSpec = m_HostName, .CommandName = Name});
+ HttpClient& Http = Service.Http();
- if (m_HostName.empty())
- {
- throw OptionParseException("Unable to resolve server specification", m_Options.help());
- }
-
- zen::HttpClient Http = CreateHttpClient(m_HostName);
-
- if (zen::HttpClient::Response Response = Http.Post("/admin/flush"sv))
+ if (HttpClient::Response Response = Http.Post("/admin/flush"sv))
{
ZEN_CONSOLE("OK: {}", Response.ToText());
@@ -624,7 +586,10 @@ Copy(const std::filesystem::path& Source, const std::filesystem::path& Target)
CreateDirectories(Target.parent_path());
CopyFileOptions Options;
- CopyFile(Source, Target, Options);
+ if (std::error_code Ec = CopyFile(Source, Target, Options); Ec)
+ {
+ throw std::system_error(Ec, fmt::format("Failed to copy '{}' to '{}'", Source, Target));
+ }
}
static bool
@@ -638,7 +603,8 @@ TryCopy(const std::filesystem::path& Source, const std::filesystem::path& Target
CreateDirectories(Target.parent_path());
CopyFileOptions Options;
- return CopyFile(Source, Target, Options);
+ std::error_code Ec = CopyFile(Source, Target, Options);
+ return !Ec;
}
void