Skip to content

Commit 5b3e8b8

Browse files
authored
Merge pull request #4514 from snyk/chore/HEAD-206_json_error_output
chore: write errors as json if —json is set
2 parents 5508ee0 + 4a9dcb7 commit 5b3e8b8

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

cliv2/cmd/cliv2/main.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"crypto/sha256"
55
"encoding/hex"
6+
"encoding/json"
67
"fmt"
78
"io"
89
"log"
@@ -36,6 +37,12 @@ var debugLogger = log.New(os.Stderr, "", 0)
3637

3738
const unknownCommandMessage string = "unknown command"
3839

40+
type JsonErrorStruct struct {
41+
Ok bool `json:"ok"`
42+
ErrorMsg string `json:"error"`
43+
Path string `json:"path"`
44+
}
45+
3946
type HandleError int
4047

4148
const (
@@ -92,12 +99,12 @@ func runCommand(cmd *cobra.Command, args []string) error {
9299
debugLogger.Println("Running", name)
93100

94101
if len(args) > 0 {
95-
config.Set("targetDirectory", args[0])
102+
config.Set(configuration.INPUT_DIRECTORY, args[0])
96103
}
97104

98105
data, err := engine.Invoke(workflow.NewWorkflowIdentifier(name))
99106
if err == nil {
100-
_, err = engine.InvokeWithInput(workflow.NewWorkflowIdentifier("output"), data)
107+
_, err = engine.InvokeWithInput(localworkflows.WORKFLOWID_OUTPUT_WORKFLOW, data)
101108
} else {
102109
debugLogger.Println("Failed to execute the command!", err)
103110
}
@@ -255,7 +262,18 @@ func handleError(err error) HandleError {
255262
func displayError(err error) {
256263
if err != nil {
257264
if _, ok := err.(*exec.ExitError); !ok {
258-
fmt.Println(err)
265+
if config.GetBool(localworkflows.OUTPUT_CONFIG_KEY_JSON) {
266+
jsonError := JsonErrorStruct{
267+
Ok: false,
268+
ErrorMsg: err.Error(),
269+
Path: config.GetString(configuration.INPUT_DIRECTORY),
270+
}
271+
272+
jsonErrorBuffer, _ := json.MarshalIndent(jsonError, "", " ")
273+
fmt.Println(string(jsonErrorBuffer))
274+
} else {
275+
fmt.Println(err)
276+
}
259277
}
260278
}
261279
}

cliv2/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/google/uuid v1.3.0
1010
github.com/pkg/errors v0.9.1
1111
github.com/snyk/cli-extension-sbom v0.0.0-20230331093938-3d6a5dfdae22
12-
github.com/snyk/go-application-framework v0.0.0-20230329114722-428327e7b1f3
12+
github.com/snyk/go-application-framework v0.0.0-20230404140303-619207af7ffe
1313
github.com/snyk/go-httpauth v0.0.0-20230328170530-1af63c87b650
1414
github.com/snyk/snyk-iac-capture v0.6.0
1515
github.com/spf13/cobra v1.6.0

cliv2/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV
196196
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
197197
github.com/snyk/cli-extension-sbom v0.0.0-20230331093938-3d6a5dfdae22 h1:ucnmZwoo1gGU+YjmbYZAix5HKIZ1FYBNDau5RPwCSS8=
198198
github.com/snyk/cli-extension-sbom v0.0.0-20230331093938-3d6a5dfdae22/go.mod h1:83CWQ4Oy3mL8cVkj/etP+bh7I8I1xb+n2bpsE6URuPs=
199-
github.com/snyk/go-application-framework v0.0.0-20230329114722-428327e7b1f3 h1:FJ8GZ1rPW5Pi3ME7zCK2cNp3n7zG9gH79f4SsjLe6PI=
200-
github.com/snyk/go-application-framework v0.0.0-20230329114722-428327e7b1f3/go.mod h1:vh4egVjz3y+keFRGr0+uaifHNHmcXjDXk/8U7UwHg9E=
199+
github.com/snyk/go-application-framework v0.0.0-20230404140303-619207af7ffe h1:PgV9ubvpZIIQ17GlRlCumvq61sCtEymBgeFI+BdsVX0=
200+
github.com/snyk/go-application-framework v0.0.0-20230404140303-619207af7ffe/go.mod h1:vh4egVjz3y+keFRGr0+uaifHNHmcXjDXk/8U7UwHg9E=
201201
github.com/snyk/go-httpauth v0.0.0-20230328170530-1af63c87b650 h1:CsLoEIHxq4i3d9di8RoN3J3D1/oK20oroEZUGShor0o=
202202
github.com/snyk/go-httpauth v0.0.0-20230328170530-1af63c87b650/go.mod h1:88KbbvGYlmLgee4OcQ19yr0bNpXpOr2kciOthaSzCAg=
203203
github.com/snyk/snyk-iac-capture v0.6.0 h1:P9GWIyvl+F23XZOCuJvzGV6tME/vxbKpZM7/9dw48as=

0 commit comments

Comments
 (0)