@@ -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
118146utils::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