Skip to content

Commit e989e1b

Browse files
committed
feat: support system-wide runtime configuration
Add system configuration directory at /etc/linglong. Configs are now merged merged in order: system global, system app-specific, user global, then user global and user app-specific, allowing user configs to override system defaults. Signed-off-by: reddevillg <reddevillg@gmail.com>
1 parent 9f8c901 commit e989e1b

3 files changed

Lines changed: 58 additions & 7 deletions

File tree

libs/common/src/linglong/common/dir.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ std::filesystem::path getUserRuntimeConfigDir() noexcept
5353
return configDir / "linglong";
5454
}
5555

56+
std::filesystem::path getSystemRuntimeConfigDir() noexcept
57+
{
58+
return "/etc/linglong";
59+
}
60+
5661
} // namespace linglong::common::dir

libs/common/src/linglong/common/dir.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ std::filesystem::path getUserCacheDir() noexcept;
2929

3030
std::filesystem::path getUserRuntimeConfigDir() noexcept;
3131

32+
std::filesystem::path getSystemRuntimeConfigDir() noexcept;
33+
3234
} // namespace linglong::common::dir

libs/utils/src/linglong/utils/runtime_config.cpp

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,35 @@ loadUserRuntimeConfig(const std::string &appId)
4545
return LINGLONG_ERR(result);
4646
}
4747

48-
return std::move(result).value();
48+
return result;
49+
}
50+
51+
linglong::utils::error::Result<std::optional<RuntimeConfigure>>
52+
loadSystemRuntimeConfig(const std::string &appId)
53+
{
54+
LINGLONG_TRACE(
55+
fmt::format("load system runtime config for {}", appId.empty() ? "global" : appId));
56+
57+
auto configDir = linglong::common::dir::getSystemRuntimeConfigDir();
58+
59+
std::filesystem::path configPath;
60+
if (appId.empty()) {
61+
configPath = configDir / "config.json";
62+
} else {
63+
configPath = configDir / "apps" / appId / "config.json";
64+
}
65+
66+
std::error_code ec;
67+
if (!std::filesystem::exists(configPath, ec)) {
68+
return std::nullopt;
69+
}
70+
71+
auto result = linglong::utils::loadRuntimeConfig(configPath);
72+
if (!result) {
73+
return LINGLONG_ERR(result);
74+
}
75+
76+
return result;
4977
}
5078

5179
} // namespace
@@ -112,30 +140,46 @@ loadRuntimeConfig(const std::filesystem::path &path)
112140
return LINGLONG_ERR("failed to load runtime config", result);
113141
}
114142

115-
return std::move(result).value();
143+
return result;
116144
}
117145

118146
utils::error::Result<std::optional<RuntimeConfigure>> loadRuntimeConfig(const std::string &appId)
119147
{
120148
LINGLONG_TRACE("load runtime config for app: " + appId);
121149

122-
// Merge configs in order: user global -> user app
150+
// Merge configs in order: system global -> system app -> user global -> user app
123151
std::vector<RuntimeConfigure> configs;
124152

153+
auto systemGlobal = loadSystemRuntimeConfig("");
154+
if (!systemGlobal) {
155+
return LINGLONG_ERR(systemGlobal);
156+
}
157+
if (systemGlobal->has_value()) {
158+
configs.emplace_back(systemGlobal->value());
159+
}
160+
161+
auto systemApp = loadSystemRuntimeConfig(appId);
162+
if (!systemApp) {
163+
return LINGLONG_ERR(systemApp);
164+
}
165+
if (systemApp->has_value()) {
166+
configs.emplace_back(systemApp->value());
167+
}
168+
125169
auto userGlobal = loadUserRuntimeConfig("");
126170
if (!userGlobal) {
127171
return LINGLONG_ERR(userGlobal);
128172
}
129-
if ((*userGlobal).has_value()) {
130-
configs.emplace_back((*userGlobal).value());
173+
if (userGlobal->has_value()) {
174+
configs.emplace_back(userGlobal->value());
131175
}
132176

133177
auto userApp = loadUserRuntimeConfig(appId);
134178
if (!userApp) {
135179
return LINGLONG_ERR(userApp);
136180
}
137-
if ((*userApp).has_value()) {
138-
configs.emplace_back((*userApp).value());
181+
if (userApp->has_value()) {
182+
configs.emplace_back(userApp->value());
139183
}
140184

141185
if (configs.empty()) {

0 commit comments

Comments
 (0)