-
Notifications
You must be signed in to change notification settings - Fork 1
/
dependency.go
83 lines (74 loc) · 2.07 KB
/
dependency.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
package deps
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var (
// ExternalDependencies define the external app dependencies needed
// to fullfill all automation tasks
ExternalDependencies = []*Dependency{
Golint,
}
)
// Installable describes something that can be installed
type Installable interface {
Install() error
}
// Dependency encapsulates attributes of a depending application
type Dependency struct {
Bin string
GoInstall string
}
// Install will install the dependency
func (d *Dependency) Install(ctx context.Context) (result bool, err error) {
if err = CheckDependencies(ctx, d); err != nil {
if os.IsNotExist(err) {
return InstallDependencies(ctx, d)
}
return true, nil
}
return false, nil
}
// CheckDependencies determines if provided dependencies are available.
// If binary is not available [os.ErrNotExist] is returned
func CheckDependencies(ctx context.Context, dependencies ...*Dependency) error {
if dependencies == nil || len(dependencies) == 0 {
dependencies = ExternalDependencies
}
for _, dep := range dependencies {
if _, err := exec.LookPath(dep.Bin); err != nil {
if errors.Is(err, exec.ErrNotFound) {
return &os.PathError{
Path: dep.Bin,
Err: os.ErrNotExist,
}
}
return err
}
}
return nil
}
// InstallDependencies makes sure to install provided dependencies
// and return if all installing all dependencies were successful
func InstallDependencies(ctx context.Context, dependencies ...*Dependency) (bool, error) {
var warnings error
if dependencies == nil || len(dependencies) == 0 {
dependencies = ExternalDependencies
}
for _, dep := range dependencies {
if dep.GoInstall != "" {
if err := sh.RunV(mg.GoCmd(), "install", dep.GoInstall); err != nil {
return false, fmt.Errorf("Dependency cannot be installed: %w", err)
}
continue
}
warnings = errors.Join(warnings, fmt.Errorf("Installation of '%s' not supported, yet. "+
"Thus installation needs to be handled, externally", dep.Bin))
}
return true, warnings
}