From 4a9dcb7329669f3edef2a79fc91351222f8e1440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Scha=CC=88fer?= <101886095+PeterSchafer@users.noreply.github.com> Date: Tue, 4 Apr 2023 13:30:54 +0200 Subject: [PATCH] =?UTF-8?q?chore:=20write=20errors=20as=20json=20if=20?= =?UTF-8?q?=E2=80=94json=20is=20set?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit and re-use global constants instead of strings --- cliv2/cmd/cliv2/main.go | 24 +++++++++++++++++++++--- cliv2/go.mod | 2 +- cliv2/go.sum | 4 ++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/cliv2/cmd/cliv2/main.go b/cliv2/cmd/cliv2/main.go index 503163e758..5a9c33b124 100644 --- a/cliv2/cmd/cliv2/main.go +++ b/cliv2/cmd/cliv2/main.go @@ -3,6 +3,7 @@ package main import ( "crypto/sha256" "encoding/hex" + "encoding/json" "fmt" "io" "log" @@ -36,6 +37,12 @@ var debugLogger = log.New(os.Stderr, "", 0) const unknownCommandMessage string = "unknown command" +type JsonErrorStruct struct { + Ok bool `json:"ok"` + ErrorMsg string `json:"error"` + Path string `json:"path"` +} + type HandleError int const ( @@ -92,12 +99,12 @@ func runCommand(cmd *cobra.Command, args []string) error { debugLogger.Println("Running", name) if len(args) > 0 { - config.Set("targetDirectory", args[0]) + config.Set(configuration.INPUT_DIRECTORY, args[0]) } data, err := engine.Invoke(workflow.NewWorkflowIdentifier(name)) if err == nil { - _, err = engine.InvokeWithInput(workflow.NewWorkflowIdentifier("output"), data) + _, err = engine.InvokeWithInput(localworkflows.WORKFLOWID_OUTPUT_WORKFLOW, data) } else { debugLogger.Println("Failed to execute the command!", err) } @@ -255,7 +262,18 @@ func handleError(err error) HandleError { func displayError(err error) { if err != nil { if _, ok := err.(*exec.ExitError); !ok { - fmt.Println(err) + if config.GetBool(localworkflows.OUTPUT_CONFIG_KEY_JSON) { + jsonError := JsonErrorStruct{ + Ok: false, + ErrorMsg: err.Error(), + Path: config.GetString(configuration.INPUT_DIRECTORY), + } + + jsonErrorBuffer, _ := json.MarshalIndent(jsonError, "", " ") + fmt.Println(string(jsonErrorBuffer)) + } else { + fmt.Println(err) + } } } } diff --git a/cliv2/go.mod b/cliv2/go.mod index dd3d3848a2..4a2a952c54 100644 --- a/cliv2/go.mod +++ b/cliv2/go.mod @@ -9,7 +9,7 @@ require ( github.com/google/uuid v1.3.0 github.com/pkg/errors v0.9.1 github.com/snyk/cli-extension-sbom v0.0.0-20230331093938-3d6a5dfdae22 - github.com/snyk/go-application-framework v0.0.0-20230329114722-428327e7b1f3 + github.com/snyk/go-application-framework v0.0.0-20230404140303-619207af7ffe github.com/snyk/go-httpauth v0.0.0-20230328170530-1af63c87b650 github.com/snyk/snyk-iac-capture v0.6.0 github.com/spf13/cobra v1.6.0 diff --git a/cliv2/go.sum b/cliv2/go.sum index 92de094757..cc4e78622d 100644 --- a/cliv2/go.sum +++ b/cliv2/go.sum @@ -196,8 +196,8 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/snyk/cli-extension-sbom v0.0.0-20230331093938-3d6a5dfdae22 h1:ucnmZwoo1gGU+YjmbYZAix5HKIZ1FYBNDau5RPwCSS8= github.com/snyk/cli-extension-sbom v0.0.0-20230331093938-3d6a5dfdae22/go.mod h1:83CWQ4Oy3mL8cVkj/etP+bh7I8I1xb+n2bpsE6URuPs= -github.com/snyk/go-application-framework v0.0.0-20230329114722-428327e7b1f3 h1:FJ8GZ1rPW5Pi3ME7zCK2cNp3n7zG9gH79f4SsjLe6PI= -github.com/snyk/go-application-framework v0.0.0-20230329114722-428327e7b1f3/go.mod h1:vh4egVjz3y+keFRGr0+uaifHNHmcXjDXk/8U7UwHg9E= +github.com/snyk/go-application-framework v0.0.0-20230404140303-619207af7ffe h1:PgV9ubvpZIIQ17GlRlCumvq61sCtEymBgeFI+BdsVX0= +github.com/snyk/go-application-framework v0.0.0-20230404140303-619207af7ffe/go.mod h1:vh4egVjz3y+keFRGr0+uaifHNHmcXjDXk/8U7UwHg9E= github.com/snyk/go-httpauth v0.0.0-20230328170530-1af63c87b650 h1:CsLoEIHxq4i3d9di8RoN3J3D1/oK20oroEZUGShor0o= github.com/snyk/go-httpauth v0.0.0-20230328170530-1af63c87b650/go.mod h1:88KbbvGYlmLgee4OcQ19yr0bNpXpOr2kciOthaSzCAg= github.com/snyk/snyk-iac-capture v0.6.0 h1:P9GWIyvl+F23XZOCuJvzGV6tME/vxbKpZM7/9dw48as=