diff --git a/cliv2/cmd/cliv2/main.go b/cliv2/cmd/cliv2/main.go index 17d8b1f009..b8d04374a2 100644 --- a/cliv2/cmd/cliv2/main.go +++ b/cliv2/cmd/cliv2/main.go @@ -261,6 +261,11 @@ func help(_ *cobra.Command, _ []string) error { } func defaultCmd(args []string) error { + inputDirectory := cliv2.DetermineInputDirectory(args) + if len(inputDirectory) > 0 { + globalConfiguration.Set(configuration.INPUT_DIRECTORY, inputDirectory) + } + // prepare the invocation of the legacy CLI by // * enabling stdio // * by specifying the raw cmd args for it @@ -494,7 +499,6 @@ func MainWithErrorCode() int { cliAnalytics.GetInstrumentation().SetStage(instrumentation.DetermineStage(cliAnalytics.IsCiEnvironment())) cliAnalytics.GetInstrumentation().SetStatus(analytics.Success) - cliAnalytics.GetInstrumentation().SetTargetId("pkg:") // TODO use method when existing if !globalConfiguration.GetBool(configuration.ANALYTICS_DISABLED) { defer sendAnalytics(cliAnalytics, globalLogger) } @@ -525,6 +529,11 @@ func MainWithErrorCode() int { exitCode := cliv2.DeriveExitCode(err) globalLogger.Printf("Exiting with %d", exitCode) + targetId, targetIdError := instrumentation.GetTargetId(globalConfiguration.GetString(configuration.INPUT_DIRECTORY), instrumentation.AutoDetectedTargetId, instrumentation.WithConfiguredRepository(globalConfiguration)) + if targetIdError != nil { + globalLogger.Printf("Failed to derive target id, %v", targetIdError) + } + cliAnalytics.GetInstrumentation().SetTargetId(targetId) cliAnalytics.GetInstrumentation().SetDuration(time.Since(startTime)) cliAnalytics.GetInstrumentation().AddExtension("exitcode", exitCode) if exitCode == 2 { diff --git a/cliv2/go.mod b/cliv2/go.mod index 8f09593fc0..6c78095c5a 100644 --- a/cliv2/go.mod +++ b/cliv2/go.mod @@ -14,7 +14,7 @@ require ( github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672 github.com/snyk/cli-extension-sbom v0.0.0-20240523084359-a2830fadb001 github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 - github.com/snyk/go-application-framework v0.0.0-20240527123353-c7245f912464 + github.com/snyk/go-application-framework v0.0.0-20240528170035-ab13975bf74d github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 github.com/snyk/snyk-iac-capture v0.6.5 github.com/snyk/snyk-ls v0.0.0-20240524141328-ca2e2955918a @@ -149,7 +149,7 @@ require ( github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect github.com/shirou/gopsutil v3.21.11+incompatible // indirect github.com/skeema/knownhosts v1.2.2 // indirect - github.com/snyk/code-client-go v1.5.5 // indirect + github.com/snyk/code-client-go v1.6.1 // indirect github.com/snyk/error-catalog-golang-public v0.0.0-20240425141803-2516e42296c3 // indirect github.com/snyk/policy-engine v0.30.11 // indirect github.com/sourcegraph/conc v0.3.0 // indirect diff --git a/cliv2/go.sum b/cliv2/go.sum index c71914b33f..e67a60f28b 100644 --- a/cliv2/go.sum +++ b/cliv2/go.sum @@ -725,14 +725,14 @@ github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672 h1:Ak github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672/go.mod h1:2vKTUsW73sVbDcyD19iNLfN0so2GSu9BE3k/fqG0mjA= github.com/snyk/cli-extension-sbom v0.0.0-20240523084359-a2830fadb001 h1:EP9cL93+Lqw/wP/C80Sx+pyMYrqQY2NiuLDrad0lZ9w= github.com/snyk/cli-extension-sbom v0.0.0-20240523084359-a2830fadb001/go.mod h1:lqmQT+QdzLdfi7qsqIH4qvCsSWu+P09GDFwQcmFfC0g= -github.com/snyk/code-client-go v1.5.5 h1:YHgNvh17jVTuSvPVMoeNuDn40z1qb99EfXva/sSRCMw= -github.com/snyk/code-client-go v1.5.5/go.mod h1:Kkr7pQc8ItsBZSYd6A1S4r4VHO6HNyTWZsqi18sAtwQ= +github.com/snyk/code-client-go v1.6.1 h1:wCCcsTLsk5ZAUwwAHiU35/hmmYzJ5lgFbcJ1fOk6L/k= +github.com/snyk/code-client-go v1.6.1/go.mod h1:Kkr7pQc8ItsBZSYd6A1S4r4VHO6HNyTWZsqi18sAtwQ= github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 h1:9RKY9NdX5DrJAoVXDP0JiqrXT+4Nb9NH8pjEcA0NsLA= github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1/go.mod h1:38w+dcAQp9eG3P5t2eNS9eG0reut10AeJjLv5lJ5lpM= github.com/snyk/error-catalog-golang-public v0.0.0-20240425141803-2516e42296c3 h1:ZUaY5LIVGQ0GScf1SsaqvUxaiGbBKgBBLsQUgB4Zx5o= github.com/snyk/error-catalog-golang-public v0.0.0-20240425141803-2516e42296c3/go.mod h1:Ytttq7Pw4vOCu9NtRQaOeDU2dhBYUyNBe6kX4+nIIQ4= -github.com/snyk/go-application-framework v0.0.0-20240527123353-c7245f912464 h1:ObGPhWY5v1lCfoM+hNbRKSem1jzf8Lg7dNVIDzdQgXE= -github.com/snyk/go-application-framework v0.0.0-20240527123353-c7245f912464/go.mod h1:5gvF6i1sqmk1oN21ZbX/EDaEyQtPMfK1pbYCwtS+Fvw= +github.com/snyk/go-application-framework v0.0.0-20240528170035-ab13975bf74d h1:OChBTTRBxQe455GQ0Sbyh6KybHCkk6bQltMCdURUeYc= +github.com/snyk/go-application-framework v0.0.0-20240528170035-ab13975bf74d/go.mod h1:ZMl89wenxt+xaRtHvI2uvWwVJjFPv3b5qzT3w+batgQ= github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 h1:CEQuYv0Go6MEyRCD3YjLYM2u3Oxkx8GpCpFBd4rUTUk= github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65/go.mod h1:88KbbvGYlmLgee4OcQ19yr0bNpXpOr2kciOthaSzCAg= github.com/snyk/policy-engine v0.30.11 h1:wUy5LMar2vccMbNM62MSBRdjAQAhAbIm7aNXXO+g2tk= diff --git a/cliv2/internal/cliv2/cliv2.go b/cliv2/internal/cliv2/cliv2.go index fec6c23473..2e78794e2a 100644 --- a/cliv2/internal/cliv2/cliv2.go +++ b/cliv2/internal/cliv2/cliv2.go @@ -14,11 +14,13 @@ import ( "os/exec" "path" "regexp" + "slices" "strings" "time" "github.com/gofrs/flock" "github.com/snyk/go-application-framework/pkg/configuration" + "github.com/snyk/go-application-framework/pkg/instrumentation" "github.com/snyk/go-application-framework/pkg/utils" cli_errors "github.com/snyk/cli/cliv2/internal/errors" @@ -472,3 +474,18 @@ func (c *CLI) SetIoStreams(stdin io.Reader, stdout io.Writer, stderr io.Writer) c.stdout = stdout c.stderr = stderr } + +func DetermineInputDirectory(args []string) string { + for _, v := range args { + if v == "--" { + break + } + + isCommand := slices.Contains(instrumentation.KNOWN_COMMANDS, v) + isFlag := strings.HasPrefix(v, "-") + if !isCommand && !isFlag { + return v + } + } + return "" +} diff --git a/cliv2/internal/cliv2/cliv2_test.go b/cliv2/internal/cliv2/cliv2_test.go index 609fa6487d..36039e2603 100644 --- a/cliv2/internal/cliv2/cliv2_test.go +++ b/cliv2/internal/cliv2/cliv2_test.go @@ -13,9 +13,10 @@ import ( "testing" "time" - cli_errors "github.com/snyk/cli/cliv2/internal/errors" "github.com/snyk/go-application-framework/pkg/configuration" + cli_errors "github.com/snyk/cli/cliv2/internal/errors" + "github.com/snyk/cli/cliv2/internal/cliv2" "github.com/snyk/cli/cliv2/internal/constants" "github.com/snyk/cli/cliv2/internal/proxy" @@ -506,3 +507,17 @@ func TestDeriveExitCode(t *testing.T) { }) } } + +func Test_determineInputDirectory(t *testing.T) { + t.Run("input directory given as positional argument", func(t *testing.T) { + expected := "./myfolder/somewhere/" + actual := cliv2.DetermineInputDirectory([]string{"iac", "test", "--remote-repo-url=something ", expected, " -d", "--project-tags=t1,t2", "--", "-DVerbose", "true", "/somefolder/here.file"}) + assert.Equal(t, expected, actual) + }) + + t.Run("no input directory specified in arguments", func(t *testing.T) { + expected := "" + actual := cliv2.DetermineInputDirectory([]string{"iac", "test", "--remote-repo-url=something", "-d", "--project-tags=t1,t2", "--", "-DVerbose", "true", "/somefolder/here.file"}) + assert.Equal(t, expected, actual) + }) +}