chore: simplify config serialisation
This commit is contained in:
parent
bb4b842598
commit
14fdabbcb6
6 changed files with 19 additions and 116 deletions
|
|
@ -54,7 +54,7 @@ func runConfig(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
|
||||
for _, c := range configurable {
|
||||
fmt.Println(config.NameForID(c.ID), "type:"+c.Type, redactJson(c.Value))
|
||||
fmt.Println(config.NameForID(c.ID), "type:"+c.Type, redactMap(c.Data))
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ func runConfigDelete(cmd *cobra.Command, args []string) {
|
|||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(config.NameForID(c.ID), "type:"+c.Type, c.Value)
|
||||
fmt.Println(config.NameForID(c.ID), "type:"+c.Type, c.Data)
|
||||
|
||||
switch c.Class {
|
||||
case templates.Charger:
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ func runDevice(cmd *cobra.Command, args []string) {
|
|||
|
||||
for _, d := range devs {
|
||||
fmt.Printf("%d. %s\n", d.ID, d.Type)
|
||||
fmt.Println(d.Value)
|
||||
fmt.Println(d.Data)
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net"
|
||||
"os"
|
||||
"regexp"
|
||||
|
|
@ -64,20 +64,15 @@ func redact(src string) string {
|
|||
ReplaceAllString(src, "$1: *****")
|
||||
}
|
||||
|
||||
func redactJson(src string) string {
|
||||
var j map[string]any
|
||||
if err := json.Unmarshal([]byte(src), &j); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for k := range j {
|
||||
func redactMap(src map[string]any) map[string]any {
|
||||
res := maps.Clone(src)
|
||||
for k := range res {
|
||||
if slices.Contains(redactSecrets, k) {
|
||||
j[k] = "*****"
|
||||
res[k] = "*****"
|
||||
}
|
||||
}
|
||||
|
||||
res, _ := json.Marshal(j)
|
||||
return string(res)
|
||||
return res
|
||||
}
|
||||
|
||||
// fatal logs a fatal error and runs shutdown functions before terminating
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue