* Copyright (c) 2025-2026 IB-Robot Group & openEuler Embedded SIG & openharmony-robot sig_RoboFrame.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "list_command.h"
#include <iostream>
#include "discovery_options.h"
#include "discovery_query.h"
namespace ibmw::cli::service {
namespace {
void PrintTypes(const std::set<std::string>& types)
{
std::cout << " [";
bool first = true;
for (const auto& type : types) {
if (!first) {
std::cout << ", ";
}
first = false;
std::cout << type;
}
std::cout << ']';
}
int RunList(const Invocation& invocation)
{
const auto snapshot = DiscoverServices(invocation);
const auto services = VisibleServices(snapshot, invocation.HasOption("--include-hidden-services"));
if (invocation.HasOption("--count-services")) {
std::cout << services.size() << '\n';
} else {
for (const auto& [name, types] : services) {
std::cout << name;
if (invocation.HasOption("--show-types")) {
PrintTypes(types);
}
std::cout << '\n';
}
}
return snapshot.Succeeded() ? 0 : 1;
}
}
void AddListCommand(Command& service)
{
auto& list = service.AddSubcommand("list", "List discovered services");
list.SetHandler(RunList);
list.AddOption("--show-types", "-t", {}, "Additionally show service types");
list.AddOption("--count-services", "-c", {}, "Only display the number of service names");
AddDiscoveryOptions(list);
}
}