From 5a48fe043baa096b2c2d9c8d796fe99450923a01 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 25 Jul 2023 11:29:42 -0600 Subject: [PATCH] fix: allow json with comments and trailing commas Fixes #38. --- .github/workflows/ci.yaml | 2 +- devcontainer/devcontainer.go | 10 +++++++--- devcontainer/devcontainer_test.go | 2 +- envbuilder.go | 11 ++++++++++- go.mod | 2 +- go.sum | 4 ++-- 6 files changed, 22 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 37a8de3c..ac3da435 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -45,7 +45,7 @@ jobs: # Install Go! - uses: actions/setup-go@v3 with: - go-version: "~1.20" + go-version: "1.20.5" - name: Test run: go test ./... diff --git a/devcontainer/devcontainer.go b/devcontainer/devcontainer.go index 19259072..3ff9e39a 100644 --- a/devcontainer/devcontainer.go +++ b/devcontainer/devcontainer.go @@ -2,6 +2,7 @@ package devcontainer import ( "crypto/md5" + "encoding/json" "fmt" "io" "os" @@ -14,14 +15,17 @@ import ( "github.com/go-git/go-billy/v5" "github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/v1/remote" - "muzzammil.xyz/jsonc" + "github.com/tailscale/hujson" ) // Parse parses a devcontainer.json file. func Parse(content []byte) (*Spec, error) { - content = jsonc.ToJSON(content) + content, err := hujson.Standardize(content) + if err != nil { + return nil, fmt.Errorf("standardize json: %w", err) + } var schema Spec - return &schema, jsonc.Unmarshal(content, &schema) + return &schema, json.Unmarshal(content, &schema) } type Spec struct { diff --git a/devcontainer/devcontainer_test.go b/devcontainer/devcontainer_test.go index 1beb55f5..a4f6a4b1 100644 --- a/devcontainer/devcontainer_test.go +++ b/devcontainer/devcontainer_test.go @@ -26,7 +26,7 @@ func TestParse(t *testing.T) { raw := `{ "build": { "dockerfile": "Dockerfile", - "context": "." + "context": ".", }, // Comments here! "image": "codercom/code-server:latest" diff --git a/envbuilder.go b/envbuilder.go index 5ed8d30a..4886e55a 100644 --- a/envbuilder.go +++ b/envbuilder.go @@ -45,6 +45,7 @@ import ( v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/remote" "github.com/sirupsen/logrus" + "github.com/tailscale/hujson" "golang.org/x/xerrors" ) @@ -258,6 +259,10 @@ func Run(ctx context.Context, options Options) error { return fmt.Errorf("decode docker config: %w", err) } var configFile DockerConfig + decoded, err = hujson.Standardize(decoded) + if err != nil { + return fmt.Errorf("humanize json for docker config: %w", err) + } err = json.Unmarshal(decoded, &configFile) if err != nil { return fmt.Errorf("parse docker config: %w", err) @@ -622,7 +627,11 @@ func Run(ctx context.Context, options Options) error { devContainerMetadata, exists := configFile.Config.Labels["devcontainer.metadata"] if exists { var devContainer []*devcontainer.Spec - err := json.Unmarshal([]byte(devContainerMetadata), &devContainer) + devContainerMetadataBytes, err := hujson.Standardize([]byte(devContainerMetadata)) + if err != nil { + return fmt.Errorf("humanize json for dev container metadata: %w", err) + } + err = json.Unmarshal(devContainerMetadataBytes, &devContainer) if err != nil { return fmt.Errorf("unmarshal metadata: %w", err) } diff --git a/go.mod b/go.mod index 7ab6f2cb..85dcea6a 100644 --- a/go.mod +++ b/go.mod @@ -27,8 +27,8 @@ require ( github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.7.0 github.com/stretchr/testify v1.8.4 + github.com/tailscale/hujson v0.0.0-20220630195928-54599719472f golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 - muzzammil.xyz/jsonc v1.0.0 ) require ( diff --git a/go.sum b/go.sum index 269544a5..fed7da35 100644 --- a/go.sum +++ b/go.sum @@ -824,6 +824,8 @@ github.com/tailscale/golang-x-crypto v0.0.0-20221102133106-bc99ab8c2d17 h1:cSm67 github.com/tailscale/golang-x-crypto v0.0.0-20221102133106-bc99ab8c2d17/go.mod h1:95n9fbUCixVSI4QXLEvdKJjnYK2eUlkTx9+QwLPXFKU= github.com/tailscale/goupnp v1.0.1-0.20210804011211-c64d0f06ea05 h1:4chzWmimtJPxRs2O36yuGRW3f9SYV+bMTTvMBI0EKio= github.com/tailscale/goupnp v1.0.1-0.20210804011211-c64d0f06ea05/go.mod h1:PdCqy9JzfWMJf1H5UJW2ip33/d4YkoKN0r67yKH1mG8= +github.com/tailscale/hujson v0.0.0-20220630195928-54599719472f h1:n4r/sJ92cBSBHK8n9lR1XLFr0OiTVeGfN5TR+9LaN7E= +github.com/tailscale/hujson v0.0.0-20220630195928-54599719472f/go.mod h1:DFSS3NAGHthKo1gTlmEcSBiZrRJXi28rLNd/1udP1c8= github.com/tailscale/netlink v1.1.1-0.20211101221916-cabfb018fe85 h1:zrsUcqrG2uQSPhaUPjUQwozcRdDdSxxqhNgNZ3drZFk= github.com/tailscale/netlink v1.1.1-0.20211101221916-cabfb018fe85/go.mod h1:NzVQi3Mleb+qzq8VmcWpSkcSYxXIg0DkI6XDzpVkhJ0= github.com/tailscale/wireguard-go v0.0.0-20221219190806-4fa124729667 h1:etWp6uUwKu8NEj37K2OuMBnZ7EnVMKA7gJg5AqPFy/o= @@ -1384,8 +1386,6 @@ honnef.co/go/tools v0.4.0-0.dev.0.20230130122044-c30b15588105 h1:2OzOQ+1scFmv2dt howett.net/plist v1.0.0 h1:7CrbWYbPPO/PyNy38b2EB/+gYbjCe2DXBxgtOOZbSQM= inet.af/peercred v0.0.0-20210906144145-0893ea02156a h1:qdkS8Q5/i10xU2ArJMKYhVa1DORzBfYS/qA2UK2jheg= inet.af/peercred v0.0.0-20210906144145-0893ea02156a/go.mod h1:FjawnflS/udxX+SvpsMgZfdqx2aykOlkISeAsADi5IU= -muzzammil.xyz/jsonc v1.0.0 h1:B6kaT3wHueZ87mPz3q1nFuM1BlL32IG0wcq0/uOsQ18= -muzzammil.xyz/jsonc v1.0.0/go.mod h1:rFv8tUUKe+QLh7v02BhfxXEf4ZHhYD7unR93HL/1Uvo= nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=