Skip to content

Commit

Permalink
[linting] Add golangci-lint and GHA that runs it (#36)
Browse files Browse the repository at this point in the history
## Summary

* Moves tests over to own workflow. Release jobs depend on it.
* Adds golangci linter and runs it in test workflows.
* Adds new pull request template.

## How was it tested?

GHA
  • Loading branch information
mikeland73 committed Aug 29, 2022
1 parent be97495 commit 14c47ea
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Summary

## How was it tested?
23 changes: 7 additions & 16 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: release

concurrency: deployment

on:
# Build/Release on demand
workflow_dispatch:
Expand All @@ -20,23 +22,12 @@ permissions:
contents: write

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout source code
uses: actions/checkout@v3
- name: Set up go
uses: actions/setup-go@v3
with:
go-version-file: ./go.mod
cache: true
- name: Build the module
run: go build -v ./...
- name: Run all tests
run: go test -v ./...
tests:
uses: ./.github/workflows/tests.yaml

release-snapshot:
runs-on: ubuntu-latest
needs: test
needs: tests
if: ${{ inputs.is_snapshot_release || github.event.schedule }}
steps:
- name: Checkout source code
Expand Down Expand Up @@ -71,7 +62,7 @@ jobs:
dist/*.tar.gz
release:
runs-on: ubuntu-latest
needs: test
needs: tests
# Only release when there's a tag for the release.
if: startsWith(github.ref, 'refs/tags/')
steps:
Expand Down
43 changes: 43 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: tests

concurrency:
group: ${{ github.ref }}
cancel-in-progress: true

on:
push:
branches-ignore:
- main
workflow_call:

permissions:
contents: read
pull-requests: read

jobs:
golangci-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up go
uses: actions/setup-go@v3
with:
go-version-file: ./go.mod
cache: false # use golangci cache instead
- name: golangci-lint
uses: golangci/golangci-lint-action@v3.2.0
with:
args: --timeout=10m

golang-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version-file: ./go.mod
cache: true
- name: Build the module
run: go build -v ./...
- name: Run all tests
run: go test -v ./...
53 changes: 53 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
run:
go: "1.19"
linters:
disable-all: true
enable:
- dupl
- errorlint
- errcheck
- gofmt
- goimports
- gosimple
- govet
- importas
- ineffassign
- misspell
- nilerr
- reassign
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- varnamelen
# - wrapcheck If we're going to use github.com/pkg/errors we should probably turn this on?

# We'd like to have the following linter enabled, but it's broken for Go
# 1.19 as of golangci-lint v1.48.0. Re-enable it when this issue is
# fixed: https://github.com/golangci/golangci-lint/issues/2649
# - structcheck
issues:
exclude:

linters-settings:
varnamelen:
max-distance: 10
ignore-decls:
- T any
- c echo.Context
- const C
- e error
- e watch.Event
- f *foo.Bar
- i int
- id string
- m map[string]any
- m map[string]int
- ns string
- r *http.Request
- t testing.T
- w http.ResponseWriter
- w io.Writer
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Instant, easy, and predictable shells and containers

[![Join Discord](https://img.shields.io/discord/903306922852245526?color=7389D8&label=discord&logo=discord&logoColor=ffffff)](https://discord.gg/agbskCJXk2) ![License: Apache 2.0](https://img.shields.io/github/license/jetpack-io/devbox) [![version](https://img.shields.io/github/v/release/jetpack-io/devbox?color=green&label=version&sort=semver)](https://github.com/jetpack-io/devbox/releases)
[![Join Discord](https://img.shields.io/discord/903306922852245526?color=7389D8&label=discord&logo=discord&logoColor=ffffff)](https://discord.gg/agbskCJXk2) ![License: Apache 2.0](https://img.shields.io/github/license/jetpack-io/devbox) [![version](https://img.shields.io/github/v/release/jetpack-io/devbox?color=green&label=version&sort=semver)](https://github.com/jetpack-io/devbox/releases) [![tests](https://github.com/jetpack-io/devbox/actions/workflows/tests.yaml/badge.svg)](https://github.com/jetpack-io/devbox/actions/workflows/tests.yaml)

---

Expand Down
2 changes: 1 addition & 1 deletion boxcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func RootCmd() *cobra.Command {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
_, err := exec.LookPath("nix-shell")
if err != nil {
return errors.New("Could not find nix in your PATH\nInstall nix by following the instructions at https://nixos.org/download.html and make sure you've set up your PATH correctly.")
return errors.New("could not find nix in your PATH\nInstall nix by following the instructions at https://nixos.org/download.html and make sure you've set up your PATH correctly")
}
return nil
},
Expand Down
4 changes: 2 additions & 2 deletions cuecfg/cuecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Marshal(v any, extension string) ([]byte, error) {

switch extension {
case ".json":
return MarshalJson(v)
return MarshalJSON(v)
case ".yml", ".yaml":
return MarshalYaml(v)
}
Expand All @@ -31,7 +31,7 @@ func Marshal(v any, extension string) ([]byte, error) {
func Unmarshal(data []byte, extension string, v any) error {
switch extension {
case ".json":
err := UnmarshalJson(data, v)
err := UnmarshalJSON(data, v)
if err != nil {
return errors.WithStack(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cuecfg/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import "encoding/json"
// TODO: consider using cue's JSON marshaller instead of
// "encoding/json" ... it might have extra functionality related
// to the cue language.
func MarshalJson(v interface{}) ([]byte, error) {
func MarshalJSON(v interface{}) ([]byte, error) {
return json.MarshalIndent(v, "", " ")
}

func UnmarshalJson(data []byte, v interface{}) error {
func UnmarshalJSON(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
6 changes: 3 additions & 3 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func generate(rootPath string, plan *planner.BuildPlan) error {

func writeFromTemplate(path string, plan *planner.BuildPlan, tmplName string) error {
embeddedPath := fmt.Sprintf("tmpl/%s.tmpl", tmplName)
t := template.Must(template.New(tmplName+".tmpl").Funcs(templateFuncs).ParseFS(tmplFS, embeddedPath))

// Should we clear the directory so we start "fresh"?
outPath := filepath.Join(path, tmplName)
Expand All @@ -54,14 +53,15 @@ func writeFromTemplate(path string, plan *planner.BuildPlan, tmplName string) er
return errors.WithStack(err)
}

t := template.Must(template.New(tmplName+".tmpl").Funcs(templateFuncs).ParseFS(tmplFS, embeddedPath))
return t.Execute(f, plan)
}

func toJson(a any) string {
func toJSON(a any) string {
data, _ := json.Marshal(a)
return string(data)
}

var templateFuncs = template.FuncMap{
"json": toJson,
"json": toJSON,
}

0 comments on commit 14c47ea

Please sign in to comment.