-
Notifications
You must be signed in to change notification settings - Fork 11
/
gofumpt.go
83 lines (65 loc) · 2.36 KB
/
gofumpt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package golang
import (
"fmt"
"github.com/talos-systems/kres/internal/dag"
"github.com/talos-systems/kres/internal/output/dockerfile"
"github.com/talos-systems/kres/internal/output/dockerfile/step"
"github.com/talos-systems/kres/internal/output/makefile"
"github.com/talos-systems/kres/internal/project/meta"
)
// Gofumpt provides gofumpt linter.
type Gofumpt struct {
dag.BaseNode
meta *meta.Options
GoVersion string `yaml:"goVersion"`
Version string `yaml:"version"`
}
// NewGofumpt builds Gofumpt node.
func NewGofumpt(meta *meta.Options) *Gofumpt {
meta.BuildArgs = append(meta.BuildArgs, "GOFUMPT_VERSION")
return &Gofumpt{
BaseNode: dag.NewBaseNode("lint-gofumpt"),
meta: meta,
GoVersion: "1.19",
Version: "v0.3.1",
}
}
// CompileMakefile implements makefile.Compiler.
func (lint *Gofumpt) CompileMakefile(output *makefile.Output) error {
output.Target("lint-gofumpt").Description("Runs gofumpt linter.").
Script("@$(MAKE) target-$@")
output.VariableGroup(makefile.VariableGroupCommon).
Variable(makefile.OverridableVariable("GOFUMPT_VERSION", lint.Version)).
Variable(makefile.OverridableVariable("GO_VERSION", lint.GoVersion))
output.Target("fmt").Description("Formats the source code").
Phony().
Script(
`@docker run --rm -it -v $(PWD):/src -w /src golang:$(GO_VERSION) \
bash -c "export GO111MODULE=on; export GOPROXY=https://proxy.golang.org; \
go install mvdan.cc/gofumpt@$(GOFUMPT_VERSION) && \
gofumpt -w ."`,
)
return nil
}
// ToolchainBuild implements common.ToolchainBuilder hook.
func (lint *Gofumpt) ToolchainBuild(stage *dockerfile.Stage) error {
stage.
Step(step.Arg("GOFUMPT_VERSION")).
Step(step.Script(fmt.Sprintf(
`go install mvdan.cc/gofumpt@${GOFUMPT_VERSION} \
&& mv /go/bin/gofumpt %s/gofumpt`, lint.meta.BinPath)))
return nil
}
// CompileDockerfile implements dockerfile.Compiler.
func (lint *Gofumpt) CompileDockerfile(output *dockerfile.Output) error {
output.Stage("lint-gofumpt").
Description("runs gofumpt").
From("base").
Step(step.Script(
`FILES="$(gofumpt -l .)" && test -z "${FILES}" || (echo -e "Source code is not formatted with 'gofumpt -w .':\n${FILES}"; exit 1)`,
))
return nil
}