Skip to content

Commit 2717b5e

Browse files
committed
cli config json output
1 parent a35ca9b commit 2717b5e

File tree

3 files changed

+93
-52
lines changed

3 files changed

+93
-52
lines changed

WORKSPACE.bazel

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ workspace(name = "ecsact_sdk")
22

33
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44

5+
_NLOHMANN_JSON_BUILD_FILE = """
6+
load("@rules_cc//cc:defs.bzl", "cc_library")
7+
8+
cc_library(
9+
name = "json",
10+
visibility = ["//visibility:public"],
11+
includes = ["include"],
12+
hdrs = glob(["include/**/*.hpp"]),
13+
strip_include_prefix = "include",
14+
)
15+
"""
16+
17+
http_archive(
18+
name = "nlohmann_json",
19+
build_file_content = _NLOHMANN_JSON_BUILD_FILE,
20+
sha256 = "b94997df68856753b72f0d7a3703b7d484d4745c567f3584ef97c96c25a5798e",
21+
url = "https://github.com/nlohmann/json/releases/download/v3.10.5/include.zip",
22+
)
23+
524
http_archive(
625
name = "bazelregistry_docopt_cpp",
726
sha256 = "a06e705978b1c09427f130a187cb361916c1e7d66b69e91b865ebcd5390a6774",

cli/commands/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ cc_library(
3636
":command",
3737
"//executable_path",
3838
"@bazelregistry_docopt_cpp//:docopt",
39+
"@nlohmann_json//:json",
3940
],
4041
)

cli/commands/config.cc

Lines changed: 73 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <map>
66
#include <string>
77
#include "docopt.h"
8+
#include "nlohmann/json.hpp"
89

910
#include "executable_path/executable_path.hh"
1011

@@ -13,7 +14,7 @@ namespace fs = std::filesystem;
1314
constexpr auto USAGE = R"(Ecsact Config Command
1415
1516
Usage:
16-
ecsact config [--include_dir] [--plugin_dir] [--builtin_plugins]
17+
ecsact config [<keys>...]
1718
1819
Options:
1920
--include_dir
@@ -56,68 +57,88 @@ int ecsact::cli::detail::config_command(int argc, char* argv[]) {
5657

5758
auto install_prefix = exec_path.parent_path().parent_path();
5859
auto plugin_dir = install_prefix / "share" / "ecsact" / "plugins";
59-
std::map<std::string, std::string> output;
60-
61-
if(args.at("--include_dir").asBool()) {
62-
auto includedir = install_prefix / "include";
63-
auto core_hdr = includedir / "ecsact" / "runtime" / "core.h";
64-
65-
if(fs::exists(core_hdr)) {
66-
output["include_dir"] = includedir.string();
67-
} else {
68-
std::cerr << CANNOT_FIND_INCLUDE_DIR;
69-
return 1;
70-
}
71-
}
72-
73-
if(args.at("--plugin_dir").asBool()) {
74-
if(fs::exists(plugin_dir)) {
75-
output["plugin_dir"] = plugin_dir.string();
76-
} else {
77-
std::cerr << CANNOT_FIND_PLUGIN_DIR;
78-
return 1;
79-
}
80-
}
60+
nlohmann::json output = "{}"_json;
61+
62+
std::unordered_map<std::string, std::function<int()>> key_handlers{
63+
{"include_dir", [&] {
64+
auto includedir = install_prefix / "include";
65+
auto core_hdr = includedir / "ecsact" / "runtime" / "core.h";
66+
67+
if(fs::exists(core_hdr)) {
68+
output["include_dir"] = includedir.string();
69+
} else {
70+
std::cerr << CANNOT_FIND_INCLUDE_DIR;
71+
return 1;
72+
}
8173

82-
if(args.at("--builtin_plugins").asBool()) {
83-
if(fs::exists(plugin_dir)) {
84-
auto& builtin_plugins_str = output["builtin_plugins"];
85-
std::vector<std::string> builtin_plugins;
86-
for(auto& entry : fs::directory_iterator(plugin_dir)) {
87-
auto filename = entry.path().filename().replace_extension("").string();
88-
const auto prefix = "ecsact_"s;
89-
const auto suffix = "_codegen"s;
90-
if(filename.starts_with(prefix) && filename.ends_with(suffix)) {
91-
builtin_plugins.emplace_back() = filename.substr(
92-
prefix.size(),
93-
filename.size() - prefix.size() - suffix.size()
94-
);
95-
}
74+
return 0;
75+
}},
76+
{"plugin_dir", [&] {
77+
if(fs::exists(plugin_dir)) {
78+
output["plugin_dir"] = plugin_dir.string();
79+
} else {
80+
std::cerr << CANNOT_FIND_PLUGIN_DIR;
81+
return 1;
9682
}
9783

98-
if(!builtin_plugins.empty()) {
84+
return 0;
85+
}},
86+
{"builtin_plugins", [&] {
87+
if(fs::exists(plugin_dir)) {
9988
auto& builtin_plugins_str = output["builtin_plugins"];
100-
for(auto i=0; builtin_plugins.size() - 1 > i; ++i) {
101-
builtin_plugins_str += builtin_plugins[i] + "\n";
89+
std::vector<std::string> builtin_plugins;
90+
for(auto& entry : fs::directory_iterator(plugin_dir)) {
91+
auto filename = entry.path().filename().replace_extension("").string();
92+
const auto prefix = "ecsact_"s;
93+
const auto suffix = "_codegen"s;
94+
if(filename.starts_with(prefix) && filename.ends_with(suffix)) {
95+
builtin_plugins.emplace_back() = filename.substr(
96+
prefix.size(),
97+
filename.size() - prefix.size() - suffix.size()
98+
);
99+
}
102100
}
103-
builtin_plugins_str += builtin_plugins.back();
101+
102+
output["builtin_plugins"] = builtin_plugins;
103+
} else {
104+
std::cerr << CANNOT_FIND_PLUGIN_DIR;
105+
return 1;
104106
}
105-
} else {
106-
std::cerr << CANNOT_FIND_PLUGIN_DIR;
107-
return 1;
108-
}
109-
}
110107

111-
if(output.empty()) {
112-
return 1;
108+
return 0;
109+
}},
110+
};
111+
112+
auto keys = args.at("<keys>").asStringList();
113+
if(keys.empty()) {
114+
for(auto&& [_, key_handler] : key_handlers) {
115+
int exit_code = key_handler();
116+
if(exit_code != 0) {
117+
return exit_code;
118+
}
119+
}
120+
} else {
121+
for(auto key : keys) {
122+
int exit_code = key_handlers.at(key)();
123+
if(exit_code != 0) {
124+
return exit_code;
125+
}
126+
}
113127
}
114128

115129
if(output.size() == 1) {
116-
std::cout << output.begin()->second << "\n";
117-
} else {
118-
for(auto&& [key, value] : output) {
119-
std::cout << key << ": " << value << "\n";
130+
auto& value = output.begin().value();
131+
if(value.is_array()) {
132+
for(auto& element : value) {
133+
std::cout << element.get<std::string>() << "\n";
134+
}
135+
} else if(value.is_string()) {
136+
std::cout << value.get<std::string>();
137+
} else {
138+
std::cout << value;
120139
}
140+
} else {
141+
std::cout << output.dump();
121142
}
122143

123144
return 0;

0 commit comments

Comments
 (0)