Skip to content

Commit 273f81b

Browse files
authored
keep original file order in SBOM Enrich (#258)
1 parent fc3c156 commit 273f81b

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

Diff for: commands/enrich/enrich.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package enrich
22

33
import (
4-
"encoding/json"
54
"encoding/xml"
65
"errors"
76
"fmt"
7+
"github.com/jfrog/jfrog-cli-security/utils/results/output"
88
"os"
99
"os/exec"
1010
"path/filepath"
@@ -17,7 +17,6 @@ import (
1717
"github.com/jfrog/jfrog-cli-security/commands/enrich/enrichgraph"
1818
"github.com/jfrog/jfrog-cli-security/utils"
1919
"github.com/jfrog/jfrog-cli-security/utils/results"
20-
"github.com/jfrog/jfrog-cli-security/utils/results/output"
2120
"github.com/jfrog/jfrog-cli-security/utils/techutils"
2221
"github.com/jfrog/jfrog-cli-security/utils/xray"
2322
"github.com/jfrog/jfrog-client-go/artifactory/services/fspatterns"
@@ -27,6 +26,7 @@ import (
2726
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
2827
"github.com/jfrog/jfrog-client-go/utils/log"
2928
"github.com/jfrog/jfrog-client-go/xray/services"
29+
orderedJson "github.com/virtuald/go-ordered-json"
3030
)
3131

3232
type FileContext func(string) parallel.TaskFunc
@@ -75,8 +75,8 @@ func AppendVulnsToJson(cmdResults *results.SecurityCommandResults) error {
7575
if err != nil {
7676
return fmt.Errorf("error reading file: %s", err.Error())
7777
}
78-
var data map[string]interface{}
79-
err = json.Unmarshal(fileContent, &data)
78+
var data orderedJson.OrderedObject
79+
err = orderedJson.Unmarshal(fileContent, &data)
8080
if err != nil {
8181
return fmt.Errorf("error parsing JSON: %s", err.Error())
8282
}
@@ -93,7 +93,7 @@ func AppendVulnsToJson(cmdResults *results.SecurityCommandResults) error {
9393
vulnerabilities = append(vulnerabilities, vulnerability)
9494
}
9595
}
96-
data["vulnerabilities"] = vulnerabilities
96+
data = append(data, orderedJson.Member{Key: "vulnerabilities", Value: vulnerabilities})
9797
return output.PrintJson(data)
9898
}
9999

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/owenrumney/go-sarif/v2 v2.3.0
1717
github.com/stretchr/testify v1.9.0
1818
github.com/urfave/cli v1.22.16
19+
github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74
1920
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
2021
golang.org/x/sync v0.9.0
2122
golang.org/x/text v0.20.0

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ github.com/urfave/cli v1.22.16 h1:MH0k6uJxdwdeWQTwhSO42Pwr4YLrNLwBtg1MRgTqPdQ=
257257
github.com/urfave/cli v1.22.16/go.mod h1:EeJR6BKodywf4zciqrdw6hpCPk68JO9z5LazXZMn5Po=
258258
github.com/vbauerster/mpb/v8 v8.8.3 h1:dTOByGoqwaTJYPubhVz3lO5O6MK553XVgUo33LdnNsQ=
259259
github.com/vbauerster/mpb/v8 v8.8.3/go.mod h1:JfCCrtcMsJwP6ZwMn9e5LMnNyp3TVNpUWWkN+nd4EWk=
260+
github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74 h1:JwtAtbp7r/7QSyGz8mKUbYJBg2+6Cd7OjM8o/GNOcVo=
261+
github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74/go.mod h1:RmMWU37GKR2s6pgrIEB4ixgpVCt/cf7dnJv3fuH1J1c=
260262
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
261263
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
262264
github.com/xanzy/go-gitlab v0.110.0 h1:hsFIFp01v/0D0sdUXoZfRk6CROzZbHQplk6NzKSFKhc=

Diff for: utils/utils.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/hex"
77
"encoding/json"
88
"fmt"
9+
orderedJson "github.com/virtuald/go-ordered-json"
910
"os"
1011
"path/filepath"
1112
"strings"
@@ -112,7 +113,7 @@ func UniqueUnion[T comparable](arr []T, elements ...T) []T {
112113

113114
func GetAsJsonBytes(output interface{}, escapeValues, indent bool) (results []byte, err error) {
114115
if escapeValues {
115-
if results, err = json.Marshal(output); errorutils.CheckError(err) != nil {
116+
if results, err = orderedJson.Marshal(output); errorutils.CheckError(err) != nil {
116117
return
117118
}
118119
} else {

0 commit comments

Comments
 (0)