fix(cli): ensure correct command is picked by telemetry (#9260)

This commit is contained in:
Owen Rumney
2025-07-30 11:33:45 +01:00
committed by GitHub
parent ed4640ec27
commit b4ad00f301
3 changed files with 7 additions and 7 deletions

View File

@@ -47,6 +47,7 @@ const (
TargetRepository TargetKind = "repo"
TargetSBOM TargetKind = "sbom"
TargetVM TargetKind = "vm"
TargetK8s TargetKind = "k8s"
)
var (
@@ -113,7 +114,7 @@ func WithInitializeService(f InitializeScanService) RunnerOption {
// NewRunner initializes Runner that provides scanning functionalities.
// It is possible to return SkipScan and it must be handled by caller.
func NewRunner(ctx context.Context, cliOptions flag.Options, opts ...RunnerOption) (Runner, error) {
func NewRunner(ctx context.Context, cliOptions flag.Options, targetKind TargetKind, opts ...RunnerOption) (Runner, error) {
r := &runner{}
for _, opt := range opts {
opt(r)
@@ -125,9 +126,8 @@ func NewRunner(ctx context.Context, cliOptions flag.Options, opts ...RunnerOptio
Timeout: cliOptions.Timeout,
TraceHTTP: cliOptions.TraceHTTP,
}))
// get the sub command that is being used or fallback to "trivy"
commandName := lo.NthOr(os.Args, 1, "trivy")
r.versionChecker = notification.NewVersionChecker(commandName, &cliOptions)
r.versionChecker = notification.NewVersionChecker(string(targetKind), &cliOptions)
// Update the vulnerability database if needed.
if err := r.initDB(ctx, cliOptions); err != nil {
@@ -421,7 +421,7 @@ func Run(ctx context.Context, opts flag.Options, targetKind TargetKind) (err err
}
func run(ctx context.Context, opts flag.Options, targetKind TargetKind) (types.Report, error) {
r, err := NewRunner(ctx, opts)
r, err := NewRunner(ctx, opts, targetKind)
if err != nil {
if errors.Is(err, SkipScan) {
return types.Report{}, nil

View File

@@ -62,7 +62,7 @@ func newRunner(flagOpts flag.Options, cluster string) *runner {
}
func (r *runner) run(ctx context.Context, artifacts []*k8sArtifacts.Artifact) error {
runner, err := cmd.NewRunner(ctx, r.flagOpts)
runner, err := cmd.NewRunner(ctx, r.flagOpts, cmd.TargetK8s)
if err != nil {
if errors.Is(err, cmd.SkipScan) {
return nil

View File

@@ -276,7 +276,7 @@ func TestScanner_Scan(t *testing.T) {
ctx := t.Context()
uuid.SetFakeUUID(t, "3ff14136-e09f-4df9-80ea-%012d")
runner, err := cmd.NewRunner(ctx, flagOpts)
runner, err := cmd.NewRunner(ctx, flagOpts, cmd.TargetK8s)
require.NoError(t, err)
scanner := NewScanner(tt.clusterName, runner, flagOpts)