/
golang.go
127 lines (108 loc) · 3.48 KB
/
golang.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2019-present Sven Greb <development@svengreb.de>
// This source code is licensed under the MIT license found in the license file.
// Package golang provides Go toolchain tasks and runner.
//
// See https://golang.org/cmd/go for more details.
package golang
import (
"fmt"
"os/exec"
"github.com/magefile/mage/sh"
glFS "github.com/svengreb/golib/pkg/io/fs"
"github.com/svengreb/wand/pkg/task"
)
// Runner is a task runner for the Go toolchain.
type Runner struct {
opts *RunnerOptions
}
// FilePath returns the path to the runner executable.
func (r *Runner) FilePath() string {
return r.opts.Exec
}
// Handles returns the supported task kind.
func (r *Runner) Handles() task.Kind {
return task.KindExec
}
// Run runs the command.
// It returns an error of type *task.ErrRunner when any error occurs during the command execution.
func (r *Runner) Run(t task.Task) error {
tExec, tErr := r.prepareTask(t)
if tErr != nil {
return fmt.Errorf("runner %q: %w", RunnerName, tErr)
}
if r.opts.Quiet {
if err := sh.RunWith(r.opts.Env, r.opts.Exec, tExec.BuildParams()...); err != nil {
return &task.ErrRunner{
Err: fmt.Errorf("run task %q: %w", t.Name(), err),
Kind: task.ErrRun,
}
}
}
if err := sh.RunWithV(r.opts.Env, r.opts.Exec, tExec.BuildParams()...); err != nil {
return &task.ErrRunner{
Err: fmt.Errorf("run task %q: %w", t.Name(), err),
Kind: task.ErrRun,
}
}
return nil
}
// RunOut runs the command and returns its output.
// It returns an error of type *task.ErrRunner when any error occurs during the command execution.
func (r *Runner) RunOut(t task.Task) (string, error) {
tExec, tErr := r.prepareTask(t)
if tErr != nil {
return "", fmt.Errorf("runner %q: %w", RunnerName, tErr)
}
out, runErr := sh.OutputWith(r.opts.Env, r.opts.Exec, tExec.BuildParams()...)
if runErr != nil {
return "", &task.ErrRunner{
Err: fmt.Errorf("run task %q: %w", t.Name(), runErr),
Kind: task.ErrRun,
}
}
return out, nil
}
// Validate validates the command executable.
// It returns an error of type *task.ErrRunner when the executable does not exist and when it is also not available in
// the executable search path(s) of the current environment.
func (r *Runner) Validate() error {
// Check if the executable exists,...
execExits, fsErr := glFS.RegularFileExists(r.opts.Exec)
if fsErr != nil {
return &task.ErrRunner{
Err: fmt.Errorf("runner %q: %w", RunnerName, fsErr),
Kind: task.ErrRunnerValidation,
}
}
// ...otherwise try to look up the executable search path(s).
if !execExits {
path, pathErr := exec.LookPath(r.opts.Exec)
if pathErr != nil {
return &task.ErrRunner{
Err: fmt.Errorf("runner %q: %q not found in PATH: %w", RunnerName, r.opts.Exec, pathErr),
Kind: task.ErrRunnerValidation,
}
}
r.opts.Exec = path
}
return nil
}
// prepareTask checks if the given task is of type task.Exec and prepares the task specific environment.
// It returns an error of type *task.ErrRunner when any error occurs during the execution.
func (r *Runner) prepareTask(t task.Task) (task.Exec, error) {
tExec, ok := t.(task.Exec)
if t.Kind() != task.KindExec || !ok {
return nil, &task.ErrRunner{
Err: fmt.Errorf("expected %q but got %q", r.Handles(), t.Kind()),
Kind: task.ErrUnsupportedTaskKind,
}
}
for k, v := range tExec.Env() {
r.opts.Env[k] = v
}
return tExec, nil
}
// NewRunner creates a new Go toolchain command runner.
func NewRunner(opts ...RunnerOption) *Runner {
return &Runner{opts: NewRunnerOptions(opts...)}
}