From e3ba99d6666872927211bff816eb0337fe1ffcaf Mon Sep 17 00:00:00 2001 From: Jared Allard Date: Fri, 23 Feb 2024 15:14:33 -0800 Subject: [PATCH] chore: project scaffolding --- .github/FUNDING.yml | 1 + .github/pull_request_template.md | 14 ++++++++ .github/workflows/golangci-lint.yaml | 54 ++++++++++++++++++++++++++++ .github/workflows/release.yaml | 46 ++++++++++++++++++++++++ .gitignore | 3 ++ .mise.toml | 16 +++++++++ internal/parser/parser.go | 22 +++++------- 7 files changed, 142 insertions(+), 14 deletions(-) create mode 100644 .github/FUNDING.yml create mode 100644 .github/pull_request_template.md create mode 100644 .github/workflows/golangci-lint.yaml create mode 100644 .github/workflows/release.yaml create mode 100644 .gitignore diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..dc123d7 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: jaredallard diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..d14aca6 --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,14 @@ + + + +## What this PR does / why we need it + + + +## Notes for your reviewers diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml new file mode 100644 index 0000000..cb94488 --- /dev/null +++ b/.github/workflows/golangci-lint.yaml @@ -0,0 +1,54 @@ +name: tests +on: + push: + branches: + - main + pull_request: + +permissions: + contents: read + +concurrency: + group: stencil-build-${{ github.head_ref }} + cancel-in-progress: true + +jobs: + gotest: + name: go test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: jdx/mise-action@v2 + with: + experimental: true + env: + GH_TOKEN: ${{ github.token }} + - name: Run go test + run: | + gotestsum -f github-actions -- -coverprofile=cover.out ./... + - name: Upload test coverage + uses: codecov/codecov-action@v4.0.1 + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: ./cover.out + fail_ci_if_error: true + + golangci-lint: + name: golangci-lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: jdx/mise-action@v2 + with: + experimental: true + env: + GH_TOKEN: ${{ github.token }} + - name: Retrieve golangci-lint version + run: | + echo "version=$(mise current golangci-lint)" >> "$GITHUB_OUTPUT" + id: golangci_lint + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v${{ steps.golangci_lint.outputs.version }} + args: --timeout=30m diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..84b0a1c --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,46 @@ +name: goreleaser + +on: + push: + # Run only on tags + tags: + - "*" + +permissions: + contents: write + packages: write + +concurrency: + group: stencil-release-${{ github.head_ref }} + cancel-in-progress: true + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - run: git fetch --force --tags + - uses: jdx/mise-action@v2 + with: + experimental: true + env: + GH_TOKEN: ${{ github.token }} + - name: Retrieve goreleaser version + run: | + echo "version=$(mise current goreleaser)" >> "$GITHUB_OUTPUT" + id: goreleaser + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - uses: goreleaser/goreleaser-action@v5 + with: + distribution: goreleaser + version: v${{ steps.goreleaser.outputs.version }} + args: release --clean + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8921ca7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bin +*.out +.DS_Store diff --git a/.mise.toml b/.mise.toml index bcf4d36..034eff6 100644 --- a/.mise.toml +++ b/.mise.toml @@ -1,2 +1,18 @@ [tools] golang = "1.22" +golangci-lint = "1.56.2" +"go:gotest.tools/gotestsum" = "v1.11.0" +"go:golang.org/x/tools/cmd/goimports" = "latest" + +[tasks.tests] +description = "Run tests" +run = ["gotestsum", "golangci-lint run --allow-parallel-runners --fast"] + +[tasks.fmt] +alias = "format" +description = "Format code" +run = ["goimports -w ."] + +[tasks.build] +description = "Build the project" +run = ["go mod download", "go build -trimpath -o bin/ ./cmd/..."] diff --git a/internal/parser/parser.go b/internal/parser/parser.go index ca1a3d9..6e57e8d 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -19,7 +19,6 @@ package parser import ( "bufio" - "errors" "fmt" "io" "reflect" @@ -57,18 +56,13 @@ func parseColonDocuments(r io.Reader) ([]map[string]string, error) { cur[parts[0]] = parts[1] } if scanner.Err() != nil { - if errors.Is(scanner.Err(), io.EOF) { - // Last entry, if cur is not empty (already had a newline?) append - // it. - if len(cur) > 0 { - out = append(out, cur) - } - return out, nil - } - return nil, scanner.Err() } + // If we ended with a non-empty document, append it to the output now. + if len(cur) > 0 { + out = append(out, cur) + } return out, nil } @@ -153,6 +147,7 @@ func (pkg *Package) EncodeInto(w io.Writer) error { return nil } +// ParsePackages parses the provided reader into a slice of packages. func ParsePackages(r io.Reader) (Packages, error) { docs, err := parseColonDocuments(r) if err != nil { @@ -202,13 +197,12 @@ func ParsePackages(r io.Reader) (Packages, error) { // Remove the field from the map so we can check for missing // fields later. - delete(tagToField, k) - + delete(doc, k) } // Check for missing fields - if len(tagToField) > 0 { - return nil, fmt.Errorf("unknown fields: %v", tagToField) + if len(doc) > 0 { + return nil, fmt.Errorf("unknown fields: %v", doc) } pkgs = append(pkgs, pkg)