From c4905fb6c92d030b568fd310eae4d9d239ae5c81 Mon Sep 17 00:00:00 2001 From: Luke Watts Date: Fri, 13 Dec 2024 11:50:36 +0100 Subject: [PATCH] spike: transfer data via filesystem for TS <=> Go --- cliv2/cmd/cliv2/main.go | 45 +++++++++++++++++++++----- cliv2/pkg/basic_workflows/legacycli.go | 5 ++- src/cli/commands/test/index.ts | 11 +------ src/cli/main.ts | 6 ++++ 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/cliv2/cmd/cliv2/main.go b/cliv2/cmd/cliv2/main.go index 0a02fb75c3..c6394cab7e 100644 --- a/cliv2/cmd/cliv2/main.go +++ b/cliv2/cmd/cliv2/main.go @@ -22,19 +22,19 @@ import ( "github.com/snyk/cli-extension-sbom/pkg/sbom" "github.com/snyk/cli/cliv2/internal/cliv2" "github.com/snyk/cli/cliv2/internal/constants" + cli_errors "github.com/snyk/cli/cliv2/internal/errors" + "github.com/snyk/cli/cliv2/pkg/basic_workflows" "github.com/snyk/container-cli/pkg/container" + "github.com/snyk/error-catalog-golang-public/snyk_errors" "github.com/snyk/go-application-framework/pkg/analytics" "github.com/snyk/go-application-framework/pkg/app" "github.com/snyk/go-application-framework/pkg/configuration" "github.com/snyk/go-application-framework/pkg/instrumentation" - "github.com/snyk/go-application-framework/pkg/local_workflows/network_utils" - "github.com/snyk/go-application-framework/pkg/local_workflows/output_workflow" - "github.com/spf13/cobra" - "github.com/spf13/pflag" - localworkflows "github.com/snyk/go-application-framework/pkg/local_workflows" "github.com/snyk/go-application-framework/pkg/local_workflows/content_type" "github.com/snyk/go-application-framework/pkg/local_workflows/json_schemas" + "github.com/snyk/go-application-framework/pkg/local_workflows/network_utils" + "github.com/snyk/go-application-framework/pkg/local_workflows/output_workflow" "github.com/snyk/go-application-framework/pkg/networking" "github.com/snyk/go-application-framework/pkg/runtimeinfo" "github.com/snyk/go-application-framework/pkg/ui" @@ -42,11 +42,10 @@ import ( "github.com/snyk/go-application-framework/pkg/workflow" "github.com/snyk/go-httpauth/pkg/httpauth" "github.com/snyk/snyk-iac-capture/pkg/capture" + "github.com/spf13/cobra" + "github.com/spf13/pflag" snykls "github.com/snyk/snyk-ls/ls_extension" - - cli_errors "github.com/snyk/cli/cliv2/internal/errors" - "github.com/snyk/cli/cliv2/pkg/basic_workflows" ) var internalOS string @@ -621,6 +620,36 @@ func MainWithErrorCode() int { cliAnalytics.GetInstrumentation().SetStatus(analytics.Failure) } + // Load error out of run-errors.json + filePath := globalConfiguration.GetString(configuration.TEMP_DIR_PATH) + "/typescript-runtime-errors" + + // Read the entire file into a byte slice + errDataFromFile, err := os.ReadFile(filePath) + if err != nil { + globalLogger.Printf("Failed to read file: %v", err) + } + + // Convert the byte slice to a string and print it + fmt.Println(`LADEBUG`) + fmt.Println(string(errDataFromFile)) + cliError := snyk_errors.Error{ + ID: uuid.NewString(), + Title: "Unable to set environment", + Description: "The specified environment cannot be used. As a result, the configuration remains unchanged.Provide the correct specifications for the environment and try again.", + StatusCode: 200, + ErrorCode: string(errDataFromFile), + Classification: "ACTIONABLE", + Links: []string{ + "https://docs.snyk.io/snyk-cli/commands/config-environment", + }, + Level: "error", + Detail: string(errDataFromFile), + } + cliAnalytics.GetInstrumentation().AddError( + cliError, + ) + // END error loading + if !globalConfiguration.GetBool(configuration.ANALYTICS_DISABLED) { sendAnalytics(cliAnalytics, globalLogger) } diff --git a/cliv2/pkg/basic_workflows/legacycli.go b/cliv2/pkg/basic_workflows/legacycli.go index bae4e7e1f9..41a2a95603 100644 --- a/cliv2/pkg/basic_workflows/legacycli.go +++ b/cliv2/pkg/basic_workflows/legacycli.go @@ -110,7 +110,10 @@ func legacycliWorkflow( if len(apiToken) == 0 { apiToken = "random" } - cli.AppendEnvironmentVariables([]string{constants.SNYK_API_TOKEN_ENV + "=" + apiToken}) + cli.AppendEnvironmentVariables([]string{ + constants.SNYK_API_TOKEN_ENV + "=" + apiToken, + "SNYK_TEMP_DIR_PATH=" + config.GetString(configuration.TEMP_DIR_PATH), + }) err = cli.Init() if err != nil { diff --git a/src/cli/commands/test/index.ts b/src/cli/commands/test/index.ts index 39a28b9f32..f3d0844cc4 100644 --- a/src/cli/commands/test/index.ts +++ b/src/cli/commands/test/index.ts @@ -295,16 +295,7 @@ export default async function test( if (notSuccess) { response += chalk.bold.red(summaryMessage); - const error = new Error(response) as any; - // take the code of the first problem to go through error - // translation - // HACK as there can be different errors, and we pass only the - // first one - error.code = errorResults[0].code; - error.userMessage = errorResults[0].userMessage; - error.strCode = errorResults[0].strCode; - error.innerError = errorResults[0].innerError; - throw error; + throw errorResults[0]; } if (foundVulnerabilities) { diff --git a/src/cli/main.ts b/src/cli/main.ts index b03d6f1d2c..e831a4895d 100755 --- a/src/cli/main.ts +++ b/src/cli/main.ts @@ -195,6 +195,12 @@ async function handleError(args, error) { analytics.add('error', true); analytics.add('command', args.command); } else { + const errorFilePath = pathLib.join(process.env.SNYK_TEMP_DIR_PATH || getFullPath(''), './typescript-runtime-errors'); + + // TODO: If error is NOT valid error catalog message transform to match structure + await saveJsonResultsToFile(JSON.stringify( + analyticsError, + ), errorFilePath); analytics.add('error-message', analyticsError.message); // Note that error.stack would also contain the error message // (see https://nodejs.org/api/errors.html#errors_error_stack)