Skip to content

Commit

Permalink
cleanup Makefile, Dockerfile, go.mod
Browse files Browse the repository at this point in the history
Updates to Go 1.19 in go.mod to get us to a more modern Go release.
Similarly changes the Dockerfile that builds the `ghwc` binary to use Go
1.19 and use a hashed version of the Alpine base image to address
security best practices.

Finally, removes the outdated and unnecessary Make targets and
unnecessarily complicated calls to find Go packages. These targets and
fancy code was left over from before Go modules and when we were using
`dep` and vendoring things.

Adds a `.github/workflows/fmtcheck.yml` that follows GH actions security
best practices and separates the fmtcheck stuff from the other tests in
the main `.github.com/workflows/go.yml`.

Signed-off-by: Jay Pipes <jaypipes@gmail.com>
  • Loading branch information
jaypipes committed Jul 21, 2023
1 parent 01428a7 commit 7657ee9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 47 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/fmtcheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: fmtcheck

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

permissions:
contents: read

jobs:
fmtcheck:
runs-on: ubuntu-latest
steps:
- name: harden runner
uses: step-security/harden-runner@55d479fb1c5bcad5a4f9099a5d9f37c8857b2845 # v2.4.1
with:
egress-policy: block
disable-sudo: true
allowed-endpoints: >
github.com:443
- name: checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: setup go
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1
with:
go-version: 1.19
- name: fmtcheck
run: bash -c "diff -u <(echo -n) <(gofmt -d .)"
15 changes: 0 additions & 15 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@ on:
# see: https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners
jobs:
# tier 0: system-independent checks
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: set up golang
uses: actions/setup-go@v2
with:
go-version: 1.19

- name: format
run: ./hack/check-format.sh

lint:
runs-on: ubuntu-latest
steps:
Expand Down
6 changes: 2 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
FROM golang:1.15-buster as builder
FROM golang:1.19-buster as builder
WORKDIR /go/src/github.com/jaypipes/ghw

# Force the go compiler to use modules.
ENV GO111MODULE=on
ENV GOPROXY=direct

# go.mod and go.sum go into their own layers.
Expand All @@ -16,7 +14,7 @@ COPY . .

RUN CGO_ENABLED=0 go build -o ghwc ./cmd/ghwc/

FROM alpine:3.7
FROM alpine:3.7@sha256:8421d9a84432575381bfabd248f1eb56f3aa21d9d7cd2511583c68c9b7511d10
RUN apk add --no-cache ethtool

WORKDIR /bin
Expand Down
31 changes: 4 additions & 27 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
VENDOR := vendor
PKGS := $(shell go list ./... | grep -v /$(VENDOR)/)
SRC = $(shell find . -type f -name '*.go' -not -path "*/$(VENDOR)/*")
BIN_DIR := $(GOPATH)/bin
GOMETALINTER := $(BIN_DIR)/gometalinter

.PHONY: test
test: vet
go test $(PKGS)

$(GOMETALINTER):
go get -u github.com/alecthomas/gometalinter
$(GOMETALINTER) --install &> /dev/null

.PHONY: lint
lint: $(GOMETALINTER)
$(GOMETALINTER) ./... --vendor
go test -v ./...

.PHONY: fmt
fmt:
@echo "Running gofmt on all sources..."
@gofmt -s -l -w $(SRC)
@gofmt -s -l -w .

.PHONY: fmtcheck
fmtcheck:
@bash -c "diff -u <(echo -n) <(gofmt -d $(SRC))"
@bash -c "diff -u <(echo -n) <(gofmt -d .)"

.PHONY: vet
vet:
go vet $(PKGS)

.PHONY: cover
cover:
$(shell [ -e coverage.out ] && rm coverage.out)
@echo "mode: count" > coverage-all.out
$(foreach pkg,$(PKGS),\
go test -coverprofile=coverage.out -covermode=count $(pkg);\
tail -n +2 coverage.out >> coverage-all.out;)
go tool cover -html=coverage-all.out -o=coverage-all.html
go vet ./...
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/jaypipes/ghw

go 1.18
go 1.19

require (
github.com/StackExchange/wmi v1.2.1
Expand Down

0 comments on commit 7657ee9

Please sign in to comment.