Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test:
go test -race -cover ./...

audit:
go list -m all | nancy sleuth
dis-vulncheck

lint:
exit
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ In order to use the `dp import cmd` sub-command (e.g. when you are using **Neo4j
git clone git@github.com:ONSdigital/dp-hierarchy-builder
```

### Tools

To run some of our tests you will need additional tooling:

#### Audit

We use `dis-vulncheck` to do auditing, which you will [need to install](https://github.com/ONSdigital/dis-vulncheck).

### Configuration

Configuration is defined in a YAML file:
Expand Down
2 changes: 1 addition & 1 deletion ci/audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ platform: linux
image_resource:
type: docker-image
source:
repository: onsdigital/dp-concourse-tools-nancy
repository: onsdigital/dp-concourse-tools-vulncheck
tag: latest

inputs:
Expand Down
2 changes: 1 addition & 1 deletion ci/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ image_resource:
type: docker-image
source:
repository: golang
tag: 1.24.1-bullseye
tag: 1.24.9-bookworm

inputs:
- name: dp-cli
Expand Down
2 changes: 1 addition & 1 deletion ci/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ image_resource:
type: docker-image
source:
repository: golang
tag: 1.24.1-bullseye
tag: 1.24.9-bookworm

inputs:
- name: dp-cli
Expand Down
2 changes: 1 addition & 1 deletion ci/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ image_resource:
type: docker-image
source:
repository: golang
tag: 1.24.1-bullseye
tag: 1.24.9-bookworm

inputs:
- name: dp-cli
Expand Down
8 changes: 2 additions & 6 deletions project_generation/content/templates/base-app/Makefile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ all: delimiter-AUDIT audit delimiter-LINTERS lint delimiter-UNIT-TESTS test deli

.PHONY: audit
audit: ## Runs checks for security vulnerabilities on dependencies (including transient ones)
go list -json -m all | nancy sleuth
dis-vulncheck

.PHONY: build
build: ## Builds binary of application code and stores in bin directory as {{.Name}}
Expand All @@ -41,11 +41,7 @@ fmt: ## Run Go formatting on code
go fmt ./...

.PHONY: lint
lint: ## Used in ci to run linters against Go code
golangci-lint run ./...

.PHONY: lint-local
lint-local: ## Use locally to run linters against Go code
lint:
golangci-lint run ./...

.PHONY: test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
| OTEL_BATCH_TIMEOUT | 5s | Timeout for OpenTelemetry
| OTEL_ENABLED | false | Feature flag to enable OpenTelemetry

### Tools

To run some of our tests you will need additional tooling:

#### Audit

We use `dis-vulncheck` to do auditing, which you will [need to install](https://github.com/ONSdigital/dis-vulncheck).

## Contributing

See [CONTRIBUTING](CONTRIBUTING.md) for details.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ platform: linux
image_resource:
type: docker-image
source:
repository: onsdigital/dp-concourse-tools-nancy
repository: onsdigital/dp-concourse-tools-vulncheck
tag: latest

inputs:
Expand Down
152 changes: 65 additions & 87 deletions project_generation/content/templates/controller/.golangci.yml.tmpl
Original file line number Diff line number Diff line change
@@ -1,107 +1,85 @@
# This file was inspired by the golangci-lint one:
# https://github.com/golangci/golangci-lint/blob/master/.golangci.yml
version: "2"
run:
# number of operating system threads that can execute golangci-lint simultaneously
concurrency: 4

# timeout for analysis, e.g. 30s, 5m, default is 1m
timeout: 5m
linters-settings:
govet:
shadow: true
golint:
min-confidence: 0
gocyclo:
min-complexity: 20
gocognit:
min-complexity: 40
maligned:
suggest-new: true
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
misspell:
locale: UK
lll:
line-length: 140
gofmt:
simplify: false
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- wrapperFunc
- dupImport # https://github.com/go-critic/go-critic/issues/845
- ifElseChain
- octalLiteral
- hugeParam
revive:
rules:
# disabled as parameter names are useful for context even when unused, especially in interface implementations
- name: unused-parameter
severity: warning
disabled: true
include:
- "**/*_test.go" # Specify test files to include
funlen:
# lines: 100
# statements: 100

linters:
# please, do not use `enable-all`: it's deprecated and will be removed soon.
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
disable-all: true
default: none
enable:
- dogsled
- errcheck
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- revive
- gosec
- gosimple
- govet
- ineffassign
- nakedret
- prealloc
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unused
- whitespace
- gocognit
- prealloc

settings:
dupl:
threshold: 100
gocognit:
min-complexity: 40
goconst:
min-len: 2
min-occurrences: 2
gocritic:
disabled-checks:
- wrapperFunc
- dupImport
- ifElseChain
- octalLiteral
- hugeParam
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
gocyclo:
min-complexity: 20
lll:
line-length: 140
misspell:
locale: UK
revive:
rules:
- name: unused-parameter
severity: warning
disabled: true
exclusions:
generated: lax
presets:
- comments
- common-false-positives
- legacy
- std-error-handling
rules:
- linters:
- dupl
- errcheck
- gocyclo
- gosec
path: _test\.go
- linters:
- revive
path: _test.go
text: dot-imports
source: github.com/smartystreets/goconvey/convey
issues:
exclude-rules:
- path: _test\.go
linters:
- gocyclo
- errcheck
- dupl
- gosec
- path: main.go
linters:
- typecheck # assets may not exist as they are auto generated
- path: utils/service.go
linters:
- typecheck # assets may not exist as they are auto generated
- path: features/steps/example_component.go
linters:
- typecheck # assets may not exist as they are auto generated
# Allow dot import in test files for goconvey
- path: _test.go
text: "dot-imports"
linters:
- revive
source: "github.com/smartystreets/goconvey/convey"
new: false
formatters:
enable:
- gofmt
- goimports
settings:
gofmt:
simplify: false
exclusions:
generated: lax
43 changes: 18 additions & 25 deletions project_generation/content/templates/controller/Makefile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,32 @@ RESET := $(shell tput -Txterm sgr0)
BUILD_TIME=$(shell date +%s)
GIT_COMMIT=$(shell git rev-parse HEAD)
VERSION ?= $(shell git tag --points-at HEAD | grep ^v | head -n 1)
LOCAL_DP_RENDERER_IN_USE = $(shell grep -c "\"github.com/ONSdigital/dp-renderer/v2\" =" go.mod)
LOCAL_RENDERER_IN_USE = $(shell grep -c "\"github.com/ONSdigital/dis-design-system-go\" =" go.mod)

CONFIG_PATH = github.com/ONSdigital/{{.Name}}/config
SERVICE_PATH = github.com/ONSdigital/{{.Name}}/service

LDFLAGS = -ldflags "-X $(SERVICE_PATH).BuildTime=$(BUILD_TIME) -X $(SERVICE_PATH).GitCommit=$(GIT_COMMIT) -X $(SERVICE_PATH).Version=$(VERSION)"
LDFLAGS_DEBUG = -ldflags "-X $(SERVICE_PATH).BuildTime=$(BUILD_TIME) -X $(SERVICE_PATH).GitCommit=$(GIT_COMMIT) -X $(SERVICE_PATH).Version=$(VERSION)"
LDFLAGS_BUILD = -ldflags "-X $(SERVICE_PATH).BuildTime=$(BUILD_TIME) -X $(SERVICE_PATH).GitCommit=$(GIT_COMMIT) -X $(SERVICE_PATH).Version=$(VERSION) -X $(CONFIG_PATH).RendererVersion=$(APP_RENDERER_VERSION)"

.PHONY: all
all: delimiter-AUDIT audit delimiter-LINTERS lint delimiter-UNIT_TESTS test delimiter-COMPONENT_TESTS test-component delimiter-FINISH ## Runs multiple targets, audit, lint, test and test-component

.PHONY: audit
audit: ## Runs checks for security vulnerabilities on dependencies (including transient ones)
go list -json -m all | nancy sleuth
audit: generate-prod ## Runs checks for security vulnerabilities on dependencies (including transient ones)
dis-vulncheck --build-tags=production

.PHONY: build
build: generate-prod ## Builds binary of application code and stores in bin directory as {{.Name}}
go build -tags 'production' $(LDFLAGS) -o $(BINPATH)/{{.Name}}
go build -tags 'production' $(LDFLAGS_BUILD) -o $(BINPATH)/{{.Name}}

.PHONY: convey
convey: ## Runs unit test suite and outputs results on http://127.0.0.1:8080/
goconvey ./...

.PHONY: debug
debug: generate-debug ## Used to run code locally in debug mode
go build -tags 'debug' $(LDFLAGS) -o $(BINPATH)/{{.Name}}
go build -tags 'debug' $(LDFLAGS_DEBUG) -o $(BINPATH)/{{.Name}}
HUMAN_LOG=1 DEBUG=1 $(BINPATH)/{{.Name}}

.PHONY: delimiter-%
Expand All @@ -44,17 +46,8 @@ fmt: ## Run Go formatting on code
go fmt ./...

.PHONY: lint
lint: ## Used in ci to run linters against Go code
cp assets/assets.go assets/assets.go.bak
echo 'func Asset(_ string) ([]byte, error) { return nil, nil }' >> assets/assets.go
echo 'func AssetNames() []string { return []string{} }' >> assets/assets.go
gofmt -w assets/assets.go
golangci-lint run ./... || { echo "Linting failed, restoring original assets.go"; mv assets/assets.go.bak assets/assets.go; exit 1; }
mv assets/assets.go.bak assets/assets.go

.PHONY: lint-local
lint-local: ## Use locally to run linters against Go code
golangci-lint run ./...
lint: generate-prod
golangci-lint run ./... --build-tags 'production'

.PHONY: test
test: generate-prod ## Runs unit tests including checks for race conditions and returns coverage
Expand All @@ -64,23 +57,23 @@ test: generate-prod ## Runs unit tests including checks for race conditions and
test-component: generate-prod ## Runs component test suite
go test -cover -tags 'production' -coverpkg=github.com/ONSdigital/dp-hello-world-controller/... -component

.PHONY: fetch-renderer-lib
fetch-renderer-lib:
ifeq ($(LOCAL_DP_RENDERER_IN_USE), 1)
$(eval CORE_ASSETS_PATH = $(shell grep -w "\"github.com/ONSdigital/dp-renderer/v2\" =>" go.mod | awk -F '=> ' '{print $$2}' | tr -d '"'))
.PHONY: fetch-renderer
fetch-renderer:
ifeq ($(LOCAL_RENDERER_IN_USE), 1)
$(eval CORE_ASSETS_PATH = $(shell grep -w "\"github.com/ONSdigital/dis-design-system-go\" =>" go.mod | awk -F '=> ' '{print $$2}' | tr -d '"'))
else
$(eval APP_RENDERER_VERSION=$(shell grep "github.com/ONSdigital/dp-renderer/v2" go.mod | cut -d ' ' -f2 ))
$(eval CORE_ASSETS_PATH = $(shell go get github.com/ONSdigital/dp-renderer/v2@$(APP_RENDERER_VERSION) && go list -f {{"'{{.Dir}}'"}} -m github.com/ONSdigital/dp-renderer/v2))
$(eval APP_RENDERER_VERSION=$(shell grep "github.com/ONSdigital/dis-design-system-go" go.mod | cut -d ' ' -f2 ))
$(eval CORE_ASSETS_PATH = $(shell go get github.com/ONSdigital/dis-design-system-go@$(APP_RENDERER_VERSION) && go list -f {{"'{{.Dir}}'"}} -m github.com/ONSdigital/dis-design-system-go))
endif

.PHONY: generate-debug
generate-debug: fetch-renderer-lib
generate-debug: fetch-renderer
cd assets; go run github.com/kevinburke/go-bindata/v4/...@latest -prefix $(CORE_ASSETS_PATH)/assets -debug -o data.go -pkg assets locales/... templates/... $(CORE_ASSETS_PATH)/assets/locales/... $(CORE_ASSETS_PATH)/assets/templates/...
{ echo "// +build debug\n"; cat assets/data.go; } > assets/debug.go.new
mv assets/debug.go.new assets/data.go

.PHONY: generate-prod
generate-prod: fetch-renderer-lib
generate-prod: fetch-renderer
cd assets; go run github.com/kevinburke/go-bindata/v4/...@latest -prefix $(CORE_ASSETS_PATH)/assets -o data.go -pkg assets locales/... templates/... $(CORE_ASSETS_PATH)/assets/locales/... $(CORE_ASSETS_PATH)/assets/templates/...
{ echo "// +build production\n"; cat assets/data.go; } > assets/data.go.new
mv assets/data.go.new assets/data.go
Expand Down
13 changes: 13 additions & 0 deletions project_generation/content/templates/controller/README.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@

## Getting started

* Run [`dis-design-system-go`](https://github.com/ONSdigital/dis-design-system-go) in a separate shell to generate static assets (css/js)
* Run `make debug` to run application on http://localhost:{{.Port}}
* Run * Run `make help` to see full list of make targets

### Dependencies

* No further dependencies other than those defined in `go.mod`

### Tools

To run some of our tests you will need additional tooling:

#### Audit

We use `dis-vulncheck` to do auditing, which you will [need to install](https://github.com/ONSdigital/dis-vulncheck).

#### Linting

We use v2 of golangci-lint, which you will [need to install](https://golangci-lint.run/docs/welcome/install).

### Configuration

| Environment variable | Default | Description
Expand Down
Loading
Loading