-
-
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 Go toolchain
install
command (#71)
As of Go version 1.16 `go install $pkg@$version` [1] allows to install commands without affecting the `main` module. Additionally commands like `go build` and `go test` no longer modify `go.mod` and `go.sum` files by default but report an error if a module requirement or checksum needs to be added or updated (as if the `-mod=readonly` flag were used). This can be used as alternative to the already existing `gobin` runner [2]. To support the `go install` command of the Go toolchain [3], a new `Task` [4] has been implemented in the new `install` [5] package that can be used through a Go toolchain `Runner` [6]. The task is customizable through the following functions: - `WithEnv(env map[string]string) install.Option` - sets the task specific environment. - `WithModulePath(path string) install.Option` - sets the module import path. - `WithModuleVersion(version *semver.Version) install.Option` - sets the module version. [1]: https://blog.golang.org/go116-module-changes#TOC_4. [2]: https://pkg.go.dev/github.com/svengreb/[email protected]/pkg/task/gobin [3]: https://pkg.go.dev/cmd/go#hdr-Compile_and_install_packages_and_dependencies [4]: https://pkg.go.dev/github.com/svengreb/wand/pkg/task#Task [5]: https://pkg.go.dev/github.com/svengreb/wand/pkg/task/golang/install [6]: https://pkg.go.dev/github.com/svengreb/wand/pkg/task/golang#Runner Closes GH-70
- Loading branch information
Showing
3 changed files
with
143 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) 2019-present Sven Greb <[email protected]> | ||
// This source code is licensed under the MIT license found in the LICENSE file. | ||
|
||
// Package install provides a task for the Go toolchain "install" command. | ||
// It requires at least Go version 1.16 which comes with support to install commands via `go install` (1) without | ||
// affecting the `main` module and will not "pollute" the `go.mod` file (2) anymore. | ||
// See https://pkg.go.dev/cmd/go#hdr-Compile_and_install_packages_and_dependencies for more details about the | ||
// `go install` command. | ||
// | ||
// References | ||
// | ||
// (1) https://blog.golang.org/go116-module-changes#TOC_4. | ||
// (2) https://blog.golang.org/go116-module-changes#TOC_3. | ||
package install | ||
|
||
import ( | ||
"github.com/svengreb/wand" | ||
"github.com/svengreb/wand/pkg/app" | ||
"github.com/svengreb/wand/pkg/task" | ||
) | ||
|
||
// Task is a task for the Go toolchain "install" command. | ||
// It requires at least Go version 1.16 which comes with support to install commands via `go install` (1) without | ||
// affecting the `main` module and will not "pollute" the `go.mod` file (2) anymore. | ||
// See https://pkg.go.dev/cmd/go#hdr-Compile_and_install_packages_and_dependencies for more details about the | ||
// `go install` command. | ||
// | ||
// References | ||
// | ||
// (1) https://blog.golang.org/go116-module-changes#TOC_4. | ||
// (2) https://blog.golang.org/go116-module-changes#TOC_3. | ||
type Task struct { | ||
ac app.Config | ||
opts *Options | ||
} | ||
|
||
// BuildParams builds the parameters. | ||
// Note that configured flags are applied after the "GOFLAGS" environment variable and could overwrite already defined | ||
// flags. | ||
// | ||
// See `go help environment`, `go help env` and the `go` command documentations for more details: | ||
// https://golang.org/cmd/go/#hdr-Environment_variables | ||
func (t *Task) BuildParams() []string { | ||
params := []string{"install"} | ||
|
||
params = append(params, t.opts.goModule.String()) | ||
|
||
return params | ||
} | ||
|
||
// Env returns the task specific environment. | ||
func (t *Task) Env() map[string]string { | ||
return t.opts.env | ||
} | ||
|
||
// Kind returns the task kind. | ||
func (t *Task) Kind() task.Kind { | ||
return task.KindExec | ||
} | ||
|
||
// Options returns the task options. | ||
func (t *Task) Options() task.Options { | ||
return *t.opts | ||
} | ||
|
||
// New creates a new task for the Go toolchain "install" 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, error) { | ||
opt, optErr := NewOptions(opts...) | ||
if optErr != nil { | ||
return nil, optErr | ||
} | ||
|
||
return &Task{ac: ac, opts: opt}, nil | ||
} |
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,66 @@ | ||
// Copyright (c) 2019-present Sven Greb <[email protected]> | ||
// This source code is licensed under the MIT license found in the LICENSE file. | ||
|
||
package install | ||
|
||
import ( | ||
"github.com/Masterminds/semver/v3" | ||
|
||
"github.com/svengreb/wand/pkg/project" | ||
) | ||
|
||
const ( | ||
// TaskName is the name of the task. | ||
TaskName = "go/install" | ||
) | ||
|
||
// 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 | ||
|
||
// goModule is the Go module identifier. | ||
goModule *project.GoModuleID | ||
} | ||
|
||
// NewOptions creates new task options. | ||
func NewOptions(opts ...Option) (*Options, error) { | ||
opt := &Options{ | ||
goModule: &project.GoModuleID{}, | ||
} | ||
for _, o := range opts { | ||
o(opt) | ||
} | ||
|
||
if opt.goModule.Version == nil { | ||
opt.goModule.IsLatest = true | ||
} | ||
|
||
return opt, nil | ||
} | ||
|
||
// WithEnv sets the task specific environment. | ||
func WithEnv(env map[string]string) Option { | ||
return func(o *Options) { | ||
o.env = env | ||
} | ||
} | ||
|
||
// WithModulePath sets the module import path. | ||
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) { | ||
o.goModule.Version = version | ||
} | ||
} |