diff --git a/Makefile b/Makefile index b4bd76b7..be65c115 100644 --- a/Makefile +++ b/Makefile @@ -12,11 +12,11 @@ PACT_CLI_TARGETS := $(TOOLS_BIN)/pact/bin/pact-broker $(TOOLS_BIN)/.pact_$(PACT_ PACT_GO_V := v2.4.1 PACT_GO_LIB_TARGETS := /tmp/.libpact-ffi_$(PACT_GO_V) # Only use a marker file since lib extension is either .so or .dll -SHELL:=env PATH=$(TOOLS_BIN)/go:$(TOOLS_BIN)/pact/bin:$(PATH) $(SHELL) +SHELL:=env PATH=$(TOOLS_BIN)/pact/bin:$(PATH) $(SHELL) ## tools: Install required tooling. .PHONY: tools -tools: $(TOOLS_BIN)/go $(GOCI_LINT_TARGETS) $(PACT_CLI_TARGETS) $(PACT_GO_LIB_TARGETS) +tools: $(GOCI_LINT_TARGETS) $(PACT_CLI_TARGETS) $(PACT_GO_LIB_TARGETS) $(TOOLS_BIN): @mkdir -p $(TOOLS_BIN) @@ -31,13 +31,9 @@ $(PACT_CLI_TARGETS): $(TOOLS_BIN) @cd $(TOOLS_BIN); curl -fsSL https://raw.githubusercontent.com/pact-foundation/pact-ruby-standalone/master/install.sh | PACT_CLI_VERSION=$(PACT_CLI_V) bash; cd ../ @touch $(TOOLS_BIN)/.pact_$(PACT_CLI_V) -$(TOOLS_BIN)/go: - mkdir -p ${TOOLS_BIN}/go - @cat tools.go | grep _ | awk -F'"' '{print $$2}' | xargs -tI % sh -c 'GOBIN=${TOOLS_BIN}/go go install %' - -$(PACT_GO_LIB_TARGETS): $(TOOLS_BIN)/go +$(PACT_GO_LIB_TARGETS): @rm -f /tmp/.libpact-ffi_* - @GOBIN=${TOOLS_BIN}/go ${TOOLS_BIN}/go/pact-go -l DEBUG install -d /tmp + @go tool github.com/pact-foundation/pact-go/v2 -l DEBUG install -d /tmp @touch /tmp/.libpact-ffi_$(PACT_GO_V) .PHONY: format @@ -101,11 +97,11 @@ generate: endif .PHONY: generate-mocks -generate-mocks: $(TOOLS_BIN)/go/mockgen +generate-mocks: @go generate -tags MOCK ./... .PHONY: generate-apis -generate-apis: $(TOOLS_BIN)/go/oapi-codegen download-apis +generate-apis: download-apis @go generate -tags API,!MOCK ./... .PHONY: download-apis diff --git a/bundle/bundle.go b/bundle/bundle.go index fee8fceb..f62910e1 100644 --- a/bundle/bundle.go +++ b/bundle/bundle.go @@ -19,13 +19,15 @@ package bundle import ( "context" "fmt" + "github.com/rs/zerolog" "github.com/snyk/code-client-go/internal/deepcode" "github.com/snyk/code-client-go/observability" ) -//go:generate mockgen -destination=mocks/bundle.go -source=bundle.go -package mocks +//go:generate go tool github.com/golang/mock/mockgen -destination=mocks/bundle.go -source=bundle.go -package mocks + type Bundle interface { UploadBatch(ctx context.Context, requestId string, batch *Batch) error GetBundleHash() string diff --git a/bundle/bundle_manager.go b/bundle/bundle_manager.go index e068e1a3..0053c6c3 100644 --- a/bundle/bundle_manager.go +++ b/bundle/bundle_manager.go @@ -21,16 +21,17 @@ import ( "os" "path/filepath" - "github.com/snyk/code-client-go/scan" - "github.com/puzpuzpuz/xsync" "github.com/rs/zerolog" "github.com/snyk/code-client-go/internal/deepcode" "github.com/snyk/code-client-go/internal/util" "github.com/snyk/code-client-go/observability" + "github.com/snyk/code-client-go/scan" ) +//go:generate go tool github.com/golang/mock/mockgen -destination=mocks/bundle_manager.go -source=bundle_manager.go -package mocks + type bundleManager struct { deepcodeClient deepcode.DeepcodeClient instrumentor observability.Instrumentor @@ -41,7 +42,6 @@ type bundleManager struct { supportedConfigFiles *xsync.MapOf[string, bool] } -//go:generate mockgen -destination=mocks/bundle_manager.go -source=bundle_manager.go -package mocks type BundleManager interface { Create(ctx context.Context, requestId string, diff --git a/config/config.go b/config/config.go index 2815ef44..9d4651c9 100644 --- a/config/config.go +++ b/config/config.go @@ -2,9 +2,9 @@ package config import "time" +//go:generate go tool github.com/golang/mock/mockgen -destination=mocks/config.go -source=config.go -package mocks + // Config defines the configurable options for the HTTP client. -// -//go:generate mockgen -destination=mocks/config.go -source=config.go -package mocks type Config interface { // Organization is the Snyk organization in which code SAST is being run. diff --git a/go.mod b/go.mod index ff441917..f5a67964 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/golang/mock v1.6.0 github.com/google/uuid v1.6.0 github.com/hexops/gotextdiff v1.0.3 - github.com/oapi-codegen/oapi-codegen/v2 v2.4.1 github.com/oapi-codegen/runtime v1.1.1 github.com/pact-foundation/pact-go/v2 v2.4.1 github.com/pkg/errors v0.9.1 @@ -44,6 +43,7 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect + github.com/oapi-codegen/oapi-codegen/v2 v2.4.1 // indirect github.com/perimeterx/marshmallow v1.1.5 // indirect github.com/pjbgf/sha1cd v0.3.2 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect @@ -68,3 +68,9 @@ require ( gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +tool ( + github.com/golang/mock/mockgen + github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen + github.com/pact-foundation/pact-go/v2 +) diff --git a/http/http.go b/http/http.go index 7b6d5495..bff44dcf 100644 --- a/http/http.go +++ b/http/http.go @@ -28,7 +28,8 @@ import ( "github.com/snyk/code-client-go/observability" ) -//go:generate mockgen -destination=mocks/http.go -source=http.go -package mocks +//go:generate go tool github.com/golang/mock/mockgen -destination=mocks/http.go -source=http.go -package mocks + type HTTPClient interface { Do(req *http.Request) (*http.Response, error) } diff --git a/internal/analysis/analysis.go b/internal/analysis/analysis.go index a86bc9ad..b1ef3dab 100644 --- a/internal/analysis/analysis.go +++ b/internal/analysis/analysis.go @@ -42,7 +42,8 @@ import ( "github.com/snyk/code-client-go/scan" ) -//go:generate mockgen -destination=mocks/analysis.go -source=analysis.go -package mocks +//go:generate go tool github.com/golang/mock/mockgen -destination=mocks/analysis.go -source=analysis.go -package mocks + type AnalysisOrchestrator interface { RunTest(ctx context.Context, orgId string, b bundle.Bundle, target scan.Target, reportingOptions AnalysisConfig) (*sarif.SarifResponse, *scan.ResultMetaData, error) RunTestRemote(ctx context.Context, orgId string, reportingOptions AnalysisConfig) (*sarif.SarifResponse, *scan.ResultMetaData, error) diff --git a/internal/api/test/2024-12-21/gen.go b/internal/api/test/2024-12-21/gen.go index 1f7df813..ea7f39fe 100644 --- a/internal/api/test/2024-12-21/gen.go +++ b/internal/api/test/2024-12-21/gen.go @@ -3,8 +3,8 @@ package v20241221 -//go:generate oapi-codegen --config common/common.config.yaml common/common.yaml -//go:generate oapi-codegen --config parameters/orgs.config.yaml parameters/orgs.yaml -//go:generate oapi-codegen --config parameters/tests.config.yaml parameters/tests.yaml -//go:generate oapi-codegen --config models/tests.config.yaml models/tests.yaml -//go:generate oapi-codegen --config spec.config.yaml spec.yaml +//go:generate go tool github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config common/common.config.yaml common/common.yaml +//go:generate go tool github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config parameters/orgs.config.yaml parameters/orgs.yaml +//go:generate go tool github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config parameters/tests.config.yaml parameters/tests.yaml +//go:generate go tool github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config models/tests.config.yaml models/tests.yaml +//go:generate go tool github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config spec.config.yaml spec.yaml diff --git a/internal/deepcode/client.go b/internal/deepcode/client.go index f5d49829..6b8f4bf3 100644 --- a/internal/deepcode/client.go +++ b/internal/deepcode/client.go @@ -36,7 +36,8 @@ import ( "github.com/snyk/code-client-go/observability" ) -//go:generate mockgen -destination=mocks/client.go -source=client.go -package mocks +//go:generate go tool github.com/golang/mock/mockgen -destination=mocks/client.go -source=client.go -package mocks + type DeepcodeClient interface { GetFilters(ctx context.Context) ( filters FiltersResponse, diff --git a/observability/error_reporter.go b/observability/error_reporter.go index 0e52cf28..5ba8f427 100644 --- a/observability/error_reporter.go +++ b/observability/error_reporter.go @@ -1,6 +1,7 @@ package observability -//go:generate mockgen -destination=mocks/error_reporter.go -source=error_reporter.go -package mocks +//go:generate go tool github.com/golang/mock/mockgen -destination=mocks/error_reporter.go -source=error_reporter.go -package mocks + type ErrorReporter interface { FlushErrorReporting() CaptureError(err error, options ErrorReporterOptions) bool diff --git a/observability/instrumentor.go b/observability/instrumentor.go index 4f97fedd..6c5b9037 100644 --- a/observability/instrumentor.go +++ b/observability/instrumentor.go @@ -4,9 +4,9 @@ import ( "context" ) +//go:generate go tool github.com/golang/mock/mockgen -destination=mocks/instrumentor.go -source=instrumentor.go -package mocks + // Instrumentor exposes functions used for adding instrumentation context to functions. -// -//go:generate mockgen -destination=mocks/instrumentor.go -source=instrumentor.go -package mocks type Instrumentor interface { StartSpan(ctx context.Context, operation string) Span NewTransaction( @@ -18,8 +18,6 @@ type Instrumentor interface { } // Span exposes functions that have context about functions. -// -//go:generate mockgen -destination=mocks/instrumentor.go -source=instrumentor.go -package mocks type Span interface { SetTransactionName(name string) StartSpan(ctx context.Context) diff --git a/scan/tracker.go b/scan/tracker.go index acaa41d0..fe46a0cf 100644 --- a/scan/tracker.go +++ b/scan/tracker.go @@ -15,7 +15,8 @@ */ package scan -//go:generate mockgen -destination=mocks/tracker.go -source=tracker.go -package mocks +//go:generate go tool github.com/golang/mock/mockgen -destination=mocks/tracker.go -source=tracker.go -package mocks + type TrackerFactory interface { GenerateTracker() Tracker } diff --git a/tools.go b/tools.go deleted file mode 100644 index 995604fb..00000000 --- a/tools.go +++ /dev/null @@ -1,9 +0,0 @@ -//go:build tools - -package codeClient - -import ( - _ "github.com/golang/mock/mockgen" - _ "github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen" - _ "github.com/pact-foundation/pact-go/v2" -)