forked from paketo-buildpacks/go-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_target_manager.go
83 lines (65 loc) · 1.76 KB
/
go_target_manager.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 gobuild
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
type GoTargetManager struct{}
func NewGoTargetManager() GoTargetManager {
return GoTargetManager{}
}
func (tm GoTargetManager) CleanAndValidate(inputTargets []string, workingDir string) ([]string, error) {
var targets []string
for _, t := range inputTargets {
if strings.HasPrefix(t, string(filepath.Separator)) {
return nil, fmt.Errorf("failed to determine build targets: %q is an absolute path, targets must be relative to the source directory", t)
}
target := filepath.Clean(t)
files, err := filepath.Glob(filepath.Join(workingDir, target, "*.go"))
if err != nil {
return nil, err
}
if len(files) == 0 {
return nil, fmt.Errorf("there were no *.go files present in %q", filepath.Join(workingDir, target))
}
targets = append(targets, fmt.Sprintf(".%c%s", filepath.Separator, target))
}
return targets, nil
}
func (tm GoTargetManager) GenerateDefaults(workingDir string) ([]string, error) {
files, err := filepath.Glob(filepath.Join(workingDir, "*.go"))
if err != nil {
return nil, err
}
if len(files) > 0 {
return []string{"."}, nil
}
var targets []string
err = filepath.Walk(filepath.Join(workingDir, "cmd"), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
match, err := filepath.Match("*.go", info.Name())
if err != nil {
return err
}
if match {
targets = append(targets, strings.ReplaceAll(filepath.Dir(path), workingDir, "."))
return filepath.SkipDir
}
}
return nil
})
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
}
if len(targets) == 0 {
return nil, errors.New("no *.go files could be found")
}
return targets, nil
}