diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5130ac76..93e36371 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 9ca3494b..a7517bb4 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 bef695cd..e9b594cd 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 225771c9..cd8da760 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 3e08364d..154cff17 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 ecc70b37..4f1d53a5 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 a6532cb1..14f28279 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 00a44e11..59fc8441 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 f3459d8f..f9caad37 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 64937479..b8f03a2d 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 9c207c25..6cb74ef7 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 cb580df8..00000000 --- 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 0ee612e1..00000000 --- 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