Skip to content

Commit 7c67d1e

Browse files
author
Brian Gibbins
authored
Download go modules before building (#20)
Fixes #14
1 parent c9fe696 commit 7c67d1e

File tree

1 file changed

+53
-9
lines changed

1 file changed

+53
-9
lines changed

internal/builder/main.go

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os/exec"
2222
"path/filepath"
2323
"text/template"
24+
"time"
2425

2526
"github.com/open-telemetry/opentelemetry-collector-builder/internal/scaffold"
2627
)
@@ -33,12 +34,17 @@ var (
3334
ErrGoNotFound = errors.New("Go binary not found")
3435
)
3536

36-
// GenerateAndCompile will generate the source files based on the given configuration and will compile it into a binary
37+
// GenerateAndCompile will generate the source files based on the given configuration, update go mod, and will compile into a binary
3738
func GenerateAndCompile(cfg Config) error {
3839
if err := Generate(cfg); err != nil {
3940
return err
4041
}
4142

43+
// run go get to update go.mod and go.sum files
44+
if err := GetModules(cfg); err != nil {
45+
return err
46+
}
47+
4248
return Compile(cfg)
4349
}
4450

@@ -83,15 +89,10 @@ func Generate(cfg Config) error {
8389

8490
// Compile generates a binary from the sources based on the configuration
8591
func Compile(cfg Config) error {
86-
goBinary := cfg.Distribution.Go
8792
// first, we test to check if we have Go at all
88-
if _, err := exec.Command(goBinary, "env").CombinedOutput(); err != nil {
89-
path, err := exec.LookPath("go")
90-
if err != nil {
91-
return ErrGoNotFound
92-
}
93-
goBinary = path
94-
cfg.Logger.Info("Using go from PATH", "Go executable", path)
93+
goBinary, err := getGoPath(cfg)
94+
if err != nil {
95+
return err
9596
}
9697

9798
cfg.Logger.Info("Compiling")
@@ -105,6 +106,49 @@ func Compile(cfg Config) error {
105106
return nil
106107
}
107108

109+
// GetModules retrieves the go modules, updating go.mod and go.sum in the process
110+
func GetModules(cfg Config) error {
111+
// first, we test to check if we have Go at all
112+
goBinary, err := getGoPath(cfg)
113+
if err != nil {
114+
return err
115+
}
116+
117+
cfg.Logger.Info("Getting go modules")
118+
cmd := exec.Command(goBinary, "mod", "download")
119+
cmd.Dir = cfg.Distribution.OutputPath
120+
121+
// basic retry if error from go mod command (in case of transient network error). This could be improved
122+
// retry 3 times with 5 second spacing interval
123+
retries := 3
124+
failReason := "unknown"
125+
for i := 1; i <= retries; i++ {
126+
if out, err := cmd.CombinedOutput(); err != nil {
127+
failReason = fmt.Sprintf("%s. Output: %q", err, out)
128+
cfg.Logger.Info("Failed modules download", "retry", fmt.Sprintf("%d/%d", i, retries))
129+
time.Sleep(5 * time.Second)
130+
continue
131+
}
132+
return nil
133+
}
134+
return fmt.Errorf("failed to download go modules: %s", failReason)
135+
}
136+
137+
// getGoPath checks if go is present and correct, and returns a useable go bin location
138+
func getGoPath(cfg Config) (string, error) {
139+
goBinary := cfg.Distribution.Go
140+
if _, err := exec.Command(goBinary, "env").CombinedOutput(); err != nil {
141+
path, err := exec.LookPath("go")
142+
if err != nil {
143+
return "", ErrGoNotFound
144+
}
145+
goBinary = path
146+
cfg.Logger.Info("Using go from PATH", "Go executable", path)
147+
}
148+
149+
return goBinary, nil
150+
}
151+
108152
func processAndWrite(cfg Config, tmpl string, outFile string, tmplParams interface{}) error {
109153
t, err := template.New("template").Parse(tmpl)
110154
if err != nil {

0 commit comments

Comments
 (0)