-
-
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.
To use the Go toolchain, also known as the `go` command [1], a new caster [2], introduced in GH-14 [3], has been implemented. To unify the handling of errors in the cast [5] package, a new `ErrCast` [4] struct type has also been implemented. The `Validate` function [6] of the new caster returns an error of type `*cast.ErrCast` when the `go` binary executable does not exists at the configured path or when it is also not available in the executable search paths of the current environment. [1]: https://golang.org/cmd/go [2]: https://pkg.go.dev/github.com/svengreb/wand/pkg/cast#Caster [3]: #14 [4]: https://pkg.go.dev/github.com/svengreb/wand/pkg/cast#ErrCast [5]: https://pkg.go.dev/github.com/svengreb/wand/pkg/cast [6]: https://pkg.go.dev/github.com/svengreb/wand/pkg/cast#Validate GH-20
- Loading branch information
Showing
12 changed files
with
271 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2019-present Sven Greb <development@svengreb.de> | ||
// This source code is licensed under the MIT license found in the LICENSE file. | ||
|
||
// Package os provides utilities for operating system related operations and interactions. | ||
package os | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// EnvMapToSlice transforms a map of environment variable key/value pairs into a slice separated by an equal sign. | ||
func EnvMapToSlice(src map[string]string) []string { | ||
dst := make([]string, len(src)) | ||
for k, v := range src { | ||
dst = append(dst, fmt.Sprintf("%s=%s", k, v)) | ||
} | ||
return dst | ||
} | ||
|
||
// EnvSliceToMap transforms a slice of environment variables separated by an equal sign into a map. | ||
func EnvSliceToMap(src []string, dst map[string]string) { | ||
for _, envVar := range src { | ||
kv := strings.Split(envVar, "=") | ||
if len(kv) == 1 { | ||
dst[kv[0]] = "" | ||
} else { | ||
dst[kv[0]] = kv[1] | ||
} | ||
} | ||
} |
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,47 @@ | ||
// Copyright (c) 2019-present Sven Greb <development@svengreb.de> | ||
// This source code is licensed under the MIT license found in the LICENSE file. | ||
|
||
package cast | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
wErr "github.com/svengreb/wand/pkg/error" | ||
) | ||
|
||
const ( | ||
// ErrCasterValidation indicates that a caster validation failed. | ||
ErrCasterValidation = wErr.ErrString("caster validation failed") | ||
|
||
// ErrCasterSpellIncantationKindUnsupported indicates that a spell incantation kind is not supported by a caster. | ||
ErrCasterSpellIncantationKindUnsupported = wErr.ErrString("unsupported spell incantation kind") | ||
) | ||
|
||
// ErrCast represents a cast error. | ||
type ErrCast struct { | ||
// Err is a wrapped error. | ||
Err error | ||
// Kind is the error kind. | ||
Kind error | ||
} | ||
|
||
func (e *ErrCast) Error() string { | ||
msg := "cast error" | ||
if e.Kind != nil { | ||
msg = fmt.Sprintf("%s: %v", msg, e.Kind) | ||
} | ||
if e.Err != nil { | ||
msg = fmt.Sprintf("%s: %v", msg, e.Err) | ||
} | ||
|
||
return msg | ||
} | ||
|
||
// Is enables usage of errors.Is() to determine the kind of error that occurred. | ||
func (e *ErrCast) Is(err error) bool { | ||
return errors.Is(err, e.Kind) | ||
} | ||
|
||
// Unwrap returns the underlying error for usage with errors.Unwrap(). | ||
func (e *ErrCast) Unwrap() error { return e.Err } |
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,64 @@ | ||
// Copyright (c) 2019-present Sven Greb <development@svengreb.de> | ||
// This source code is licensed under the MIT license found in the LICENSE file. | ||
|
||
package toolchain | ||
|
||
import ( | ||
"github.com/magefile/mage/mg" | ||
) | ||
|
||
const ( | ||
// CasterName is the name of the Go toolchain command caster. | ||
CasterName = "golang" | ||
|
||
// DefaultEnvVarGO111MODULE is the default environment variable name to toggle the Go 1.11 module mode. | ||
DefaultEnvVarGO111MODULE = "GO111MODULE" | ||
|
||
// DefaultEnvVarGOBIN is the default environment variable name for the Go binary executable path. | ||
DefaultEnvVarGOBIN = "GOBIN" | ||
|
||
// DefaultEnvVarGOFLAGS is the default environment variable name for Go tool flags. | ||
DefaultEnvVarGOFLAGS = "GOFLAGS" | ||
|
||
// DefaultEnvVarGOPATH is the default environment variable name for the Go path. | ||
DefaultEnvVarGOPATH = "GOPATH" | ||
) | ||
|
||
// DefaultExec is the default path to the Go executable. | ||
var DefaultExec = mg.GoCmd() | ||
|
||
// Options stores Go toolchain command caster options. | ||
type Options struct { | ||
Env map[string]string | ||
Exec string | ||
} | ||
|
||
// Option is a Go toolchain command caster option. | ||
type Option func(*Options) | ||
|
||
// WithEnv sets the caster environment. | ||
func WithEnv(env map[string]string) Option { | ||
return func(o *Options) { | ||
o.Env = env | ||
} | ||
} | ||
|
||
// WithExec sets the path to the Go executable. | ||
func WithExec(nameOrPath string) Option { | ||
return func(o *Options) { | ||
o.Exec = nameOrPath | ||
} | ||
} | ||
|
||
// newOptions creates new Go toolchain command caster options. | ||
func newOptions(opts ...Option) *Options { | ||
opt := &Options{ | ||
Env: make(map[string]string), | ||
Exec: DefaultExec, | ||
} | ||
for _, o := range opts { | ||
o(opt) | ||
} | ||
|
||
return opt | ||
} |
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,89 @@ | ||
// Copyright (c) 2019-present Sven Greb <development@svengreb.de> | ||
// This source code is licensed under the MIT license found in the LICENSE file. | ||
|
||
package toolchain | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
|
||
"github.com/magefile/mage/sh" | ||
glFS "github.com/svengreb/golib/pkg/io/fs" | ||
|
||
"github.com/svengreb/wand/pkg/cast" | ||
"github.com/svengreb/wand/pkg/spell" | ||
) | ||
|
||
// Caster is a Go toolchain command caster. | ||
type Caster struct { | ||
opts *Options | ||
} | ||
|
||
// GetExec returns the path to the binary executable. | ||
func (c *Caster) GetExec() string { | ||
return c.opts.Exec | ||
} | ||
|
||
// Cast casts a spell incantation. | ||
// It returns an error of type *cast.ErrCast when the spell is not a spell.KindBinary and any other error that occurs | ||
// during the command execution. | ||
func (c *Caster) Cast(si spell.Incantation) error { | ||
if si.Kind() != spell.KindBinary { | ||
return &cast.ErrCast{ | ||
Err: fmt.Errorf("%q", si.Kind()), | ||
Kind: cast.ErrCasterSpellIncantationKindUnsupported, | ||
} | ||
} | ||
|
||
s, ok := si.(spell.Binary) | ||
if !ok { | ||
return &cast.ErrCast{ | ||
Err: fmt.Errorf("expected %q but got %q", s.Kind(), si.Kind()), | ||
Kind: cast.ErrCasterSpellIncantationKindUnsupported, | ||
} | ||
} | ||
|
||
args := si.Formula() | ||
for k, v := range s.Env() { | ||
c.opts.Env[k] = v | ||
} | ||
|
||
return sh.RunWithV(c.opts.Env, c.opts.Exec, args...) | ||
} | ||
|
||
// Handles returns the supported spell.Kind. | ||
func (c *Caster) Handles() spell.Kind { | ||
return spell.KindBinary | ||
} | ||
|
||
// Validate validates the Go toolchain command caster. | ||
// It returns an error of type *cast.ErrCast when the binary executable does not exists at the configured path and when | ||
// it is also not available in the executable search paths of the current environment. | ||
func (c *Caster) Validate() error { | ||
// Check if the Go executable exists,... | ||
execExits, fsErr := glFS.FileExists(c.opts.Exec) | ||
if fsErr != nil { | ||
return &cast.ErrCast{ | ||
Err: fmt.Errorf("caster %q: %w", CasterName, fsErr), | ||
Kind: cast.ErrCasterValidation, | ||
} | ||
} | ||
// ...otherwise try to look up the system-wide executable paths. | ||
if !execExits { | ||
path, pathErr := exec.LookPath(c.opts.Exec) | ||
if pathErr != nil { | ||
return &cast.ErrCast{ | ||
Err: fmt.Errorf("caster %q: %q not found or does not exist: %w", CasterName, c.opts.Exec, pathErr), | ||
Kind: cast.ErrCasterValidation, | ||
} | ||
} | ||
c.opts.Exec = path | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// NewCaster creates a new Go toolchain command caster. | ||
func NewCaster(opts ...Option) *Caster { | ||
return &Caster{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,12 @@ | ||
// Copyright (c) 2019-present Sven Greb <development@svengreb.de> | ||
// This source code is licensed under the MIT license found in the LICENSE file. | ||
|
||
// Package error provides types and utilities to handle errors. | ||
package error | ||
|
||
// ErrString is a string type for implementing constant errors. | ||
type ErrString string | ||
|
||
func (e ErrString) Error() string { | ||
return string(e) | ||
} |
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,6 @@ | ||
// Copyright (c) 2019-present Sven Greb <development@svengreb.de> | ||
// This source code is licensed under the MIT license found in the LICENSE file. | ||
|
||
// Package vcs provides packages utilities to interact with version control systems. | ||
// See https://en.wikipedia.org/wiki/Version_control for more details about VCS. | ||
package vcs |
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