Skip to content

Commit 41872af

Browse files
committed
convert ENVs to the correct type
1 parent 9e88777 commit 41872af

File tree

3 files changed

+104
-19
lines changed

3 files changed

+104
-19
lines changed

config.schema.json

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"type": "boolean"
1818
},
1919
"time_format": {
20-
"type": "integer"
20+
"type": ["integer", "string"]
2121
},
2222
"show_date": {
2323
"type": "boolean"
@@ -103,6 +103,9 @@
103103
"menu_position": {
104104
"type": "string"
105105
},
106+
"disable_navigation": {
107+
"type": "boolean"
108+
},
106109
"disable_ui": {
107110
"type": "boolean"
108111
},
@@ -139,6 +142,9 @@
139142
"sleep_icon": {
140143
"type": "boolean"
141144
},
145+
"disable_sleep": {
146+
"type": "boolean"
147+
},
142148
"transition": {
143149
"type": "string"
144150
},
@@ -154,6 +160,12 @@
154160
"progress_bar_position": {
155161
"type": "string"
156162
},
163+
"custom_css": {
164+
"type": "boolean"
165+
},
166+
"custom_css_class": {
167+
"type": "string"
168+
},
157169
"image_fit": {
158170
"type": "string"
159171
},
@@ -188,7 +200,7 @@
188200
"type": "boolean"
189201
},
190202
"image_time_format": {
191-
"type": "integer"
203+
"type": ["integer", "string"]
192204
},
193205
"show_image_date": {
194206
"type": "boolean"
@@ -248,10 +260,10 @@
248260
"type": "string"
249261
},
250262
"lat": {
251-
"type": "number"
263+
"type": ["number", "string"]
252264
},
253265
"lon": {
254-
"type": "number"
266+
"type": ["number", "string"]
255267
},
256268
"api": {
257269
"type": "string"
@@ -291,6 +303,12 @@
291303
"required": ["url", "event"]
292304
}
293305
},
306+
"blacklist": {
307+
"type": "array",
308+
"items": {
309+
"type": "string"
310+
}
311+
},
294312
"iframe": {
295313
"type": ["string", "array"],
296314
"items": {
@@ -307,6 +325,12 @@
307325
},
308326
"required": []
309327
},
328+
"user": {
329+
"type": ["string", "array"],
330+
"items": {
331+
"type": "string"
332+
}
333+
},
310334
"show_user": {
311335
"type": "boolean"
312336
},
@@ -338,6 +362,9 @@
338362
"type": "object",
339363
"additionalProperties": false,
340364
"properties": {
365+
"version": {
366+
"type": "string"
367+
},
341368
"debug": {
342369
"type": "boolean"
343370
},

internal/config/config.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,14 @@ func (c *Config) Load() error {
508508

509509
c.V.AutomaticEnv()
510510

511-
err := c.V.ReadInConfig()
512-
if err != nil {
511+
readInConfigErr := c.V.ReadInConfig()
512+
if readInConfigErr != nil {
513513
var configFileNotFoundErr viper.ConfigFileNotFoundError
514514
switch {
515-
case errors.As(err, &configFileNotFoundErr):
515+
case errors.As(readInConfigErr, &configFileNotFoundErr):
516516
log.Info("Not using config.yaml")
517517
case !isValidYAML(c.V.ConfigFileUsed()):
518-
log.Fatal(err)
518+
log.Fatal(readInConfigErr)
519519
}
520520
} else {
521521
level := strings.ToLower(strings.TrimSpace(c.V.GetString("kiosk.config_validation_level")))
@@ -529,7 +529,7 @@ func (c *Config) Load() error {
529529
}
530530
}
531531

532-
err = c.V.Unmarshal(&c)
532+
err := c.V.Unmarshal(&c)
533533
if err != nil {
534534
log.Error("Environment can't be loaded", "err", err)
535535
return err

internal/config/config_validation.go

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"reflect"
99
"slices"
10+
"strconv"
1011
"strings"
1112

1213
"github.com/charmbracelet/log"
@@ -368,18 +369,16 @@ func checkSchema(config map[string]any, level string) bool {
368369
return true
369370
}
370371

371-
// if we are using a config.yaml file but supplying immich_api_key || immich_url via ENVs get them
372-
if v, ok := config["immich_api_key"]; !ok || v == "" {
373-
config["immich_api_key"] = os.Getenv("KIOSK_IMMICH_API_KEY")
374-
}
375-
376-
if v, ok := config["immich_url"]; !ok || v == "" {
377-
config["immich_url"] = os.Getenv("KIOSK_IMMICH_URL")
372+
typed := ConfigTypes(config, Config{})
373+
for k, v := range config {
374+
if _, ok := typed[k]; !ok {
375+
typed[k] = v
376+
}
378377
}
379378

380-
jsonData, err := json.Marshal(config)
379+
jsonData, err := json.Marshal(typed)
381380
if err != nil {
382-
log.Error("Failed to marshal config to JSON", "err", err)
381+
log.Error("Schema validation setup failed: marshal", "err", err)
383382
return false
384383
}
385384

@@ -390,7 +389,7 @@ func checkSchema(config map[string]any, level string) bool {
390389
// Validate
391390
result, err := gojsonschema.Validate(schemaLoader, docLoader)
392391
if err != nil {
393-
log.Error("Schema validation setup failed", "err", err)
392+
log.Error("Schema validation setup failed: validate", "err", err)
394393
return false
395394
}
396395

@@ -412,3 +411,62 @@ func checkSchema(config map[string]any, level string) bool {
412411

413412
return true
414413
}
414+
415+
func ConfigTypes(settings map[string]any, cfgStruct any) map[string]any {
416+
return convertConfigTypes(reflect.TypeOf(cfgStruct), settings)
417+
}
418+
419+
func convertConfigTypes(typ reflect.Type, settings map[string]any) map[string]any {
420+
result := make(map[string]any)
421+
422+
// If pointer, get the element
423+
if typ.Kind() == reflect.Pointer {
424+
typ = typ.Elem()
425+
}
426+
427+
for i := range typ.NumField() {
428+
field := typ.Field(i)
429+
tag := field.Tag.Get("mapstructure")
430+
if tag == "" {
431+
tag = field.Name
432+
}
433+
434+
raw, rawFound := settings[tag]
435+
if !rawFound {
436+
continue
437+
}
438+
439+
switch field.Type.Kind() {
440+
case reflect.Struct:
441+
if nestedMap, ok := raw.(map[string]any); ok {
442+
result[tag] = convertConfigTypes(field.Type, nestedMap)
443+
}
444+
case reflect.Int:
445+
switch v := raw.(type) {
446+
case string:
447+
if n, err := strconv.Atoi(v); err == nil {
448+
result[tag] = n
449+
}
450+
default:
451+
result[tag] = v
452+
}
453+
case reflect.Bool:
454+
switch v := raw.(type) {
455+
case string:
456+
if b, err := strconv.ParseBool(v); err == nil {
457+
result[tag] = b
458+
}
459+
default:
460+
result[tag] = v
461+
}
462+
case reflect.String:
463+
if s, ok := raw.(string); ok {
464+
result[tag] = s
465+
}
466+
default:
467+
result[tag] = raw
468+
}
469+
}
470+
471+
return result
472+
}

0 commit comments

Comments
 (0)