Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .custom-gcl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: v2.0.2
destination: .
plugins:
- module: 'github.com/cschleiden/go-workflows/analyzer'
path: ./analyzer
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
vendor
plugin.so

web/app/node_modules
web/app/node_modules

custom-gcl
57 changes: 48 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
version: "2"

run:
tests: false
allow-parallel-runners: true

linters:
enable:
- goworkflows

linters-settings:
custom:
goworkflows:
path: ./plugin.so
description: go-workflows
original-url: github.com/cschleiden/go-workflows/analyzer
- bidichk
- bodyclose
- errcheck
- errname
- errorlint
- goprintffuncname
- govet
- importas
- ineffassign
- makezero
- prealloc
- predeclared
- promlinter
- rowserrcheck
- staticcheck
- tagalign
- testifylint
- tparallel
- unconvert
- usetesting
- wastedassign
- whitespace
- unused
- goworkflows
settings:
staticcheck:
checks:
- "all"
- "-ST1003"
custom:
goworkflows:
type: "module"
original-url: "github.com/cschleiden/go-workflows/analyzer"
settings:
checkprivatereturnvalues: true


formatters:
enable:
- gofmt
- goimports
settings:
goimports:
local-prefixes:
- "github.com/cschleiden/go-workflows"
9 changes: 6 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
// Some integration tests take quite long to run, increase the overall limit for now
"go.testFlags": ["-timeout", "120s", "-race", "-count", "1" , "-short"],
"go.testFlags": ["-timeout", "120s", "-race", "-count", "1", "-short"],
"files.exclude": {
"**/.git": true,
"**/.svn": true,
Expand All @@ -13,6 +13,9 @@
"github-actions.workflows.pinned.workflows": [],
"go.testExplorer.showDynamicSubtestsInEditor": true,
"go.lintTool": "golangci-lint-v2",
"Lua.diagnostics.globals": ["KEYS", "ARGV", "redis", "cjson"],

// "go.lintFlags": ["--path-mode=abs", "--fast-only"],
// "go.alternateTools": {
// "golangci-lint-v2": "${workspaceFolder}/custom-gcl"
// },
"Lua.diagnostics.globals": ["KEYS", "ARGV", "redis", "cjson"]
}
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod
GOFMT=gofmt
GOLINT=golangci-lint
CUSTOM_GOLINT=./custom-gcl

# Test parameters
TEST_TIMEOUT=240s
Expand Down Expand Up @@ -59,12 +60,16 @@ test-monoprocess:
# Run all backend tests
test-backends: test-redis test-mysql test-sqlite test-monoprocess

# Lint the code
lint:
custom-gcl:
@echo "Checking if golangci-lint is installed..."
@which $(GOLINT) > /dev/null || (echo "golangci-lint is not installed. Please install it first." && exit 1)
@echo "Building custom linter plugin..."
$(GOLINT) custom

# Lint the code
lint: custom-gcl
@echo "Running linter..."
$(GOLINT) run
$(CUSTOM_GOLINT) run

# Format the code
fmt:
Expand Down
16 changes: 15 additions & 1 deletion analyzer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,18 @@

This package implements a basic analyzer for checking various common workflow error conditions.

It can be used with golangci-lint as a custom linter to provide feedback in editors or in CI runs.
In your own .golangci.yaml configuration file, you can enable it like this:

```yaml
version: "2"

linters:
enable:
- goworkflows

settings:
custom:
goworkflows:
type: module
original-url: github.com/cschleiden/go-workflows/analyzer
```
50 changes: 39 additions & 11 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,55 @@ import (
"go/ast"
"go/types"

"github.com/golangci/plugin-module-register/register"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

var checkPrivateReturnValues bool
func init() {
register.Plugin("goworkflows", New)
}

func New() *analysis.Analyzer {
a := &analysis.Analyzer{
Name: "goworkflows",
Doc: "Checks for common errors when writing workflows",
Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
func New(settings any) (register.LinterPlugin, error) {
// The configuration type will be map[string]any or []interface, it depends on your configuration.
// You can use https://github.com/go-viper/mapstructure to convert map to struct.
s, err := register.DecodeSettings[Settings](settings)
if err != nil {
return nil, err
}

a.Flags.BoolVar(&checkPrivateReturnValues, "checkprivatereturnvalues", false, "Check return values of workflows which aren't exported")
return &GoWorkflowsPlugin{Settings: s}, nil
}

type GoWorkflowsPlugin struct {
Settings Settings
}

type Settings struct {
CheckPrivateReturnValues bool `json:"checkprivatereturnvalues"`
}

func (w *GoWorkflowsPlugin) BuildAnalyzers() ([]*analysis.Analyzer, error) {
return []*analysis.Analyzer{
{
Name: "goworkflows",
Doc: "Checks for common errors when writing workflows",
Run: w.run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
},
}, nil
}

func (w *GoWorkflowsPlugin) GetLoadMode() string {
// NOTE: the mode can be `register.LoadModeSyntax` or `register.LoadModeTypesInfo`.
// - `register.LoadModeSyntax`: if the linter doesn't use types information.
// - `register.LoadModeTypesInfo`: if the linter uses types information.

return a
return register.LoadModeSyntax
}

func run(pass *analysis.Pass) (interface{}, error) {
func (w *GoWorkflowsPlugin) run(pass *analysis.Pass) (interface{}, error) {
inspector := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

// Expect workflows to be top level functions in a file. Therefore it should be enough to just keep track if the current
Expand Down Expand Up @@ -84,7 +112,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
inWorkflow = true

// Check return types
if n.Name.IsExported() || checkPrivateReturnValues {
if n.Name.IsExported() || w.Settings.CheckPrivateReturnValues {
if n.Type.Results == nil || len(n.Type.Results.List) == 0 {
pass.Reportf(n.Pos(), "workflow `%v` doesn't return anything. needs to return at least `error`", n.Name.Name)
} else {
Expand Down
31 changes: 25 additions & 6 deletions analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,39 @@ package analyzer
import (
"testing"

"github.com/golangci/plugin-module-register/register"
"github.com/stretchr/testify/require"
"golang.org/x/tools/go/analysis/analysistest"
)

func TestAll(t *testing.T) {
a := New()
a.Flags.Set("checkprivatereturnvalues", "true")
analysistest.Run(t, analysistest.TestData(), a, "p", "q")
newPlugin, err := register.GetPlugin("goworkflows")
require.NoError(t, err)

plugin, err := newPlugin(map[string]any{
"checkprivatereturnvalues": true,
})
require.NoError(t, err)

analyzers, err := plugin.BuildAnalyzers()
require.NoError(t, err)

analysistest.Run(t, analysistest.TestData(), analyzers[0], "p", "q")
}

func TestComplex(t *testing.T) {
a := New()
a.Flags.Set("checkprivatereturnvalues", "true")
result := analysistest.Run(t, analysistest.TestData(), a, "q")
newPlugin, err := register.GetPlugin("goworkflows")
require.NoError(t, err)

plugin, err := newPlugin(map[string]any{
"checkprivatereturnvalues": true,
})
require.NoError(t, err)

analyzers, err := plugin.BuildAnalyzers()
require.NoError(t, err)

result := analysistest.Run(t, analysistest.TestData(), analyzers[0], "q")
for _, r := range result {
require.NoError(t, r.Err)
require.Equal(t, 1, len(r.Diagnostics))
Expand Down
17 changes: 17 additions & 0 deletions analyzer/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/cschleiden/go-workflows/analyzer

go 1.24.5

require (
github.com/golangci/plugin-module-register v0.1.2
github.com/stretchr/testify v1.10.0
golang.org/x/tools v0.37.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.28.0 // indirect
golang.org/x/sync v0.17.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
25 changes: 25 additions & 0 deletions analyzer/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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/golangci/plugin-module-register v0.1.2 h1:e5WM6PO6NIAEcij3B053CohVp3HIYbzSuP53UAYgOpg=
github.com/golangci/plugin-module-register v0.1.2/go.mod h1:1+QGTsKBvAIvPvoY/os+G5eoqxWn70HYDm2uvUyGuVw=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
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/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/mod v0.28.0 h1:gQBtGhjxykdjY9YhZpSlZIsbnaE2+PgjfLWUQTnoZ1U=
golang.org/x/mod v0.28.0/go.mod h1:yfB/L0NOf/kmEbXjzCPOx1iK1fRutOydrCMsqRhEBxI=
golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/telemetry v0.0.0-20250908211612-aef8a434d053/go.mod h1:+nZKN+XVh4LCiA9DV3ywrzN4gumyCnKjau3NGb9SGoE=
golang.org/x/tools v0.37.0 h1:DVSRzp7FwePZW356yEAChSdNcQo6Nsp+fex1SUW09lE=
golang.org/x/tools v0.37.0/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w=
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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16 changes: 0 additions & 16 deletions analyzer/plugin/plugin.go

This file was deleted.

16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/cschleiden/go-workflows

go 1.24
go 1.24.5

toolchain go1.24.6

Expand All @@ -11,7 +11,7 @@ require (
github.com/google/uuid v1.6.0
github.com/jellydator/ttlcache/v3 v3.0.0
github.com/redis/go-redis/v9 v9.0.2
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
go.opentelemetry.io/otel v1.31.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.31.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.31.0
Expand All @@ -31,7 +31,7 @@ require (
go.opentelemetry.io/otel/metric v1.31.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
golang.org/x/tools v0.37.0 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/grpc v1.67.1 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
Expand All @@ -55,11 +55,11 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/mod v0.28.0 // indirect
golang.org/x/net v0.44.0 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/sys v0.36.0 // indirect
golang.org/x/text v0.29.0 // indirect
google.golang.org/protobuf v1.35.1 // indirect
)

Expand Down
Loading
Loading