From 1c4fe660e562223566af5e33b18fa8e101379b5b Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Thu, 10 Feb 2022 20:31:56 -0500 Subject: [PATCH 01/22] feat: initial commit --- .chglog/CHANGELOG.tpl.md | 38 ++++++++++ .chglog/config.yml | 28 ++++++++ .github/workflows/build.yaml | 44 ++++++++++++ .github/workflows/release.yml | 128 ++++++++++++++++++++++++++++++++++ .gitignore | 18 +++++ .pre-commit-config.yaml | 18 +++++ Dockerfile | 20 ++++++ LICENSE | 21 ++++++ Makefile | 56 +++++++++++++++ README.md | 8 +++ go.mod | 10 +++ go.sum | 49 +++++++++++++ main.go | 21 ++++++ program/program.go | 34 +++++++++ 14 files changed, 493 insertions(+) create mode 100755 .chglog/CHANGELOG.tpl.md create mode 100755 .chglog/config.yml create mode 100644 .github/workflows/build.yaml create mode 100755 .github/workflows/release.yml create mode 100644 .gitignore create mode 100644 .pre-commit-config.yaml create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 program/program.go diff --git a/.chglog/CHANGELOG.tpl.md b/.chglog/CHANGELOG.tpl.md new file mode 100755 index 0000000..a092888 --- /dev/null +++ b/.chglog/CHANGELOG.tpl.md @@ -0,0 +1,38 @@ +{{ range .Versions }} + +## {{ if .Tag.Previous }}[{{ .Tag.Name }}]({{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}){{ else }}{{ .Tag.Name }}{{ end }} ({{ datetime "2006-01-02" .Tag.Date }}) + +{{ range .CommitGroups -}} +### {{ .Title }} + +{{ range .Commits -}} +* {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }} +{{ end }} +{{ end -}} + +{{- if .RevertCommits -}} +### Reverts + +{{ range .RevertCommits -}} +* {{ .Revert.Header }} +{{ end }} +{{ end -}} + +{{- if .MergeCommits -}} +### Pull Requests + +{{ range .MergeCommits -}} +* {{ .Header }} +{{ end }} +{{ end -}} + +{{- if .NoteGroups -}} +{{ range .NoteGroups -}} +### {{ .Title }} + +{{ range .Notes }} +{{ .Body }} +{{ end }} +{{ end -}} +{{ end -}} +{{ end -}} \ No newline at end of file diff --git a/.chglog/config.yml b/.chglog/config.yml new file mode 100755 index 0000000..38acfb6 --- /dev/null +++ b/.chglog/config.yml @@ -0,0 +1,28 @@ +style: github +template: CHANGELOG.tpl.md +info: + title: CHANGELOG + repository_url: https://github.com/deweysasser/golang-program +options: + commits: + # filters: + # Type: + # - feat + # - fix + # - perf + # - refactor + commit_groups: + # title_maps: + # feat: Features + # fix: Bug Fixes + # perf: Performance Improvements + # refactor: Code Refactoring + header: + pattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$" + pattern_maps: + - Type + - Scope + - Subject + notes: + keywords: + - BREAKING CHANGE \ No newline at end of file diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..59afd28 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,44 @@ +on: + push: + branches: + - "**" + +name: Build +env: + GO_VERSION: 1.17 + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: ${{env.GO_VERSION}} + + - name: Vet + run: make vet + + - name: Test + run: make test + + - name: Build + run: make + + + format: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: ${{env.GO_VERSION}} + + - run: go fmt ./... + + - name: Verify No Differences after format + run: test -z "$(git diff)" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100755 index 0000000..b0651f1 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,128 @@ +name: Release + +on: + push: + tags: [ v* ] + +env: + GO_VERSION: 1.17 + REPO: ${{github.repository}} + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + GOOS: [linux, darwin, windows] + GOARCH: [amd64, arm64] + include: + - GOOS: windows + ext: .exe + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: ${{ env.GO_VERSION}} + + - name: Repo Name + id: repo-name + run: echo ::set-output name=name::$(basename ${{github.repository}}) + + - name: Test + run: go test -v ./... + + - name: Build + run: make compile PROGRAM=output/${{steps.repo-name.outputs.name}}-${{env.GOOS}}-${{env.GOARCH}}${{matrix.ext}} + env: + GOOS: ${{matrix.GOOS}} + GOARCH: ${{matrix.GOARCH}} + + - name: 'Upload Artifact' + uses: actions/upload-artifact@v2 + with: + name: artifacts + path: output + retention-days: 1 + if-no-files-found: error + + release: + runs-on: ubuntu-latest + needs: + - build + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Download Artifacts + uses: actions/download-artifact@v2 + + - name: Install ChangeLog generator + run: | + wget https://github.com/git-chglog/git-chglog/releases/download/v0.15.1/git-chglog_0.15.1_linux_amd64.tar.gz + tar xzf git-chglog*.tar.gz git-chglog + + - name: "Get Last Release" + id: last_release + uses: InsonusK/get-latest-release@v1.0.1 + with: + myToken: ${{ github.token }} + exclude_types: "draft|prerelease" + + - name: Generate Changelog for ${{ github.ref_name }} + id: generate-changelog + run: make CHANGELOG.md + + - name: Create Release + id: create_release + uses: softprops/action-gh-release@v1 + with: + files: ./artifacts/* + body_path: ./CHANGELOG.md + draft: true + + docker-build: + runs-on: ubuntu-latest + name: Build the docker image + steps: + - name: Login to GitHub Container Registry + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Checkout + uses: actions/checkout@v2 + + - name: Docker Release to GHCR + uses: docker/build-push-action@v2 + with: + context: . + push: true + build-args: | + GO_VERSION=${{ env.GO_VERSION }} + VERSION=${{ github.ref_name }} + tags: | + ghcr.io/${{env.REPO}}:latest + ghcr.io/${{env.REPO}}:${{ github.ref_name }} + +# uncomment this if you're also using docker hub +# - name: Login to Docker Container Registry +# if: ${{ secrets.DOCKERHUB_TOKEN }} +# uses: docker/login-action@v1 +# with: +# registry: ghcr.io +# username: ${{ github.repository_owner }} +# password: ${{ secrets.GITHUB_TOKEN }} +# +# - name: Docker Release to Docker Hub +# if: ${{ secrets.DOCKERHUB_TOKEN }} +# uses: docker/build-push-action@v2 +# with: +# context: . +# push: true +# tags: | +# ${{env.REPO}}:latest +# ${{env.REPO}}:${{github.ref_name}} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..34d6b74 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ +golang-program +.idea +CHANGELOG.md diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..55df628 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,18 @@ +repos: +- repo: https://github.com/dnephin/pre-commit-golang + rev: v0.4.0 + hooks: + - id: go-fmt + - id: go-vet + #- id: go-cyclo + #args: [-over=15] + #- id: golangci-lint + - id: go-critic + +- repo: https://github.com/deweysasser/conventional-pre-commit + rev: v1.2.1 + hooks: + - id: conventional-pre-commit + stages: [commit-msg] + args: [] # optional: list of Conventional Commits types to allow + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5a55dcf --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +ARG GO_VERSION=1.17 +FROM golang:${GO_VERSION} as builder +ARG PROGRAM=nothing +ARG VERSION=development + +RUN mkdir /src /output + +WORKDIR /src + +COPY . . +RUN GOBIN=/output make install VERSION=$VERSION +RUN PROGRAM=$(ls /output); echo "#!/bin/sh\nexec '/usr/bin/$PROGRAM' \"\$@\"" > /docker-entrypoint.sh && chmod +x /docker-entrypoint.sh + + +FROM alpine:latest +RUN apk add --no-cache libc6-compat ca-certificates + +COPY --from=builder /output/* /usr/bin +COPY --from=builder /docker-entrypoint.sh /docker-entrypoint.sh +ENTRYPOINT ["/docker-entrypoint.sh"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..44ba256 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Dewey Sasser + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dd1ec14 --- /dev/null +++ b/Makefile @@ -0,0 +1,56 @@ + +#ifeq $(OS) windows +#EXE=.exe +#else +#EXE= +#endif + +BASENAME=$(notdir $(shell pwd)) +PROGRAM=$(BASENAME)$(EXE) +LAST_RELEASE= + +REPO=$(shell go list | head -n 1) +IMAGE=$(BASENAME) +VERSION ?= $(shell git describe --tags --always --dirty) +DOCKER=docker + +.PHONY: $(PROGRAM) + +all: $(PROGRAM) + +compile: $(PROGRAM) + +$(PROGRAM): + go build -ldflags="-X '$(REPO)/program.Version=${VERSION}'" -o $(PROGRAM) + +install: + go install -ldflags="-X '$(REPO)/program.Version=${VERSION}'" + + +image: Dockerfile + $(DOCKER) build --build-arg PROGRAM=$(BASENAME) --build-arg VERSION=$(VERSION) --build-arg BASENAME=$(BASENAME) -t $(IMAGE) . + +test: + go test -v ./... + +vet: + go vet ./... + +changelog: CHANGELOG.md +CHANGELOG.md: .chglog/config.yml + git chglog $(LAST_RELEASE) >$@ + +.chglog/config.yml: go.mod + sed -i.bak -e "s/repository_url:.*/repository_url: $(REPO)/" $@ + +hooks: .git/hooks/pre-commit + +.git/hooks/pre-commit: .pre-commit-config.yaml + pre-commit install + pre-commit install --hook-type commit-msg + + +info:: + @echo BASENAME=$(BASENAME) + @echo PROGRAM=$(PROGRAM) + @echo IMAGE=$(IMAGE) \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..21c345c --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# golang-program + +A base for utility software written in golang + +## Overview + +This is a template project. You (or I) can use it when starting a simple golang program so we can +just immediately start coding interesting things instead of remembering the boiler-plate. \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3d519d5 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/deweysasser/golang-program + +go 1.17 + +require ( + github.com/alecthomas/kong v0.4.0 + github.com/rs/zerolog v1.26.1 +) + +require github.com/pkg/errors v0.9.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..10167e2 --- /dev/null +++ b/go.sum @@ -0,0 +1,49 @@ +github.com/alecthomas/kong v0.4.0 h1:uj5Xe5RaoSRu2sUIy6XZk5QO9NaLU7/+2HXq7GRas/4= +github.com/alecthomas/kong v0.4.0/go.mod h1:uzxf/HUh0tj43x1AyJROl3JT7SgsZ5m+icOv1csRhc0= +github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48= +github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8= +github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc= +github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..3e056c7 --- /dev/null +++ b/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "github.com/alecthomas/kong" + "github.com/deweysasser/golang-program/program" + "github.com/rs/zerolog/log" +) + +func main() { + + var Options program.Options + + context := kong.Parse(&Options, + kong.Description("Brief Program Summary"), + ) + + // This ends up calling Options.Run() + if err := context.Run(); err != nil { + log.Err(err).Msg("Program failed") + } +} diff --git a/program/program.go b/program/program.go new file mode 100644 index 0000000..732f604 --- /dev/null +++ b/program/program.go @@ -0,0 +1,34 @@ +package program + +import ( + "fmt" + "github.com/rs/zerolog" + "os" +) + +// Version is created by the Makefile and passed in as a linker flag +var Version = "unknown" + +// Options is the structure of program options +type Options struct { + Debug bool `short:"d" help:"Show debugging information"` + Version bool `short:"v" help:"Show program version"` + Quiet bool `short:"q" help:"Be less verbose than usual"` +} + +// Run runs the program +func (program *Options) Run() error { + + if program.Version { + fmt.Println(Version) + os.Exit(0) + } + + if program.Debug { + zerolog.SetGlobalLevel(zerolog.DebugLevel) + } else { + zerolog.SetGlobalLevel(zerolog.InfoLevel) + } + + return nil +} From 037896478860ea2575b372ee348ff06c1490d70e Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Thu, 10 Feb 2022 22:42:13 -0500 Subject: [PATCH 02/22] refactor: change logging init --- program/program.go | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/program/program.go b/program/program.go index 732f604..d2de85d 100644 --- a/program/program.go +++ b/program/program.go @@ -3,32 +3,64 @@ package program import ( "fmt" "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "os" ) -// Version is created by the Makefile and passed in as a linker flag +// Version is created by the Makefile and passed in as a linker flag. When go 1.18 is released, this will be replaced +// with the built-in mechanism + var Version = "unknown" // Options is the structure of program options type Options struct { - Debug bool `short:"d" help:"Show debugging information"` - Version bool `short:"v" help:"Show program version"` - Quiet bool `short:"q" help:"Be less verbose than usual"` + Debug bool `short:"d" help:"Show debugging information"` + Version bool `short:"v" help:"Show program version"` + LogFormat string `short:"l" enum:"auto,jsonl,terminal" default:"auto" help:"How to show program output (auto|terminal|jsonl)"` + Quiet bool `short:"q" help:"Be less verbose than usual"` } // Run runs the program func (program *Options) Run() error { + program.initLogging() + + return nil +} +func (program *Options) initLogging() { if program.Version { fmt.Println(Version) os.Exit(0) } - if program.Debug { + switch { + case program.Debug: zerolog.SetGlobalLevel(zerolog.DebugLevel) - } else { + case program.Quiet: + zerolog.SetGlobalLevel(zerolog.WarnLevel) + default: zerolog.SetGlobalLevel(zerolog.InfoLevel) } - return nil + if program.LogFormat == "terminal" || + (program.LogFormat == "auto" && isTerminal(os.Stdout)) { + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout}) + } else { + log.Logger = log.Output(os.Stdout) + } + + log.Logger.Debug(). + Str("version", Version). + Str("program", os.Args[0]). + Msg("Starting") +} + +// isTerminal returns true if the file given points to a character device (i.e. a terminal) +func isTerminal(file *os.File) bool { + if fileInfo, err := file.Stat(); err != nil { + log.Err(err).Msg("Error running stat") + return false + } else { + return (fileInfo.Mode() & os.ModeCharDevice) != 0 + } } From d620c569801ffe165b16b16f8080cbc511a7dfc7 Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Thu, 10 Feb 2022 22:50:43 -0500 Subject: [PATCH 03/22] test: change hooks --- .pre-commit-config.yaml | 17 ++++++++--------- Makefile | 8 +++++++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 55df628..630a65e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,13 +1,12 @@ repos: -- repo: https://github.com/dnephin/pre-commit-golang - rev: v0.4.0 - hooks: - - id: go-fmt - - id: go-vet - #- id: go-cyclo - #args: [-over=15] - #- id: golangci-lint - - id: go-critic +- repo: https://github.com/tekwizely/pre-commit-golang + rev: v1.0.0-beta.5 + hooks: + - id: go-fmt + - id: go-vet + - id: go-sec-mod + - id: go-staticcheck-mod + - id: go-critic - repo: https://github.com/deweysasser/conventional-pre-commit rev: v1.2.1 diff --git a/Makefile b/Makefile index dd1ec14..b519a7c 100644 --- a/Makefile +++ b/Makefile @@ -53,4 +53,10 @@ hooks: .git/hooks/pre-commit info:: @echo BASENAME=$(BASENAME) @echo PROGRAM=$(PROGRAM) - @echo IMAGE=$(IMAGE) \ No newline at end of file + @echo IMAGE=$(IMAGE) + + +tools: + go install honnef.co/go/tools/cmd/staticcheck@latest + go install github.com/go-critic/go-critic/cmd/gocritic@latest + go install github.com/securego/gosec/v2/cmd/gosec@latest From e5790c76fa7e0d33a05741c932c63eaeebcc7d99 Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Sun, 27 Feb 2022 19:03:08 -0500 Subject: [PATCH 04/22] feat: improve logging/output handling --- main.go | 15 +++++++++------ program/program.go | 30 +++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 3e056c7..13fdd21 100644 --- a/main.go +++ b/main.go @@ -1,21 +1,24 @@ package main import ( - "github.com/alecthomas/kong" "github.com/deweysasser/golang-program/program" "github.com/rs/zerolog/log" + "os" ) func main() { - var Options program.Options + var options program.Options - context := kong.Parse(&Options, - kong.Description("Brief Program Summary"), - ) + context, err := options.Parse(os.Args[1:]) - // This ends up calling Options.Run() + if err != nil { + os.Exit(1) + } + + // This ends up calling options.Run() if err := context.Run(); err != nil { log.Err(err).Msg("Program failed") + os.Exit(1) } } diff --git a/program/program.go b/program/program.go index d2de85d..6c8bbb4 100644 --- a/program/program.go +++ b/program/program.go @@ -2,6 +2,7 @@ package program import ( "fmt" + "github.com/alecthomas/kong" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "os" @@ -14,16 +15,31 @@ var Version = "unknown" // Options is the structure of program options type Options struct { - Debug bool `short:"d" help:"Show debugging information"` - Version bool `short:"v" help:"Show program version"` - LogFormat string `short:"l" enum:"auto,jsonl,terminal" default:"auto" help:"How to show program output (auto|terminal|jsonl)"` - Quiet bool `short:"q" help:"Be less verbose than usual"` + Debug bool `help:"Show debugging information"` + Version bool `help:"Show program version"` + OutputFormat string `enum:"auto,jsonl,terminal" default:"auto" help:"How to show program output (auto|terminal|jsonl)"` + Quiet bool `help:"Be less verbose than usual"` +} + +// Parse calls the CLI parsing routines +func (program *Options) Parse(args []string) (*kong.Context, error) { + parser, err := kong.New(program) // kong.Description("Brief Program Summary"), + + if err != nil { + return nil, err + } + + return parser.Parse(args) } // Run runs the program func (program *Options) Run() error { - program.initLogging() + return nil +} +// AfterApply runs after the options are parsed but before anything runs +func (program *Options) AfterApply() error { + program.initLogging() return nil } @@ -42,8 +58,8 @@ func (program *Options) initLogging() { zerolog.SetGlobalLevel(zerolog.InfoLevel) } - if program.LogFormat == "terminal" || - (program.LogFormat == "auto" && isTerminal(os.Stdout)) { + if program.OutputFormat == "terminal" || + (program.OutputFormat == "auto" && isTerminal(os.Stdout)) { log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout}) } else { log.Logger = log.Output(os.Stdout) From 1b4b59167468c6487fbfb2e8a4e4af6b3a085516 Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Sun, 27 Feb 2022 19:21:05 -0500 Subject: [PATCH 05/22] fix: handle terminal colors on windows outside of Cygwin properly --- Makefile | 10 +++++----- go.mod | 7 ++++++- go.sum | 7 +++++++ program/program.go | 22 ++++++++++++++++------ 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index b519a7c..30c7724 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ -#ifeq $(OS) windows -#EXE=.exe -#else -#EXE= -#endif +ifeq ($(OS),Windows_NT) +EXE=.exe +else +EXE= +endif BASENAME=$(notdir $(shell pwd)) PROGRAM=$(BASENAME)$(EXE) diff --git a/go.mod b/go.mod index 3d519d5..610b7de 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,12 @@ go 1.17 require ( github.com/alecthomas/kong v0.4.0 + github.com/mattn/go-colorable v0.1.12 github.com/rs/zerolog v1.26.1 ) -require github.com/pkg/errors v0.9.1 // indirect +require ( + github.com/mattn/go-isatty v0.0.14 // indirect + github.com/pkg/errors v0.9.1 // indirect + golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect +) diff --git a/go.sum b/go.sum index 10167e2..83b1b89 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,10 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= +github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -32,7 +36,10 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/program/program.go b/program/program.go index 6c8bbb4..d9ffbe1 100644 --- a/program/program.go +++ b/program/program.go @@ -3,9 +3,12 @@ package program import ( "fmt" "github.com/alecthomas/kong" + "github.com/mattn/go-colorable" "github.com/rs/zerolog" "github.com/rs/zerolog/log" + "io" "os" + "runtime" ) // Version is created by the Makefile and passed in as a linker flag. When go 1.18 is released, this will be replaced @@ -15,10 +18,11 @@ var Version = "unknown" // Options is the structure of program options type Options struct { - Debug bool `help:"Show debugging information"` - Version bool `help:"Show program version"` - OutputFormat string `enum:"auto,jsonl,terminal" default:"auto" help:"How to show program output (auto|terminal|jsonl)"` - Quiet bool `help:"Be less verbose than usual"` + Version bool `help:"Show program version"` + + Debug bool `group:"Output" help:"Show debugging information"` + OutputFormat string `group:"Output" enum:"auto,jsonl,terminal" default:"auto" help:"How to show program output (auto|terminal|jsonl)"` + Quiet bool `group:"Output" help:"Be less verbose than usual"` } // Parse calls the CLI parsing routines @@ -58,11 +62,17 @@ func (program *Options) initLogging() { zerolog.SetGlobalLevel(zerolog.InfoLevel) } + var out io.Writer = os.Stdout + + if os.Getenv("TERM") == "" && runtime.GOOS == "windows" { + out = colorable.NewColorableStdout() + } + if program.OutputFormat == "terminal" || (program.OutputFormat == "auto" && isTerminal(os.Stdout)) { - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout}) + log.Logger = log.Output(zerolog.ConsoleWriter{Out: out}) } else { - log.Logger = log.Output(os.Stdout) + log.Logger = log.Output(out) } log.Logger.Debug(). From f23be9037ba5cfc4d6ee12a803b137addf8dd42f Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Sun, 27 Feb 2022 19:40:45 -0500 Subject: [PATCH 06/22] fix: display error message when command line doesn't parse --- main.go | 2 ++ program/program.go | 11 +++++------ program/version.go | 6 ++++++ 3 files changed, 13 insertions(+), 6 deletions(-) create mode 100755 program/version.go diff --git a/main.go b/main.go index 13fdd21..5bdfeaa 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "github.com/deweysasser/golang-program/program" "github.com/rs/zerolog/log" "os" @@ -13,6 +14,7 @@ func main() { context, err := options.Parse(os.Args[1:]) if err != nil { + fmt.Println(err) os.Exit(1) } diff --git a/program/program.go b/program/program.go index d9ffbe1..7db3e72 100644 --- a/program/program.go +++ b/program/program.go @@ -11,11 +11,6 @@ import ( "runtime" ) -// Version is created by the Makefile and passed in as a linker flag. When go 1.18 is released, this will be replaced -// with the built-in mechanism - -var Version = "unknown" - // Options is the structure of program options type Options struct { Version bool `help:"Show program version"` @@ -27,9 +22,13 @@ type Options struct { // Parse calls the CLI parsing routines func (program *Options) Parse(args []string) (*kong.Context, error) { - parser, err := kong.New(program) // kong.Description("Brief Program Summary"), + parser, err := kong.New(program, + kong.ShortUsageOnError(), + // kong.Description("Brief Program Summary"), + ) if err != nil { + fmt.Println(err) return nil, err } diff --git a/program/version.go b/program/version.go new file mode 100755 index 0000000..76d5559 --- /dev/null +++ b/program/version.go @@ -0,0 +1,6 @@ +package program + +// Version is created by the Makefile and passed in as a linker flag. When go 1.18 is released, this will be replaced +// with the built-in mechanism + +var Version = "unknown" From a93feb0622b71eb2edf31d551b38bebbef7e052f Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Sun, 27 Feb 2022 19:42:41 -0500 Subject: [PATCH 07/22] Add an optional version command to print version instead of using the option --- program/program.go | 1 + program/version.go | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/program/program.go b/program/program.go index 7db3e72..f0fddcb 100644 --- a/program/program.go +++ b/program/program.go @@ -14,6 +14,7 @@ import ( // Options is the structure of program options type Options struct { Version bool `help:"Show program version"` + // VersionCmd VersionCmd `name:"version" cmd:"" help:"show program version"` Debug bool `group:"Output" help:"Show debugging information"` OutputFormat string `group:"Output" enum:"auto,jsonl,terminal" default:"auto" help:"How to show program output (auto|terminal|jsonl)"` diff --git a/program/version.go b/program/version.go index 76d5559..0c692cf 100755 --- a/program/version.go +++ b/program/version.go @@ -1,6 +1,17 @@ package program +import "fmt" + // Version is created by the Makefile and passed in as a linker flag. When go 1.18 is released, this will be replaced // with the built-in mechanism var Version = "unknown" + +// VersionCmd prints the program version +type VersionCmd struct{} + +func (v *VersionCmd) Run(program *Options) error { + _ = program + _, _ = fmt.Println(Version) + return nil +} From 8dfe4d5bd7cf1cd958fe3cc409e66b6b6af8e301 Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Sun, 27 Feb 2022 19:43:17 -0500 Subject: [PATCH 08/22] fix: change name of the option group that handles command line output --- program/program.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/program/program.go b/program/program.go index f0fddcb..870e55f 100644 --- a/program/program.go +++ b/program/program.go @@ -16,9 +16,9 @@ type Options struct { Version bool `help:"Show program version"` // VersionCmd VersionCmd `name:"version" cmd:"" help:"show program version"` - Debug bool `group:"Output" help:"Show debugging information"` - OutputFormat string `group:"Output" enum:"auto,jsonl,terminal" default:"auto" help:"How to show program output (auto|terminal|jsonl)"` - Quiet bool `group:"Output" help:"Be less verbose than usual"` + Debug bool `group:"Info" help:"Show debugging information"` + OutputFormat string `group:"Info" enum:"auto,jsonl,terminal" default:"auto" help:"How to show program output (auto|terminal|jsonl)"` + Quiet bool `group:"Info" help:"Be less verbose than usual"` } // Parse calls the CLI parsing routines From 0432f6ad6a36caf96b8b610b118737df209cb1d3 Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Sun, 27 Feb 2022 20:15:15 -0500 Subject: [PATCH 09/22] test: add a basic test case for the program --- go.mod | 6 ++++++ go.sum | 5 +++++ program/program_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100755 program/program_test.go diff --git a/go.mod b/go.mod index 610b7de..d5a5616 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,19 @@ module github.com/deweysasser/golang-program go 1.17 require ( + bou.ke/monkey v1.0.2 github.com/alecthomas/kong v0.4.0 github.com/mattn/go-colorable v0.1.12 github.com/rs/zerolog v1.26.1 + github.com/stretchr/testify v1.7.0 + github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04 ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/go.sum b/go.sum index 83b1b89..9beecc3 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI= +bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA= github.com/alecthomas/kong v0.4.0 h1:uj5Xe5RaoSRu2sUIy6XZk5QO9NaLU7/+2HXq7GRas/4= github.com/alecthomas/kong v0.4.0/go.mod h1:uzxf/HUh0tj43x1AyJROl3JT7SgsZ5m+icOv1csRhc0= github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48= @@ -22,6 +24,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04 h1:qXafrlZL1WsJW5OokjraLLRURHiw0OzKHD/RNdspp4w= +github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04/go.mod h1:FiwNQxz6hGoNFBC4nIx+CxZhI3nne5RmIOlT/MXcSD4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= @@ -50,6 +54,7 @@ golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= diff --git a/program/program_test.go b/program/program_test.go new file mode 100755 index 0000000..8715247 --- /dev/null +++ b/program/program_test.go @@ -0,0 +1,32 @@ +package program + +import ( + "bou.ke/monkey" + "github.com/stretchr/testify/assert" + "github.com/zenizh/go-capturer" + "os" + "testing" +) + +func TestOptions_Run(t *testing.T) { + var program Options + + exitValue := -1 + fakeExit := func(x int) { + exitValue = x + } + patch := monkey.Patch(os.Exit, fakeExit) + defer patch.Unpatch() + + out := capturer.CaptureStdout(func() { + + _, err := program.Parse([]string{"--version"}) + + assert.NoError(t, err) + + // version output is done as part of parsing, so we don't need to run the program + }) + + assert.Equal(t, exitValue, 0) + assert.Equal(t, "unknown\n", out) +} From b2de74e05c4229194ec7b46d458626b66e103ff9 Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Sun, 27 Feb 2022 20:18:42 -0500 Subject: [PATCH 10/22] test: add multiplatform tests in a separate workflow --- .github/workflows/build.yaml | 88 +++++++++++------------ .github/workflows/test-multiplatform.yaml | 34 +++++++++ 2 files changed, 78 insertions(+), 44 deletions(-) create mode 100755 .github/workflows/test-multiplatform.yaml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 59afd28..3a0e46a 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -1,44 +1,44 @@ -on: - push: - branches: - - "**" - -name: Build -env: - GO_VERSION: 1.17 - -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: ${{env.GO_VERSION}} - - - name: Vet - run: make vet - - - name: Test - run: make test - - - name: Build - run: make - - - format: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: ${{env.GO_VERSION}} - - - run: go fmt ./... - - - name: Verify No Differences after format - run: test -z "$(git diff)" +on: + push: + branches: + - "**" + +name: Build +env: + GO_VERSION: 1.17 + +jobs: + build: + name: Run checks and build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: ${{env.GO_VERSION}} + + - name: Vet + run: make vet + + - name: Test + run: make test + + - name: Build + run: make + + format: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: ${{env.GO_VERSION}} + + - run: go fmt ./... + + - name: Verify No Differences after format + run: test -z "$(git diff)" diff --git a/.github/workflows/test-multiplatform.yaml b/.github/workflows/test-multiplatform.yaml new file mode 100755 index 0000000..680f2fe --- /dev/null +++ b/.github/workflows/test-multiplatform.yaml @@ -0,0 +1,34 @@ +on: + # This is configured to run on pull requests. If desired you can change it to run on all pushes. + # be aware that windows and MacOS takes can take a while for setup and both of them incur extra expense for Actions. + pull_request: + # this will allow the tests to be run on demand + workflow_dispatch: + + +name: Test on all platforms + +env: + GO_VERSION: 1.17 + +jobs: + test: + if: ${{ true }} + name: Test on all platforms + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: + - ubuntu-latest + - macos-latest + - windows-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: ${{env.GO_VERSION}} + + - name: Test + run: go test ./... \ No newline at end of file From 7f2aafb8d9a5bf0514e83cfcdb30c4af221c32dd Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Fri, 3 Jun 2022 14:17:36 -0400 Subject: [PATCH 11/22] chore: Upgrade to golang 1.18 --- Dockerfile | 2 +- go.mod | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5a55dcf..bbcecd4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -ARG GO_VERSION=1.17 +ARG GO_VERSION=1.18 FROM golang:${GO_VERSION} as builder ARG PROGRAM=nothing ARG VERSION=development diff --git a/go.mod b/go.mod index d5a5616..ac7e5c5 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/deweysasser/golang-program -go 1.17 +go 1.18 require ( bou.ke/monkey v1.0.2 From d19e6c2e97067d641af49286b7e9d00509c8d0fe Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Fri, 3 Jun 2022 14:18:56 -0400 Subject: [PATCH 12/22] Upgrade minor versions of all dependencies --- go.mod | 8 ++++---- go.sum | 8 ++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index ac7e5c5..2644de8 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,10 @@ go 1.18 require ( bou.ke/monkey v1.0.2 - github.com/alecthomas/kong v0.4.0 + github.com/alecthomas/kong v0.5.0 github.com/mattn/go-colorable v0.1.12 github.com/rs/zerolog v1.26.1 - github.com/stretchr/testify v1.7.0 + github.com/stretchr/testify v1.7.1 github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04 ) @@ -16,6 +16,6 @@ require ( github.com/mattn/go-isatty v0.0.14 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect + golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 9beecc3..57824cb 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI= bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA= github.com/alecthomas/kong v0.4.0 h1:uj5Xe5RaoSRu2sUIy6XZk5QO9NaLU7/+2HXq7GRas/4= github.com/alecthomas/kong v0.4.0/go.mod h1:uzxf/HUh0tj43x1AyJROl3JT7SgsZ5m+icOv1csRhc0= +github.com/alecthomas/kong v0.5.0 h1:u8Kdw+eeml93qtMZ04iei0CFYve/WPcA5IFh+9wSskE= +github.com/alecthomas/kong v0.5.0/go.mod h1:uzxf/HUh0tj43x1AyJROl3JT7SgsZ5m+icOv1csRhc0= github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48= github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8= github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= @@ -23,6 +25,8 @@ github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04 h1:qXafrlZL1WsJW5OokjraLLRURHiw0OzKHD/RNdspp4w= github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04/go.mod h1:FiwNQxz6hGoNFBC4nIx+CxZhI3nne5RmIOlT/MXcSD4= @@ -44,6 +48,8 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -59,3 +65,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 08d0aebd177dfa68604d9ad697b30af0d541a230 Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Mon, 24 Oct 2022 18:28:20 -0400 Subject: [PATCH 13/22] build: fix changelog generation path --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b0651f1..113e35a 100755 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -72,7 +72,7 @@ jobs: - name: Generate Changelog for ${{ github.ref_name }} id: generate-changelog - run: make CHANGELOG.md + run: PATH="${PATH}:." make CHANGELOG.md - name: Create Release id: create_release From 9b53db76f8a156e2dd1fabe154770ef9f101243a Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Mon, 24 Oct 2022 19:03:37 -0400 Subject: [PATCH 14/22] build: made packaging a lot better. * now builds .zip or .tgz packages * intelligently names packages depending on the OS/ARCH --- .github/workflows/release.yml | 4 ++-- .gitignore | 2 ++ Makefile | 27 ++++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 113e35a..dab238e 100755 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -34,7 +34,7 @@ jobs: run: go test -v ./... - name: Build - run: make compile PROGRAM=output/${{steps.repo-name.outputs.name}}-${{env.GOOS}}-${{env.GOARCH}}${{matrix.ext}} + run: make package PROGRAM=bin/${{env.GOOS}}-${{env.GOARCH}}/${{steps.repo-name.outputs.name}}${{matrix.ext}} PACKAGE=dist/${{steps.repo-name.outputs.name}}-${{env.GOOS}}-${{env.GOARCH}}.zip env: GOOS: ${{matrix.GOOS}} GOARCH: ${{matrix.GOARCH}} @@ -43,7 +43,7 @@ jobs: uses: actions/upload-artifact@v2 with: name: artifacts - path: output + path: dist retention-days: 1 if-no-files-found: error diff --git a/.gitignore b/.gitignore index 34d6b74..b74256f 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ golang-program .idea CHANGELOG.md +dist +bin diff --git a/Makefile b/Makefile index 30c7724..a2a4add 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,23 @@ -ifeq ($(OS),Windows_NT) +ifeq ($(shell go env GOOS),windows) EXE=.exe else EXE= endif +DIST=dist +BINDIR=. + BASENAME=$(notdir $(shell pwd)) -PROGRAM=$(BASENAME)$(EXE) +PROGRAM=$(BINDIR)/$(BASENAME)$(EXE) LAST_RELEASE= REPO=$(shell go list | head -n 1) IMAGE=$(BASENAME) VERSION ?= $(shell git describe --tags --always --dirty) DOCKER=docker +PACKAGE=$(DIST)/$(basename $(notdir $(PROGRAM)))-$(shell go env GOOS)-$(shell go env GOARCH).zip + .PHONY: $(PROGRAM) @@ -20,9 +25,25 @@ all: $(PROGRAM) compile: $(PROGRAM) -$(PROGRAM): +$(PROGRAM): $(BINDIR) + mkdir -p $(dir $@) go build -ldflags="-X '$(REPO)/program.Version=${VERSION}'" -o $(PROGRAM) +package: $(PACKAGE) + +$(PACKAGE): $(PROGRAM) + +# These next 2 recipes know how to make .zip and .tar files, which are used implicitly in making the package +%.zip: + mkdir $(dir $@) + zip -j $@ $? + +%.tar.gz %.tgz: + mkdir $(dir $@) + tar -czf $@ -C $(dir $<) $(notdir $<) + + + install: go install -ldflags="-X '$(REPO)/program.Version=${VERSION}'" From 8a4c3bde8f28f5c6dbfe427f6e41d97c2f3d995a Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Mon, 24 Oct 2022 19:27:10 -0400 Subject: [PATCH 15/22] build: fix changelog repository URL capture --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a2a4add..97036b9 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,7 @@ CHANGELOG.md: .chglog/config.yml git chglog $(LAST_RELEASE) >$@ .chglog/config.yml: go.mod - sed -i.bak -e "s/repository_url:.*/repository_url: $(REPO)/" $@ + sed -i.bak -e "s|repository_url:.*|repository_url: https://$(REPO)|" $@ hooks: .git/hooks/pre-commit From bb5a1cbdba4c439342a550a8d0dfb9658ba262c2 Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Mon, 24 Oct 2022 19:28:48 -0400 Subject: [PATCH 16/22] chore: update to go 1.19 --- .github/workflows/build.yaml | 2 +- .github/workflows/release.yml | 2 +- .github/workflows/test-multiplatform.yaml | 2 +- Dockerfile | 2 +- go.mod | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 3a0e46a..a191701 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -5,7 +5,7 @@ on: name: Build env: - GO_VERSION: 1.17 + GO_VERSION: 1.19 jobs: build: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index dab238e..9995076 100755 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -5,7 +5,7 @@ on: tags: [ v* ] env: - GO_VERSION: 1.17 + GO_VERSION: 1.19 REPO: ${{github.repository}} jobs: diff --git a/.github/workflows/test-multiplatform.yaml b/.github/workflows/test-multiplatform.yaml index 680f2fe..ed17fd3 100755 --- a/.github/workflows/test-multiplatform.yaml +++ b/.github/workflows/test-multiplatform.yaml @@ -9,7 +9,7 @@ on: name: Test on all platforms env: - GO_VERSION: 1.17 + GO_VERSION: 1.19 jobs: test: diff --git a/Dockerfile b/Dockerfile index bbcecd4..bc4532d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -ARG GO_VERSION=1.18 +ARG GO_VERSION=1.19 FROM golang:${GO_VERSION} as builder ARG PROGRAM=nothing ARG VERSION=development diff --git a/go.mod b/go.mod index 2644de8..634b7b5 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/deweysasser/golang-program -go 1.18 +go 1.19 require ( bou.ke/monkey v1.0.2 From 09f3bbf91b59e44c1f22b29fa0abbf0ba3d97cd9 Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Mon, 24 Oct 2022 19:29:14 -0400 Subject: [PATCH 17/22] chore: update dependency versions --- go.mod | 12 ++++++------ go.sum | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 634b7b5..cf97765 100644 --- a/go.mod +++ b/go.mod @@ -4,18 +4,18 @@ go 1.19 require ( bou.ke/monkey v1.0.2 - github.com/alecthomas/kong v0.5.0 - github.com/mattn/go-colorable v0.1.12 - github.com/rs/zerolog v1.26.1 - github.com/stretchr/testify v1.7.1 + github.com/alecthomas/kong v0.6.1 + github.com/mattn/go-colorable v0.1.13 + github.com/rs/zerolog v1.28.0 + github.com/stretchr/testify v1.8.1 github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/mattn/go-isatty v0.0.14 // indirect + github.com/mattn/go-isatty v0.0.16 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect + golang.org/x/sys v0.1.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 57824cb..632129a 100644 --- a/go.sum +++ b/go.sum @@ -4,29 +4,45 @@ github.com/alecthomas/kong v0.4.0 h1:uj5Xe5RaoSRu2sUIy6XZk5QO9NaLU7/+2HXq7GRas/4 github.com/alecthomas/kong v0.4.0/go.mod h1:uzxf/HUh0tj43x1AyJROl3JT7SgsZ5m+icOv1csRhc0= github.com/alecthomas/kong v0.5.0 h1:u8Kdw+eeml93qtMZ04iei0CFYve/WPcA5IFh+9wSskE= github.com/alecthomas/kong v0.5.0/go.mod h1:uzxf/HUh0tj43x1AyJROl3JT7SgsZ5m+icOv1csRhc0= +github.com/alecthomas/kong v0.6.1 h1:1kNhcFepkR+HmasQpbiKDLylIL8yh5B5y1zPp5bJimA= +github.com/alecthomas/kong v0.6.1/go.mod h1:JfHWDzLmbh/puW6I3V7uWenoh56YNVONW+w8eKeUr9I= github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48= github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8= github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc= github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc= +github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY= +github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04 h1:qXafrlZL1WsJW5OokjraLLRURHiw0OzKHD/RNdspp4w= github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04/go.mod h1:FiwNQxz6hGoNFBC4nIx+CxZhI3nne5RmIOlT/MXcSD4= @@ -50,6 +66,9 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYe golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= From 235d0399e11d932b7b483f3dfd3c756254f417f2 Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Mon, 24 Oct 2022 19:33:47 -0400 Subject: [PATCH 18/22] chore: upgrade github actions to current versions * actions/checkout, etc from v2 -> v3 * change the set::output to the currently preferred pattern --- .github/workflows/build.yaml | 8 ++++---- .github/workflows/release.yml | 20 ++++++++++---------- .github/workflows/test-multiplatform.yaml | 4 ++-- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index a191701..a08c325 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -12,10 +12,10 @@ jobs: name: Run checks and build runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: go-version: ${{env.GO_VERSION}} @@ -31,10 +31,10 @@ jobs: format: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: go-version: ${{env.GO_VERSION}} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9995076..a4b7530 100755 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -19,16 +19,16 @@ jobs: - GOOS: windows ext: .exe steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: go-version: ${{ env.GO_VERSION}} - name: Repo Name id: repo-name - run: echo ::set-output name=name::$(basename ${{github.repository}}) + run: echo name=$(basename ${{github.repository}}) >> $GITHUB_OUTPUT - name: Test run: go test -v ./... @@ -40,7 +40,7 @@ jobs: GOARCH: ${{matrix.GOARCH}} - name: 'Upload Artifact' - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v3 with: name: artifacts path: dist @@ -52,11 +52,11 @@ jobs: needs: - build steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 with: fetch-depth: 0 - name: Download Artifacts - uses: actions/download-artifact@v2 + uses: actions/download-artifact@v3 - name: Install ChangeLog generator run: | @@ -87,17 +87,17 @@ jobs: name: Build the docker image steps: - name: Login to GitHub Container Registry - uses: docker/login-action@v1 + uses: docker/login-action@v2 with: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.GITHUB_TOKEN }} - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Docker Release to GHCR - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v3 with: context: . push: true @@ -111,7 +111,7 @@ jobs: # uncomment this if you're also using docker hub # - name: Login to Docker Container Registry # if: ${{ secrets.DOCKERHUB_TOKEN }} -# uses: docker/login-action@v1 +# uses: docker/login-action@v2 # with: # registry: ghcr.io # username: ${{ github.repository_owner }} diff --git a/.github/workflows/test-multiplatform.yaml b/.github/workflows/test-multiplatform.yaml index ed17fd3..42c4af0 100755 --- a/.github/workflows/test-multiplatform.yaml +++ b/.github/workflows/test-multiplatform.yaml @@ -23,10 +23,10 @@ jobs: - macos-latest - windows-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: go-version: ${{env.GO_VERSION}} From ea48b69447c77fa93fa2fca5ba9eb32b4354cb4b Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Fri, 4 Nov 2022 14:10:45 -0400 Subject: [PATCH 19/22] feat: pass the options to the run methods, so that all commands can get access to the full program options --- main.go | 2 +- program/program.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 5bdfeaa..ae1f61e 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ func main() { } // This ends up calling options.Run() - if err := context.Run(); err != nil { + if err := context.Run(options); err != nil { log.Err(err).Msg("Program failed") os.Exit(1) } diff --git a/program/program.go b/program/program.go index 870e55f..37a4688 100644 --- a/program/program.go +++ b/program/program.go @@ -37,7 +37,7 @@ func (program *Options) Parse(args []string) (*kong.Context, error) { } // Run runs the program -func (program *Options) Run() error { +func (program *Options) Run(options *Options) error { return nil } From 2f8054bc6d854c123c7896813592fb3db78590cc Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Fri, 4 Nov 2022 19:31:57 -0400 Subject: [PATCH 20/22] feat: improve security of docker runtime --- .gitignore | 1 + Dockerfile | 10 +++++----- Makefile | 8 ++++++-- main.go | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index b74256f..f76ae5f 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ golang-program CHANGELOG.md dist bin +.Dockerfile.tmp diff --git a/Dockerfile b/Dockerfile index bc4532d..488d643 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,9 +12,9 @@ RUN GOBIN=/output make install VERSION=$VERSION RUN PROGRAM=$(ls /output); echo "#!/bin/sh\nexec '/usr/bin/$PROGRAM' \"\$@\"" > /docker-entrypoint.sh && chmod +x /docker-entrypoint.sh -FROM alpine:latest -RUN apk add --no-cache libc6-compat ca-certificates +FROM gcr.io/distroless/base:latest +ARG PROGRAM=nothing -COPY --from=builder /output/* /usr/bin -COPY --from=builder /docker-entrypoint.sh /docker-entrypoint.sh -ENTRYPOINT ["/docker-entrypoint.sh"] +COPY --from=builder /output/${PROGRAM} / +USER 1000 +ENTRYPOINT [""] diff --git a/Makefile b/Makefile index 97036b9..99cb20c 100644 --- a/Makefile +++ b/Makefile @@ -48,8 +48,12 @@ install: go install -ldflags="-X '$(REPO)/program.Version=${VERSION}'" -image: Dockerfile - $(DOCKER) build --build-arg PROGRAM=$(BASENAME) --build-arg VERSION=$(VERSION) --build-arg BASENAME=$(BASENAME) -t $(IMAGE) . +image: .Dockerfile.tmp + $(DOCKER) build -f $< --build-arg PROGRAM=$(BASENAME) --build-arg VERSION=$(VERSION) --build-arg BASENAME=$(BASENAME) -t $(IMAGE) . + +.Dockerfile.tmp: Dockerfile + sed -e "s|^ENTRYPOINT.*|ENTRYPOINT [\"/${BASENAME}\"]|" < $< > $@.tmp + mv -f $@.tmp $@ test: go test -v ./... diff --git a/main.go b/main.go index ae1f61e..ec96f32 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ func main() { } // This ends up calling options.Run() - if err := context.Run(options); err != nil { + if err := context.Run(&options); err != nil { log.Err(err).Msg("Program failed") os.Exit(1) } From bc518f052773d116ba76f603b509f14b74217b39 Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Sat, 5 Nov 2022 01:20:09 -0400 Subject: [PATCH 21/22] fix: change to using make to generate the image --- .github/workflows/release.yml | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a4b7530..3e69798 100755 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -51,6 +51,7 @@ jobs: runs-on: ubuntu-latest needs: - build + - docker-build steps: - uses: actions/checkout@v3 with: @@ -96,17 +97,20 @@ jobs: - name: Checkout uses: actions/checkout@v3 - - name: Docker Release to GHCR - uses: docker/build-push-action@v3 - with: - context: . - push: true - build-args: | - GO_VERSION=${{ env.GO_VERSION }} - VERSION=${{ github.ref_name }} - tags: | - ghcr.io/${{env.REPO}}:latest - ghcr.io/${{env.REPO}}:${{ github.ref_name }} + - name: Checkout + uses: actions/checkout@v2 + + - name: Build Image + run: make image IMAGE=ghcr.io/${{env.REPO}}:${{ github.ref_name }} + + - name: Tag latest + run: docker tag ghcr.io/${{env.REPO}}:${{ github.ref_name }} ghcr.io/${{env.REPO}}:latest + + - name: Push + run: docker push ghcr.io/${{env.REPO}}:${{ github.ref_name }} + + - name: Push Latest + run: docker push ghcr.io/${{env.REPO}}:latest # uncomment this if you're also using docker hub # - name: Login to Docker Container Registry From 1f0fdf203f98dc0703051bc1bd1b9ef915a2daf8 Mon Sep 17 00:00:00 2001 From: Dewey Sasser Date: Wed, 9 Nov 2022 10:35:34 -0500 Subject: [PATCH 22/22] feat: create public releases (non-draft) by default --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3e69798..54480f8 100755 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -81,7 +81,7 @@ jobs: with: files: ./artifacts/* body_path: ./CHANGELOG.md - draft: true + draft: false docker-build: runs-on: ubuntu-latest