feat: use hujson to parse config files

This commit is contained in:
Sylvia Crowe 2024-12-04 13:58:02 -08:00
parent d8edea9fbc
commit 56f291d25a
3 changed files with 14 additions and 6 deletions

5
go.mod
View File

@ -1,6 +1,8 @@
module github.com/wavetermdev/waveterm module github.com/wavetermdev/waveterm
go 1.22.4 go 1.23
toolchain go1.23.4
require ( require (
github.com/alexflint/go-filemutex v1.3.0 github.com/alexflint/go-filemutex v1.3.0
@ -21,6 +23,7 @@ require (
github.com/shirou/gopsutil/v4 v4.24.10 github.com/shirou/gopsutil/v4 v4.24.10
github.com/skeema/knownhosts v1.3.0 github.com/skeema/knownhosts v1.3.0
github.com/spf13/cobra v1.8.1 github.com/spf13/cobra v1.8.1
github.com/tailscale/hujson v0.0.0-20241010212012-29efb4a0184b
github.com/ubuntu/gowsl v0.0.0-20240906163211-049fd49bd93b github.com/ubuntu/gowsl v0.0.0-20240906163211-049fd49bd93b
github.com/wavetermdev/htmltoken v0.2.0 github.com/wavetermdev/htmltoken v0.2.0
golang.org/x/crypto v0.29.0 golang.org/x/crypto v0.29.0

2
go.sum
View File

@ -78,6 +78,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tailscale/hujson v0.0.0-20241010212012-29efb4a0184b h1:MNaGusDfB1qxEsl6iVb33Gbe777IKzPP5PDta0xGC8M=
github.com/tailscale/hujson v0.0.0-20241010212012-29efb4a0184b/go.mod h1:EbW0wDK/qEUYI0A5bqq0C2kF8JTQwWONmGDBbzsxxHo=
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU= github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI= github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk= github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=

View File

@ -15,6 +15,7 @@ import (
"sort" "sort"
"strings" "strings"
"github.com/tailscale/hujson"
"github.com/wavetermdev/waveterm/pkg/util/utilfn" "github.com/wavetermdev/waveterm/pkg/util/utilfn"
"github.com/wavetermdev/waveterm/pkg/wavebase" "github.com/wavetermdev/waveterm/pkg/wavebase"
"github.com/wavetermdev/waveterm/pkg/waveobj" "github.com/wavetermdev/waveterm/pkg/waveobj"
@ -153,7 +154,11 @@ func readConfigHelper(fileName string, barr []byte, readErr error) (waveobj.Meta
return nil, cerrs return nil, cerrs
} }
var rtn waveobj.MetaMapType var rtn waveobj.MetaMapType
err := json.Unmarshal(barr, &rtn) barr, err := hujson.Standardize(barr)
if err != nil {
cerrs = append(cerrs, ConfigError{File: fileName, Err: err.Error()})
}
err = json.Unmarshal(barr, &rtn)
if err != nil { if err != nil {
if syntaxErr, ok := err.(*json.SyntaxError); ok { if syntaxErr, ok := err.(*json.SyntaxError); ok {
offset := syntaxErr.Offset offset := syntaxErr.Offset
@ -407,7 +412,7 @@ func jsonMarshalConfigInOrder(m waveobj.MetaMapType) ([]byte, error) {
var buf bytes.Buffer var buf bytes.Buffer
orderedKeys := orderConfigKeys(m) orderedKeys := orderConfigKeys(m)
buf.WriteString("{\n") buf.WriteString("{\n")
for idx, key := range orderedKeys { for _, key := range orderedKeys {
val := m[key] val := m[key]
keyBarr, err := json.Marshal(key) keyBarr, err := json.Marshal(key)
if err != nil { if err != nil {
@ -422,9 +427,7 @@ func jsonMarshalConfigInOrder(m waveobj.MetaMapType) ([]byte, error) {
buf.Write(keyBarr) buf.Write(keyBarr)
buf.WriteString(": ") buf.WriteString(": ")
buf.Write(valBarr) buf.Write(valBarr)
if idx < len(orderedKeys)-1 {
buf.WriteString(",") buf.WriteString(",")
}
buf.WriteString("\n") buf.WriteString("\n")
} }
buf.WriteString("}") buf.WriteString("}")