Skip to content

Commit

Permalink
chore: add target id to instrumentation data (#5260)
Browse files Browse the repository at this point in the history
* chore: udpate gaf

* chore: udpate gaf to final version

* chore: add target id to instrumentation data

* chore: update gaf

* refactor: and test determinInputDirectory

* fix: linter

* chore: use configured repo-url for target id

* chore: update gaf to final commit

* chore: add missing test title
  • Loading branch information
PeterSchafer authored May 28, 2024
1 parent 056cdab commit c2c2672
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
11 changes: 10 additions & 1 deletion cliv2/cmd/cliv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cliv2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions cliv2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
17 changes: 17 additions & 0 deletions cliv2/internal/cliv2/cliv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 ""
}
17 changes: 16 additions & 1 deletion cliv2/internal/cliv2/cliv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})
}

0 comments on commit c2c2672

Please sign in to comment.