From bda0ce4b1f93111f4fc17e38cb86958658bbea66 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Fri, 7 Aug 2020 15:00:30 -0700 Subject: [PATCH] Add pre-commit and clean up --- .pre-commit-config.yaml | 21 +++++++++++++++++++++ Dockerfile | 8 +++----- Dockerfile.multi-stage | 8 ++++---- LICENSE | 16 ++++++++-------- Makefile | 10 ++++++++++ main.go | 13 +++++++------ main_test.go | 5 +++-- 7 files changed, 56 insertions(+), 25 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..02e83ec --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,21 @@ +--- +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.4.0 + hooks: + - id: check-added-large-files + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-merge-conflict + - repo: git://github.com/dnephin/pre-commit-golang + rev: v0.3.5 + hooks: + - id: go-fmt + - id: go-imports + # - id: gometalinter + # - id: golangci-lint + - repo: https://github.com/IamTheFij/docker-pre-commit + rev: v2.0.0 + hooks: + - id: docker-compose-check + - id: hadolint diff --git a/Dockerfile b/Dockerfile index edf0813..022ba81 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,6 @@ -ARG REPO=library -FROM ${REPO}/busybox:latest -WORKDIR /root/ +FROM scratch ARG ARCH=amd64 -COPY ./dockron-linux-${ARCH} ./dockron +COPY ./dockron-linux-${ARCH} /dockron -ENTRYPOINT [ "./dockron" ] +ENTRYPOINT [ "/dockron" ] diff --git a/Dockerfile.multi-stage b/Dockerfile.multi-stage index 157d886..13a8095 100644 --- a/Dockerfile.multi-stage +++ b/Dockerfile.multi-stage @@ -1,6 +1,7 @@ ARG REPO=library FROM golang:1.12-alpine AS builder +# hadolint ignore=DL3018 RUN apk add --no-cache git RUN mkdir /app @@ -16,8 +17,7 @@ ARG VERSION=dev ENV CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} RUN go build -ldflags "-X main.version=${VERSION}" -a -installsuffix nocgo -o dockron . -FROM ${REPO}/busybox:latest -WORKDIR /root/ -COPY --from=builder /app/dockron . +FROM scratch +COPY --from=builder /app/dockron / -ENTRYPOINT [ "./dockron" ] +ENTRYPOINT [ "/dockron" ] diff --git a/LICENSE b/LICENSE index dbfab25..f8a1b2a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,5 @@ -Apache License -Version 2.0, January 2004 +Apache License +Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION @@ -59,14 +59,14 @@ To apply the Apache License to your work, attach the following boilerplate notic Copyright [yyyy] [name of copyright owner] -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. +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 +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. diff --git a/Makefile b/Makefile index 4625ed0..281d0d9 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,16 @@ test: # @go tool cover -func=coverage.out | awk -v target=80.0% \ '/^total:/ { print "Total coverage: " $$3 " Minimum coverage: " target; if ($$3+0.0 >= target+0.0) print "ok"; else { print "fail"; exit 1; } }' +# Installs pre-commit hooks +.PHONY: install-hooks +install-hooks: + pre-commit install --install-hooks + +# Runs pre-commit checks on files +.PHONY: check +check: + pre-commit run --all-files + # Output target dockron: @echo Version: $(VERSION) diff --git a/main.go b/main.go index bc8c12a..7d6b8e9 100644 --- a/main.go +++ b/main.go @@ -3,14 +3,15 @@ package main import ( "flag" "fmt" - dockerTypes "github.com/docker/docker/api/types" - dockerClient "github.com/docker/docker/client" - "github.com/robfig/cron/v3" - "golang.org/x/net/context" "log" "os" "strings" "time" + + dockerTypes "github.com/docker/docker/api/types" + dockerClient "github.com/docker/docker/client" + "github.com/robfig/cron/v3" + "golang.org/x/net/context" ) var ( @@ -86,7 +87,7 @@ func ScheduleJobs(c *cron.Cron, jobs []ContainerStartJob) { log.Printf("Scheduled %s (%s) with schedule '%s'\n", job.Name, job.ContainerID[:10], job.Schedule) } else { // TODO: Track something for a healthcheck here - log.Printf("Error scheduling %s (%s) with schedule '%s'. %v", job.Name, job.ContainerID[:10], job.Schedule, err) + log.Printf("Error scheduling %s (%s) with schedule '%s'. %v\n", job.Name, job.ContainerID[:10], job.Schedule, err) } } } @@ -123,9 +124,9 @@ func main() { c.Stop() c = cron.New() - // Schedule jobs again jobs := QueryScheduledJobs(client) ScheduleJobs(c, jobs) + c.Start() // Sleep until the next query time diff --git a/main_test.go b/main_test.go index edba326..fccafe8 100644 --- a/main_test.go +++ b/main_test.go @@ -2,11 +2,12 @@ package main import ( "fmt" + "log" + "testing" + dockerTypes "github.com/docker/docker/api/types" "github.com/robfig/cron/v3" "golang.org/x/net/context" - "log" - "testing" ) // FakeDockerClient is used to test without interracting with Docker