Add `make lint` and run it on CI

This commit is contained in:
Djordje Lukic 2020-05-04 23:50:00 +02:00 committed by Djordje Lukic
parent 6feedaf939
commit 4e9a4185af
13 changed files with 58 additions and 101 deletions

View File

@ -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

View File

@ -30,4 +30,6 @@ linters-settings:
lll:
line-length: 200
issues:
# 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

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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()

View File

@ -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 {

View File

@ -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")

2
go.mod
View File

@ -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

View File

@ -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

View File

@ -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