Skip to content

Commit

Permalink
chore: write errors as json if —json is set
Browse files Browse the repository at this point in the history
and re-use global constants instead of strings
  • Loading branch information
PeterSchafer committed Apr 4, 2023
1 parent 5508ee0 commit 4a9dcb7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
24 changes: 21 additions & 3 deletions cliv2/cmd/cliv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cliv2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cliv2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 4a9dcb7

Please sign in to comment.