-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task for "mvdan.cc/gofumpt" Go module command (#57)
The "mvdan.cc/gofumpt" [1] Go module provides the `gofumpt` command, a tool that enforces a stricter format than "https://pkg.go.dev/cmd/gofmt" and provides additional rules [2], while being backwards compatible. It is a modified fork of "https://pkg.go.dev/cmd/gofmt" so it can be used as a drop-in replacement. To configure and run the `gofumpt` command, a new `task.GoModule` [3] has been implemented in the new "gofumpt" [4] package that can be run using the "gobin" command runner [5] or any other command runner [6] that handles tasks of kind `KindGoModule` [7]. The task is customizable through the following functions: - `WithEnv(map[string]string) gofumpt.Option` - sets the task specific environment. - `WithExtraArgs(...string) gofumpt.Option` - sets additional arguments to pass to the command. - `WithExtraRules(bool) gofumpt.Option` - indicates whether gofumpt's extra rules should be enabled. See the [repository documentation for a listing of available rules][2]. - `WithListNonCompliantFiles(bool) gofumpt.Option` - indicates whether files, whose formatting are not conform to the style guide, are listed. - `WithModulePath(string) gofumpt.Option` - sets the module import path. - `WithModuleVersion(*semver.Version) gofumpt.Option` - sets the module version. - `WithPaths(...string) gofumpt.Option` - sets the paths to search for Go source files. By default all directories are scanned recursively starting from the current working directory. - `WithReportAllErrors(bool) gofumpt.Option` - indicates whether all errors should be printed instead of only the first 10 on different lines. - `WithSimplify(bool) gofumpt.Option` - indicates whether code should be simplified. The "elder" reference implementation provides the new `Gofumpt` method [8]. [1]: https://pkg.go.dev/mvdan.cc/gofumpt [2]: https://github.com/mvdan/gofumpt#added-rules [3]: https://pkg.go.dev/github.com/svengreb/wand/pkg/task#GoModule [4]: https://pkg.go.dev/github.com/svengreb/wand/pkg/task/gofumpt [5]: https://pkg.go.dev/github.com/svengreb/wand/pkg/task/gobin#Runner [6]: https://pkg.go.dev/github.com/svengreb/wand/pkg/task#Runner [7]: https://pkg.go.dev/github.com/svengreb/wand/pkg/task#KindGoModule [8]: https://pkg.go.dev/github.com/svengreb/wand/pkg/elder#Elder.Gofumpt Closes GH-56
- Loading branch information
Showing
12 changed files
with
300 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright (c) 2019-present Sven Greb <development@svengreb.de> | ||
// This source code is licensed under the MIT license found in the LICENSE file. | ||
|
||
// Package gofumpt provides a task for the "mvdan.cc/gofumpt" Go module command. | ||
// "gofumpt" enforce a stricter format than "https://pkg.go.dev/cmd/gofmt" and provides additional rules, while being | ||
// backwards compatible. | ||
// It is a modified fork of "https://pkg.go.dev/cmd/gofmt" so it can be used as a drop-in replacement. | ||
// | ||
// See https://pkg.go.dev/mvdan.cc/gofumpt for more details about "gofumpt". | ||
// The source code of "gofumpt" is available at https://github.com/mvdan/gofumpt. | ||
// See https://github.com/mvdan/gofumpt#added-rules for more details about available rules. | ||
package gofumpt | ||
|
||
import ( | ||
"github.com/svengreb/wand" | ||
"github.com/svengreb/wand/pkg/app" | ||
"github.com/svengreb/wand/pkg/project" | ||
"github.com/svengreb/wand/pkg/task" | ||
) | ||
|
||
// Task is a task for the "mvdan.cc/gofumpt" Go module command. | ||
// "gofumpt" enforce a stricter format than "https://pkg.go.dev/cmd/gofmt", while being backwards compatible, | ||
// and provides additional rules. | ||
// It is a modified fork of "https://pkg.go.dev/cmd/gofmt" so it can be used as a drop-in replacement. | ||
// | ||
// See https://pkg.go.dev/mvdan.cc/gofumpt for more details about "gofumpt". | ||
// The source code of "gofumpt" is available at https://github.com/mvdan/gofumpt. | ||
// See https://github.com/mvdan/gofumpt#added-rules for more details about available rules. | ||
type Task struct { | ||
ac app.Config | ||
opts *Options | ||
} | ||
|
||
// BuildParams builds the parameters. | ||
func (t *Task) BuildParams() []string { | ||
var params []string | ||
|
||
// Enable gofumpt's extra rules. | ||
if t.opts.extraRules { | ||
params = append(params, "-extra") | ||
} | ||
|
||
// List files whose formatting are non-compliant to gofumpt's styles. | ||
if t.opts.listNonCompliantFiles { | ||
params = append(params, "-l") | ||
} | ||
|
||
// Write result to source files instead of stdout. | ||
if t.opts.persistChanges { | ||
params = append(params, "-w") | ||
} | ||
|
||
// Report all errors and not just the first 10 on different lines. | ||
if t.opts.reportAllErrors { | ||
params = append(params, "-e") | ||
} | ||
|
||
if t.opts.simplify { | ||
params = append(params, "-s") | ||
} | ||
|
||
// Include additionally configured arguments. | ||
params = append(params, t.opts.extraArgs...) | ||
|
||
// Only search in specified paths for Go source files... | ||
if len(t.opts.paths) > 0 { | ||
params = append(params, t.opts.paths...) | ||
} else { | ||
// ...or otherwise search recursively starting from the working directory of the current process. | ||
params = append(params, ".") | ||
} | ||
|
||
return params | ||
} | ||
|
||
// Env returns the task specific environment. | ||
func (t *Task) Env() map[string]string { | ||
return t.opts.env | ||
} | ||
|
||
// ID returns the identifier of the Go module. | ||
func (t *Task) ID() *project.GoModuleID { | ||
return t.opts.goModule | ||
} | ||
|
||
// Kind returns the task kind. | ||
func (t *Task) Kind() task.Kind { | ||
return task.KindGoModule | ||
} | ||
|
||
// Options returns the task options. | ||
func (t *Task) Options() task.Options { | ||
return *t.opts | ||
} | ||
|
||
// New creates a new task for the "mvdan.cc/gofumpt" Go module command. | ||
//nolint:gocritic // The app.Config struct is passed as value by design to ensure immutability. | ||
func New(wand wand.Wand, ac app.Config, opts ...Option) *Task { | ||
return &Task{ac: ac, opts: NewOptions(opts...)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// Copyright (c) 2019-present Sven Greb <development@svengreb.de> | ||
// This source code is licensed under the MIT license found in the LICENSE file. | ||
|
||
package gofumpt | ||
|
||
import ( | ||
"github.com/Masterminds/semver/v3" | ||
|
||
"github.com/svengreb/wand/pkg/project" | ||
) | ||
|
||
const ( | ||
// DefaultGoModulePath is the default module import path. | ||
DefaultGoModulePath = "mvdan.cc/gofumpt" | ||
|
||
// TaskName is the name of the task. | ||
TaskName = "gofumpt" | ||
) | ||
|
||
// Option is a task option. | ||
type Option func(*Options) | ||
|
||
// Options are task options. | ||
type Options struct { | ||
// env is the task specific environment. | ||
env map[string]string | ||
|
||
// extraArgs are additional arguments passed to the command. | ||
extraArgs []string | ||
|
||
// extraRules indicates whether gofumpt's extra rules should be enabled. | ||
// See https://github.com/mvdan/gofumpt#added-rules for more details about available rules. | ||
extraRules bool | ||
|
||
// goModule is the Go module identifier. | ||
goModule *project.GoModuleID | ||
|
||
// listNonCompliantFiles indicates whether files, whose formatting are not conform to the style guide, should be | ||
// listed. | ||
listNonCompliantFiles bool | ||
|
||
// paths are the paths to search for Go source files. | ||
// By default all directories are scanned recursively starting from the working directory of the current process. | ||
paths []string | ||
|
||
// persistChanges indicates whether results are written to the source files instead of standard output. | ||
persistChanges bool | ||
|
||
// reportAllErrors indicates whether all errors should be printed instead of only the first 10 on different lines. | ||
reportAllErrors bool | ||
|
||
// simplify indicates whether code should be simplified. | ||
simplify bool | ||
} | ||
|
||
// NewOptions creates new task options. | ||
func NewOptions(opts ...Option) *Options { | ||
opt := &Options{ | ||
env: make(map[string]string), | ||
goModule: &project.GoModuleID{ | ||
Path: DefaultGoModulePath, | ||
IsLatest: true, | ||
}, | ||
} | ||
for _, o := range opts { | ||
o(opt) | ||
} | ||
|
||
return opt | ||
} | ||
|
||
// WithEnv sets the task specific environment. | ||
func WithEnv(env map[string]string) Option { | ||
return func(o *Options) { | ||
o.env = env | ||
} | ||
} | ||
|
||
// WithExtraArgs sets additional arguments to pass to the command. | ||
func WithExtraArgs(extraArgs ...string) Option { | ||
return func(o *Options) { | ||
o.extraArgs = append(o.extraArgs, extraArgs...) | ||
} | ||
} | ||
|
||
// WithExtraRules indicates whether gofumpt's extra rules should be enabled. | ||
// See https://github.com/mvdan/gofumpt#added-rules for more details about available rules. | ||
func WithExtraRules(extraRules bool) Option { | ||
return func(o *Options) { | ||
o.extraRules = extraRules | ||
} | ||
} | ||
|
||
// WithListNonCompliantFiles indicates whether files, whose formatting are not conform to the style guide, are listed. | ||
func WithListNonCompliantFiles(listNonCompliantFiles bool) Option { | ||
return func(o *Options) { | ||
o.listNonCompliantFiles = listNonCompliantFiles | ||
} | ||
} | ||
|
||
// WithModulePath sets the module import path. | ||
// Defaults to DefaultGoModulePath. | ||
func WithModulePath(path string) Option { | ||
return func(o *Options) { | ||
if path != "" { | ||
o.goModule.Path = path | ||
} | ||
} | ||
} | ||
|
||
// WithModuleVersion sets the module version. | ||
func WithModuleVersion(version *semver.Version) Option { | ||
return func(o *Options) { | ||
if version != nil { | ||
o.goModule.Version = version | ||
} | ||
} | ||
} | ||
|
||
// WithPaths sets the paths to search for Go source files. | ||
// By default all directories are scanned recursively starting from the working directory of the current process. | ||
func WithPaths(paths ...string) Option { | ||
return func(o *Options) { | ||
o.paths = append(o.paths, paths...) | ||
} | ||
} | ||
|
||
// WithPersistedChanges indicates whether results are written to the source files instead of standard output. | ||
func WithPersistedChanges(persistChanges bool) Option { | ||
return func(o *Options) { | ||
o.persistChanges = persistChanges | ||
} | ||
} | ||
|
||
// WithReportAllErrors indicates whether all errors should be printed instead of only the first 10 on different lines. | ||
func WithReportAllErrors(reportAllErrors bool) Option { | ||
return func(o *Options) { | ||
o.reportAllErrors = reportAllErrors | ||
} | ||
} | ||
|
||
// WithSimplify indicates whether code should be simplified. | ||
func WithSimplify(simplify bool) Option { | ||
return func(o *Options) { | ||
o.simplify = simplify | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.