Skip to content

Commit 4793538

Browse files
committed
simplify
1 parent 680639a commit 4793538

File tree

2 files changed

+24
-37
lines changed

2 files changed

+24
-37
lines changed

options/locale/locale_en-US.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3425,7 +3425,6 @@ config.picture_service = Picture Service
34253425
config.disable_gravatar = Disable Gravatar
34263426
config.enable_federated_avatar = Enable Federated Avatars
34273427
config.open_with_editor_app_help = The "Open with" editors for the clone menu. If left empty, the default will be used. Expand to see the default.
3428-
34293428
config.git_guide_remote_name = Repository remote name for git commands in the guide
34303429
34313430
config.git_config = Git Configuration

routers/web/admin/config.go

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -198,27 +198,18 @@ func ConfigSettings(ctx *context.Context) {
198198
func ChangeConfig(ctx *context.Context) {
199199
cfg := setting.Config()
200200

201-
marshalBool := func(v string) (string, error) { //nolint:unparam // error is always nil
202-
if b, _ := strconv.ParseBool(v); b {
203-
return "true", nil
204-
}
205-
return "false", nil
201+
marshalBool := func(v string) ([]byte, error) { //nolint:unparam // error is always nil
202+
b, _ := strconv.ParseBool(v)
203+
return json.Marshal(b)
206204
}
207205

208-
marshalString := func(emptyDefault string) func(v string) (string, error) {
209-
return func(v string) (string, error) {
210-
if v == "" {
211-
v = emptyDefault
212-
}
213-
b, err := json.Marshal(v)
214-
if err != nil {
215-
return "", err
216-
}
217-
return string(b), nil
206+
marshalString := func(emptyDefault string) func(v string) ([]byte, error) {
207+
return func(v string) ([]byte, error) {
208+
return json.Marshal(util.IfZero(v, emptyDefault))
218209
}
219210
}
220211

221-
marshalOpenWithApps := func(value string) (string, error) {
212+
marshalOpenWithApps := func(value string) ([]byte, error) {
222213
lines := strings.Split(value, "\n")
223214
var openWithEditorApps setting.OpenWithEditorAppsType
224215
for _, line := range lines {
@@ -236,29 +227,26 @@ func ChangeConfig(ctx *context.Context) {
236227
OpenURL: strings.TrimSpace(openURL),
237228
})
238229
}
239-
b, err := json.Marshal(openWithEditorApps)
240-
if err != nil {
241-
return "", err
242-
}
243-
return string(b), nil
230+
return json.Marshal(openWithEditorApps)
244231
}
245-
marshallers := map[string]func(string) (string, error){
232+
marshallers := map[string]func(string) ([]byte, error){
246233
cfg.Picture.DisableGravatar.DynKey(): marshalBool,
247234
cfg.Picture.EnableFederatedAvatar.DynKey(): marshalBool,
248235
cfg.Repository.OpenWithEditorApps.DynKey(): marshalOpenWithApps,
249236
cfg.Repository.GitGuideRemoteName.DynKey(): marshalString(cfg.Repository.GitGuideRemoteName.DefaultValue()),
250237
}
251238

252239
_ = ctx.Req.ParseForm()
253-
queryKeys := ctx.Req.Form["key"]
254-
queryValues := ctx.Req.Form["value"]
240+
configKeys := ctx.Req.Form["key"]
241+
configValues := ctx.Req.Form["value"]
242+
configSettings := map[string]string{}
255243
loop:
256-
for i, key := range queryKeys {
257-
if i >= len(queryValues) {
244+
for i, key := range configKeys {
245+
if i >= len(configValues) {
258246
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
259247
break loop
260248
}
261-
value := queryValues[i]
249+
value := configValues[i]
262250

263251
marshaller, hasMarshaller := marshallers[key]
264252
if !hasMarshaller {
@@ -271,15 +259,15 @@ loop:
271259
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
272260
break loop
273261
}
274-
275-
if err = system_model.SetSettings(ctx, map[string]string{key: marshaledValue}); err != nil {
276-
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
277-
break loop
278-
}
262+
configSettings[key] = string(marshaledValue)
279263
}
280-
281-
config.GetDynGetter().InvalidateCache()
282-
if !ctx.Written() {
283-
ctx.JSONOK()
264+
if ctx.Written() {
265+
return
266+
}
267+
if err := system_model.SetSettings(ctx, configSettings); err != nil {
268+
ctx.ServerError("SetSettings", err)
269+
return
284270
}
271+
config.GetDynGetter().InvalidateCache()
272+
ctx.JSONOK()
285273
}

0 commit comments

Comments
 (0)