From 4e9a4185af2054f914c8e2d33d7c64f867dcc1e6 Mon Sep 17 00:00:00 2001 From: Djordje Lukic Date: Mon, 4 May 2020 23:50:00 +0200 Subject: [PATCH] Add `make lint` and run it on CI --- .github/workflows/ci.yml | 14 +++----- .golangci.yml | 4 ++- Dockerfile | 10 +++--- Makefile | 4 +-- README.md | 12 ++++++- azure/aci.go | 33 +++++++++++++----- builder.Makefile | 6 +++- cli/cmd/serve.go | 2 +- context/config.go | 2 +- context/flags.go | 2 +- go.mod | 2 +- scripts/setup/install-go-gen | 7 ---- scripts/setup/install-protobuf | 61 ---------------------------------- 13 files changed, 58 insertions(+), 101 deletions(-) delete mode 100755 scripts/setup/install-go-gen delete mode 100755 scripts/setup/install-protobuf diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5130ac760..93e363716 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,23 +11,17 @@ jobs: name: Build runs-on: ubuntu-latest steps: - - name: Set up Go 1.13 + - name: Set up Go 1.14 uses: actions/setup-go@v1 with: - go-version: 1.13 + go-version: 1.14 id: go - name: Checkout code into the Go module directory uses: actions/checkout@v2 - - name: Install Protoc - uses: arduino/setup-protoc@master - - - name: Get dependencies - run: ./scripts/setup/install-go-gen - - - name: Protos - run: make protos + - name: Lint + run: make lint - name: Build run: make cli diff --git a/.golangci.yml b/.golangci.yml index 9ca3494b6..a7517bb47 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -30,4 +30,6 @@ linters-settings: lll: line-length: 200 issues: - exclude-use-default: false \ No newline at end of file + # golangci hides some golint warnings (the warning about exported things + # withtout documentation for example), this will make it show them anyway. + exclude-use-default: false diff --git a/Dockerfile b/Dockerfile index bef695cd3..e9b594cd8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,9 +13,9 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ protobuf-compiler \ libprotobuf-dev -RUN go get github.com/golang/protobuf/protoc-gen-go && \ +RUN go get github.com/golang/protobuf/protoc-gen-go@v1.4.1 && \ go get golang.org/x/tools/cmd/goimports && \ - go get gotest.tools/gotestsum && \ + go get gotest.tools/gotestsum@v0.4.2 && \ go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.26.0 WORKDIR ${PWD} @@ -37,13 +37,13 @@ RUN --mount=type=cache,target=/root/.cache/go-build \ make -f builder.Makefile cross FROM scratch AS protos -COPY --from=make-protos /go/src/github.com/docker/api . +COPY --from=make-protos /api . FROM scratch AS cli -COPY --from=make-cli /go/src/github.com/docker/api/bin/* . +COPY --from=make-cli /api/bin/* . FROM scratch AS cross -COPY --from=make-cross /go/src/github.com/docker/api/bin/* . +COPY --from=make-cross /api/bin/* . FROM make-protos as test RUN make -f builder.Makefile test diff --git a/Makefile b/Makefile index 225771c99..cd8da7607 100644 --- a/Makefile +++ b/Makefile @@ -54,8 +54,8 @@ cache-clear: # Clear the builder cache @docker builder prune --force --filter type=exec.cachemount --filter=unused-for=24h lint: ## run linter(s) - @echo "Linting..." - golangci-lint run --timeout 10m0s ./... + @docker build . \ + --target lint help: ## Show help @echo Please specify a build target. The choices are: diff --git a/README.md b/README.md index 3e08364d6..154cff171 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,17 @@ ## Dev Setup -Make sure you have Docker installed and running. +The recommended way is to use the main `Makefile` that runs everything inside a container. + +If you don't have or want to use Docker for building you need to make sure you have all the needed tools installed locally: + +* go 1.14 +* `go get github.com/golang/protobuf/protoc-gen-go@v1.4.1` +* `go get golang.org/x/tools/cmd/goimports` +* `go get gotest.tools/gotestsum@v0.4.2` +* `go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.26.0` + +And then you can call the same make targets but you need to pass it the `builder.Makefile` (`make -f builder.Makefile`). ## Building the project diff --git a/azure/aci.go b/azure/aci.go index ecc70b37f..4f1d53a59 100644 --- a/azure/aci.go +++ b/azure/aci.go @@ -30,7 +30,10 @@ func init() { } func createACIContainers(ctx context.Context, aciContext store.AciContext, groupDefinition containerinstance.ContainerGroup) (c containerinstance.ContainerGroup, err error) { - containerGroupsClient := getContainerGroupsClient(aciContext.SubscriptionID) + containerGroupsClient, err := getContainerGroupsClient(aciContext.SubscriptionID) + if err != nil { + return c, errors.Wrapf(err, "cannot get container group client") + } // Check if the container group already exists _, err = containerGroupsClient.Get(ctx, aciContext.ResourceGroup, *groupDefinition.Name) @@ -94,7 +97,10 @@ func createACIContainers(ctx context.Context, aciContext store.AciContext, group } func execACIContainer(ctx context.Context, aciContext store.AciContext, command, containerGroup string, containerName string) (c containerinstance.ContainerExecResponse, err error) { - containerClient := getContainerClient(aciContext.SubscriptionID) + containerClient, err := getContainerClient(aciContext.SubscriptionID) + if err != nil { + return c, errors.Wrapf(err, "cannot get container client") + } rows, cols := getTermSize() containerExecRequest := containerinstance.ContainerExecRequest{ Command: to.StringPtr(command), @@ -199,7 +205,10 @@ func exec(ctx context.Context, address string, password string, reader io.Reader } func getACIContainerLogs(ctx context.Context, aciContext store.AciContext, containerGroupName, containerName string) (string, error) { - containerClient := getContainerClient(aciContext.SubscriptionID) + containerClient, err := getContainerClient(aciContext.SubscriptionID) + if err != nil { + return "", errors.Wrapf(err, "cannot get container client") + } logs, err := containerClient.ListLogs(ctx, aciContext.ResourceGroup, containerGroupName, containerName, nil) if err != nil { @@ -208,16 +217,22 @@ func getACIContainerLogs(ctx context.Context, aciContext store.AciContext, conta return *logs.Content, err } -func getContainerGroupsClient(subscriptionID string) containerinstance.ContainerGroupsClient { - auth, _ := auth.NewAuthorizerFromCLI() +func getContainerGroupsClient(subscriptionID string) (containerinstance.ContainerGroupsClient, error) { + auth, err := auth.NewAuthorizerFromCLI() + if err != nil { + return containerinstance.ContainerGroupsClient{}, err + } containerGroupsClient := containerinstance.NewContainerGroupsClient(subscriptionID) containerGroupsClient.Authorizer = auth - return containerGroupsClient + return containerGroupsClient, nil } -func getContainerClient(subscriptionID string) containerinstance.ContainerClient { - auth, _ := auth.NewAuthorizerFromCLI() +func getContainerClient(subscriptionID string) (containerinstance.ContainerClient, error) { + auth, err := auth.NewAuthorizerFromCLI() + if err != nil { + return containerinstance.ContainerClient{}, err + } containerClient := containerinstance.NewContainerClient(subscriptionID) containerClient.Authorizer = auth - return containerClient + return containerClient, nil } diff --git a/builder.Makefile b/builder.Makefile index a6532cb10..14f28279e 100644 --- a/builder.Makefile +++ b/builder.Makefile @@ -57,6 +57,10 @@ cross: test: @gotestsum ./... +lint: + golangci-lint run --timeout 10m0s ./... + + FORCE: -.PHONY: all protos cli cross test +.PHONY: all protos cli cross test lint diff --git a/cli/cmd/serve.go b/cli/cmd/serve.go index 00a44e119..59fc8441b 100644 --- a/cli/cmd/serve.go +++ b/cli/cmd/serve.go @@ -43,7 +43,7 @@ func runServe(ctx context.Context, opts serveOpts) error { if err != nil { return errors.Wrap(err, "listen unix socket") } - // nolint + // nolint errcheck defer listener.Close() p := proxy.NewContainerAPI() diff --git a/context/config.go b/context/config.go index f3459d8f8..f9caad374 100644 --- a/context/config.go +++ b/context/config.go @@ -46,7 +46,7 @@ func LoadConfigFile(configDir string, configFileName string) (*ConfigFile, error if err != nil { return nil, fmt.Errorf("can't read %s: %w", filename, err) } - // nolint + // nolint errcheck defer file.Close() err = json.NewDecoder(file).Decode(&configFile) if err != nil { diff --git a/context/flags.go b/context/flags.go index 649374791..b8f03a2dd 100644 --- a/context/flags.go +++ b/context/flags.go @@ -47,7 +47,7 @@ type Flags struct { Context string } -// AddFlags adds persistent (globa) flags +// AddFlags adds persistent (global) flags func (c *Flags) AddFlags(flags *pflag.FlagSet) { flags.StringVar(&c.Config, "config", filepath.Join(home(), configFileDir), "Location of the client config files `DIRECTORY`") flags.StringVarP(&c.Context, "context", "c", os.Getenv("DOCKER_CONTEXT"), "context") diff --git a/go.mod b/go.mod index 9c207c255..6cb74ef79 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/docker/api -go 1.13 +go 1.14 require ( github.com/Azure/azure-sdk-for-go v42.0.0+incompatible diff --git a/scripts/setup/install-go-gen b/scripts/setup/install-go-gen deleted file mode 100755 index cb580df8c..000000000 --- a/scripts/setup/install-go-gen +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env bash - -go get -u github.com/gogo/protobuf/proto -go get -u github.com/gogo/protobuf/jsonpb -go get -u github.com/golang/protobuf/protoc-gen-go -go get -u github.com/stevvooe/protobuild -go get -u gotest.tools/gotestsum diff --git a/scripts/setup/install-protobuf b/scripts/setup/install-protobuf deleted file mode 100755 index 0ee612e1b..000000000 --- a/scripts/setup/install-protobuf +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env bash - -# Copyright The containerd 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. - -# -# Downloads and installs protobuf -# -set -eu -o pipefail - -PROTOBUF_VERSION=3.11.4 -GOARCH=$(go env GOARCH) -GOOS=$(go env GOOS) -PROTOBUF_DIR=$(mktemp -d) - -case $GOARCH in - -arm64) - wget -O $PROTOBUF_DIR/protobuf "https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protoc-$PROTOBUF_VERSION-linux-aarch64.zip" - unzip $PROTOBUF_DIR/protobuf -d /usr/local - ;; - -amd64 | 386) - if [ "$GOOS" = windows ]; then - wget -O $PROTOBUF_DIR/protobuf "https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protoc-$PROTOBUF_VERSION-win32.zip" - elif [ "$GOOS" = linux ]; then - wget -O $PROTOBUF_DIR/protobuf "https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protoc-$PROTOBUF_VERSION-linux-x86_64.zip" - elif [ "$GOOS" = darwin ]; then - curl -Lo $PROTOBUF_DIR/protobuf "https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protoc-$PROTOBUF_VERSION-osx-x86_64.zip" - fi - unzip $PROTOBUF_DIR/protobuf -x readme.txt -d /usr/local - ;; - -ppc64le) - wget -O $PROTOBUF_DIR/protobuf "https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protoc-$PROTOBUF_VERSION-linux-ppcle_64.zip" - unzip $PROTOBUF_DIR/protobuf -d /usr/local - ;; -*) - wget -O $PROTOBUF_DIR/protobuf "https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-cpp-$PROTOBUF_VERSION.zip" - unzip $PROTOBUF_DIR/protobuf -d /usr/src/protobuf - cd /usr/src/protobuf/protobuf-$PROTOBUF_VERSION - ./autogen.sh - ./configure --disable-shared - make - make check - make install - ldconfig - ;; -esac -rm -rf $PROTOBUF_DIR