@@ -21,6 +21,7 @@ import (
21
21
"os/exec"
22
22
"path/filepath"
23
23
"text/template"
24
+ "time"
24
25
25
26
"github.com/open-telemetry/opentelemetry-collector-builder/internal/scaffold"
26
27
)
@@ -33,12 +34,17 @@ var (
33
34
ErrGoNotFound = errors .New ("Go binary not found" )
34
35
)
35
36
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
37
38
func GenerateAndCompile (cfg Config ) error {
38
39
if err := Generate (cfg ); err != nil {
39
40
return err
40
41
}
41
42
43
+ // run go get to update go.mod and go.sum files
44
+ if err := GetModules (cfg ); err != nil {
45
+ return err
46
+ }
47
+
42
48
return Compile (cfg )
43
49
}
44
50
@@ -83,15 +89,10 @@ func Generate(cfg Config) error {
83
89
84
90
// Compile generates a binary from the sources based on the configuration
85
91
func Compile (cfg Config ) error {
86
- goBinary := cfg .Distribution .Go
87
92
// 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
95
96
}
96
97
97
98
cfg .Logger .Info ("Compiling" )
@@ -105,6 +106,49 @@ func Compile(cfg Config) error {
105
106
return nil
106
107
}
107
108
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
+
108
152
func processAndWrite (cfg Config , tmpl string , outFile string , tmplParams interface {}) error {
109
153
t , err := template .New ("template" ).Parse (tmpl )
110
154
if err != nil {
0 commit comments