diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..6c65571 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,37 @@ +run: + timeout: 10m + +linters: + enable: + - deadcode + - depguard + - gofmt + - goimports + - revive + - govet + - importas + - ineffassign + - misspell + - typecheck + - varcheck + - errname + - makezero + - whitespace + disable-all: true + +linters-settings: + depguard: + list-type: blacklist + include-go-root: true + packages: + # The io/ioutil package has been deprecated. + # https://go.dev/doc/go1.16#ioutil + - io/ioutil + importas: + no-unaliased: true + +issues: + exclude-rules: + - linters: + - revive + text: "stutters" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f0e2739 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,86 @@ +# syntax=docker/dockerfile:1 + +# Copyright 2021 cli-docs-tool authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +ARG GO_VERSION="1.18" +ARG GOLANGCI_LINT_VERSION="v1.45" +ARG ADDLICENSE_VERSION="v1.0.0" + +ARG LICENSE_ARGS="-c cli-docs-tool -l apache" +ARG LICENSE_FILES=".*\(Dockerfile\|\.go\|\.hcl\|\.sh\)" + +FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION}-alpine AS golangci-lint +FROM ghcr.io/google/addlicense:${ADDLICENSE_VERSION} AS addlicense + +FROM golang:${GO_VERSION}-alpine AS base +RUN apk add --no-cache cpio findutils git linux-headers +ENV CGO_ENABLED=0 +WORKDIR /src + +FROM base AS vendored +RUN --mount=type=bind,target=.,rw \ + --mount=type=cache,target=/go/pkg/mod \ + go mod tidy && go mod download && \ + mkdir /out && cp go.mod go.sum /out + +FROM scratch AS vendor-update +COPY --from=vendored /out / + +FROM vendored AS vendor-validate +RUN --mount=type=bind,target=.,rw <&2 'ERROR: Vendor result differs. Please vendor your package with "docker buildx bake vendor"' + echo "$diff" + exit 1 +fi +EOT + +FROM base AS lint +RUN --mount=type=bind,target=. \ + --mount=type=cache,target=/root/.cache \ + --mount=from=golangci-lint,source=/usr/bin/golangci-lint,target=/usr/bin/golangci-lint \ + golangci-lint run ./... + +FROM base AS license-set +ARG LICENSE_ARGS +ARG LICENSE_FILES +RUN --mount=type=bind,target=.,rw \ + --mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \ + find . -regex "${LICENSE_FILES}" | xargs addlicense ${LICENSE_ARGS} \ + && mkdir /out \ + && find . -regex "${LICENSE_FILES}" | cpio -pdm /out + +FROM scratch AS license-update +COPY --from=set /out / + +FROM base AS license-validate +ARG LICENSE_ARGS +ARG LICENSE_FILES +RUN --mount=type=bind,target=. \ + --mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \ + find . -regex "${LICENSE_FILES}" | xargs addlicense -check ${LICENSE_ARGS} + +FROM vendored AS test +RUN --mount=type=bind,target=. \ + --mount=type=cache,target=/root/.cache \ + --mount=type=cache,target=/go/pkg/mod \ + go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic ./... + +FROM scratch AS test-coverage +COPY --from=test /tmp/coverage.txt /coverage.txt diff --git a/docker-bake.hcl b/docker-bake.hcl index e5ed7b7..4a5f44f 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -12,10 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -variable "GO_VERSION" { - default = "1.16" -} - group "default" { targets = ["test"] } @@ -25,49 +21,31 @@ group "validate" { } target "lint" { - args = { - GO_VERSION = GO_VERSION - } - dockerfile = "./hack/lint.Dockerfile" target = "lint" output = ["type=cacheonly"] } target "vendor-validate" { - args = { - GO_VERSION = GO_VERSION - } - dockerfile = "./hack/vendor.Dockerfile" - target = "validate" + target = "vendor-validate" output = ["type=cacheonly"] } target "vendor-update" { - args = { - GO_VERSION = GO_VERSION - } - dockerfile = "./hack/vendor.Dockerfile" - target = "update" + target = "vendor-update" output = ["."] } target "test" { - args = { - GO_VERSION = GO_VERSION - } - dockerfile = "./hack/test.Dockerfile" target = "test-coverage" output = ["."] } target "license-validate" { - dockerfile = "./hack/license.Dockerfile" - target = "validate" + target = "license-validate" output = ["type=cacheonly"] } target "license-update" { - dockerfile = "./hack/license.Dockerfile" - target = "update" + target = "license-update" output = ["."] } diff --git a/go.mod b/go.mod index 9252b68..effe310 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/docker/cli-docs-tool -go 1.16 +go 1.18 require ( github.com/spf13/cobra v1.2.1 @@ -8,3 +8,10 @@ require ( github.com/stretchr/testify v1.7.0 gopkg.in/yaml.v2 v2.4.0 ) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect +) diff --git a/hack/license.Dockerfile b/hack/license.Dockerfile deleted file mode 100644 index 06ffb45..0000000 --- a/hack/license.Dockerfile +++ /dev/null @@ -1,43 +0,0 @@ -# syntax=docker/dockerfile:1.3 - -# Copyright 2021 cli-docs-tool authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -ARG LICENSE_ARGS="-c cli-docs-tool -l apache" -ARG LICENSE_FILES=".*\(Dockerfile\|\.go\|\.hcl\|\.sh\)" - -FROM ghcr.io/google/addlicense:v1.0.0 AS addlicense - -FROM alpine:3.14 AS base -WORKDIR /src -RUN apk add --no-cache cpio findutils git - -FROM base AS set -ARG LICENSE_ARGS -ARG LICENSE_FILES -RUN --mount=type=bind,target=.,rw \ - --mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \ - find . -regex "${LICENSE_FILES}" | xargs addlicense ${LICENSE_ARGS} \ - && mkdir /out \ - && find . -regex "${LICENSE_FILES}" | cpio -pdm /out - -FROM scratch AS update -COPY --from=set /out / - -FROM base AS validate -ARG LICENSE_ARGS -ARG LICENSE_FILES -RUN --mount=type=bind,target=. \ - --mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \ - find . -regex "${LICENSE_FILES}" | xargs addlicense -check ${LICENSE_ARGS} diff --git a/hack/lint.Dockerfile b/hack/lint.Dockerfile deleted file mode 100644 index 458c10c..0000000 --- a/hack/lint.Dockerfile +++ /dev/null @@ -1,31 +0,0 @@ -# syntax=docker/dockerfile:1.3 - -# Copyright 2021 cli-docs-tool authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -ARG GO_VERSION - -FROM golang:${GO_VERSION}-alpine AS base -RUN apk add --no-cache linux-headers -ENV CGO_ENABLED=0 -WORKDIR /src - -FROM golangci/golangci-lint:v1.37-alpine AS golangci-lint - -FROM base AS lint -RUN --mount=type=bind,target=. \ - --mount=type=cache,target=/root/.cache/go-build \ - --mount=type=cache,target=/root/.cache/golangci-lint \ - --mount=from=golangci-lint,source=/usr/bin/golangci-lint,target=/usr/bin/golangci-lint \ - golangci-lint run --timeout 10m0s ./... diff --git a/hack/test.Dockerfile b/hack/test.Dockerfile deleted file mode 100644 index 6530c27..0000000 --- a/hack/test.Dockerfile +++ /dev/null @@ -1,36 +0,0 @@ -# syntax=docker/dockerfile:1.3 - -# Copyright 2021 cli-docs-tool authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -ARG GO_VERSION - -FROM golang:${GO_VERSION}-alpine AS base -RUN apk add --no-cache gcc linux-headers musl-dev -ENV CGO_ENABLED=0 -WORKDIR /src - -FROM base AS gomod -RUN --mount=type=bind,target=.,rw \ - --mount=type=cache,target=/go/pkg/mod \ - go mod tidy && go mod download - -FROM gomod AS test -RUN --mount=type=bind,target=. \ - --mount=type=cache,target=/go/pkg/mod \ - --mount=type=cache,target=/root/.cache/go-build \ - go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic ./... - -FROM scratch AS test-coverage -COPY --from=test /tmp/coverage.txt /coverage.txt diff --git a/hack/vendor.Dockerfile b/hack/vendor.Dockerfile deleted file mode 100644 index db0cf85..0000000 --- a/hack/vendor.Dockerfile +++ /dev/null @@ -1,39 +0,0 @@ -# syntax=docker/dockerfile:1.3 - -# Copyright 2021 cli-docs-tool authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -ARG GO_VERSION - -FROM golang:${GO_VERSION}-alpine AS base -RUN apk add --no-cache git -WORKDIR /src - -FROM base AS vendored -RUN --mount=type=bind,target=.,rw \ - --mount=type=cache,target=/go/pkg/mod \ - go mod tidy && go mod download && \ - mkdir /out && cp go.mod go.sum /out - -FROM scratch AS update -COPY --from=vendored /out / - -FROM vendored AS validate -RUN --mount=type=bind,target=.,rw \ - git add -A && cp -rf /out/* .; \ - if [ -n "$(git status --porcelain -- go.mod go.sum)" ]; then \ - echo >&2 'ERROR: Vendor result differs. Please vendor your package with "docker buildx bake vendor-update"'; \ - git status --porcelain -- go.mod go.sum; \ - exit 1; \ - fi