From c0b2fe3018436764d260d3eab0b4103a64fa0589 Mon Sep 17 00:00:00 2001 From: barv Date: Tue, 3 Dec 2024 17:27:38 +0200 Subject: [PATCH 1/2] keep original file order --- commands/enrich/enrich.go | 7 ++++--- go.mod | 1 + go.sum | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/commands/enrich/enrich.go b/commands/enrich/enrich.go index f3c5752d..83900673 100644 --- a/commands/enrich/enrich.go +++ b/commands/enrich/enrich.go @@ -5,6 +5,7 @@ import ( "encoding/xml" "errors" "fmt" + "github.com/jfrog/jfrog-cli-security/utils/results/output" "os" "os/exec" "path/filepath" @@ -17,7 +18,6 @@ import ( "github.com/jfrog/jfrog-cli-security/commands/enrich/enrichgraph" "github.com/jfrog/jfrog-cli-security/utils" "github.com/jfrog/jfrog-cli-security/utils/results" - "github.com/jfrog/jfrog-cli-security/utils/results/output" "github.com/jfrog/jfrog-cli-security/utils/techutils" "github.com/jfrog/jfrog-cli-security/utils/xray" "github.com/jfrog/jfrog-client-go/artifactory/services/fspatterns" @@ -27,6 +27,7 @@ import ( "github.com/jfrog/jfrog-client-go/utils/io/fileutils" "github.com/jfrog/jfrog-client-go/utils/log" "github.com/jfrog/jfrog-client-go/xray/services" + orderedJson "gitlab.com/c0b/go-ordered-json" ) type FileContext func(string) parallel.TaskFunc @@ -75,7 +76,7 @@ func AppendVulnsToJson(cmdResults *results.SecurityCommandResults) error { if err != nil { return fmt.Errorf("error reading file: %s", err.Error()) } - var data map[string]interface{} + data := orderedJson.NewOrderedMap() err = json.Unmarshal(fileContent, &data) if err != nil { return fmt.Errorf("error parsing JSON: %s", err.Error()) @@ -93,7 +94,7 @@ func AppendVulnsToJson(cmdResults *results.SecurityCommandResults) error { vulnerabilities = append(vulnerabilities, vulnerability) } } - data["vulnerabilities"] = vulnerabilities + data.Set("vulnerabilities", vulnerabilities) return output.PrintJson(data) } diff --git a/go.mod b/go.mod index 2072de20..3a722ba8 100644 --- a/go.mod +++ b/go.mod @@ -97,6 +97,7 @@ require ( github.com/xanzy/ssh-agent v0.3.3 // indirect github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect + gitlab.com/c0b/go-ordered-json v0.0.0-20201030195603-febf46534d5a // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.9.0 // indirect golang.org/x/crypto v0.29.0 // indirect diff --git a/go.sum b/go.sum index f88b8d6e..726eb69c 100644 --- a/go.sum +++ b/go.sum @@ -275,6 +275,8 @@ github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHg github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/zclconf/go-cty v1.10.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk= +gitlab.com/c0b/go-ordered-json v0.0.0-20201030195603-febf46534d5a h1:DxppxFKRqJ8WD6oJ3+ZXKDY0iMONQDl5UTg2aTyHh8k= +gitlab.com/c0b/go-ordered-json v0.0.0-20201030195603-febf46534d5a/go.mod h1:NREvu3a57BaK0R1+ztrEzHWiZAihohNLQ6trPxlIqZI= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= From 49ec2f7ea85503edb767a0f4aee5aafa70a7be8f Mon Sep 17 00:00:00 2001 From: barv Date: Wed, 4 Dec 2024 13:57:48 +0200 Subject: [PATCH 2/2] keep original file order --- commands/enrich/enrich.go | 9 ++++----- go.mod | 2 +- go.sum | 4 ++-- utils/utils.go | 3 ++- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/commands/enrich/enrich.go b/commands/enrich/enrich.go index 83900673..e4a1e35a 100644 --- a/commands/enrich/enrich.go +++ b/commands/enrich/enrich.go @@ -1,7 +1,6 @@ package enrich import ( - "encoding/json" "encoding/xml" "errors" "fmt" @@ -27,7 +26,7 @@ import ( "github.com/jfrog/jfrog-client-go/utils/io/fileutils" "github.com/jfrog/jfrog-client-go/utils/log" "github.com/jfrog/jfrog-client-go/xray/services" - orderedJson "gitlab.com/c0b/go-ordered-json" + orderedJson "github.com/virtuald/go-ordered-json" ) type FileContext func(string) parallel.TaskFunc @@ -76,8 +75,8 @@ func AppendVulnsToJson(cmdResults *results.SecurityCommandResults) error { if err != nil { return fmt.Errorf("error reading file: %s", err.Error()) } - data := orderedJson.NewOrderedMap() - err = json.Unmarshal(fileContent, &data) + var data orderedJson.OrderedObject + err = orderedJson.Unmarshal(fileContent, &data) if err != nil { return fmt.Errorf("error parsing JSON: %s", err.Error()) } @@ -94,7 +93,7 @@ func AppendVulnsToJson(cmdResults *results.SecurityCommandResults) error { vulnerabilities = append(vulnerabilities, vulnerability) } } - data.Set("vulnerabilities", vulnerabilities) + data = append(data, orderedJson.Member{Key: "vulnerabilities", Value: vulnerabilities}) return output.PrintJson(data) } diff --git a/go.mod b/go.mod index 3a722ba8..2f69c265 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/owenrumney/go-sarif/v2 v2.3.0 github.com/stretchr/testify v1.9.0 github.com/urfave/cli v1.22.16 + github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74 golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f golang.org/x/sync v0.9.0 golang.org/x/text v0.20.0 @@ -97,7 +98,6 @@ require ( github.com/xanzy/ssh-agent v0.3.3 // indirect github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect - gitlab.com/c0b/go-ordered-json v0.0.0-20201030195603-febf46534d5a // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.9.0 // indirect golang.org/x/crypto v0.29.0 // indirect diff --git a/go.sum b/go.sum index 726eb69c..0b6b9be1 100644 --- a/go.sum +++ b/go.sum @@ -257,6 +257,8 @@ github.com/urfave/cli v1.22.16 h1:MH0k6uJxdwdeWQTwhSO42Pwr4YLrNLwBtg1MRgTqPdQ= github.com/urfave/cli v1.22.16/go.mod h1:EeJR6BKodywf4zciqrdw6hpCPk68JO9z5LazXZMn5Po= github.com/vbauerster/mpb/v8 v8.8.3 h1:dTOByGoqwaTJYPubhVz3lO5O6MK553XVgUo33LdnNsQ= github.com/vbauerster/mpb/v8 v8.8.3/go.mod h1:JfCCrtcMsJwP6ZwMn9e5LMnNyp3TVNpUWWkN+nd4EWk= +github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74 h1:JwtAtbp7r/7QSyGz8mKUbYJBg2+6Cd7OjM8o/GNOcVo= +github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74/go.mod h1:RmMWU37GKR2s6pgrIEB4ixgpVCt/cf7dnJv3fuH1J1c= github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4= github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI= github.com/xanzy/go-gitlab v0.110.0 h1:hsFIFp01v/0D0sdUXoZfRk6CROzZbHQplk6NzKSFKhc= @@ -275,8 +277,6 @@ github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHg github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/zclconf/go-cty v1.10.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk= -gitlab.com/c0b/go-ordered-json v0.0.0-20201030195603-febf46534d5a h1:DxppxFKRqJ8WD6oJ3+ZXKDY0iMONQDl5UTg2aTyHh8k= -gitlab.com/c0b/go-ordered-json v0.0.0-20201030195603-febf46534d5a/go.mod h1:NREvu3a57BaK0R1+ztrEzHWiZAihohNLQ6trPxlIqZI= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= diff --git a/utils/utils.go b/utils/utils.go index aa5f5d2b..1681ae38 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + orderedJson "github.com/virtuald/go-ordered-json" "os" "path/filepath" "strings" @@ -112,7 +113,7 @@ func UniqueUnion[T comparable](arr []T, elements ...T) []T { func GetAsJsonBytes(output interface{}, escapeValues, indent bool) (results []byte, err error) { if escapeValues { - if results, err = json.Marshal(output); errorutils.CheckError(err) != nil { + if results, err = orderedJson.Marshal(output); errorutils.CheckError(err) != nil { return } } else {