@@ -33,7 +33,7 @@ import (
33
33
34
34
// updateGomodWithTaggedDependencies gets the dependencies at the given tag and fills go.mod and go.sum.
35
35
// If anything is changed, it commits the changes. Returns true if go.mod changed.
36
- func updateGomodWithTaggedDependencies (tag string , depsRepo []string ) (bool , error ) {
36
+ func updateGomodWithTaggedDependencies (tag string , depsRepo []string , semverTag bool ) (bool , error ) {
37
37
found := map [string ]bool {}
38
38
changed := false
39
39
@@ -59,10 +59,14 @@ func updateGomodWithTaggedDependencies(tag string, depsRepo []string) (bool, err
59
59
return changed , fmt .Errorf ("failed to get tag %s for %q: %v" , tag , depPkg , err )
60
60
}
61
61
rev := commit .String ()
62
- pseudoVersion := fmt .Sprintf ("v0.0.0-%s-%s" , commitTime .UTC ().Format ("20060102150405" ), rev [:12 ])
62
+ pseudoVersionOrTag := fmt .Sprintf ("v0.0.0-%s-%s" , commitTime .UTC ().Format ("20060102150405" ), rev [:12 ])
63
63
64
- // in case the pseudoVersion has not changed, running go mod download will help
65
- // in avoiding packaging it up if the pseudoVersion has been published already
64
+ if semverTag {
65
+ pseudoVersionOrTag = tag
66
+ }
67
+
68
+ // in case the pseudoVersion/tag has not changed, running go mod download will help
69
+ // in avoiding packaging it up if the pseudoVersion/tag has been published already
66
70
downloadCommand := exec .Command ("go" , "mod" , "download" )
67
71
downloadCommand .Env = append (os .Environ (), "GO111MODULE=on" , fmt .Sprintf ("GOPRIVATE=%s" , depPackages ), "GOPROXY=https://proxy.golang.org" )
68
72
downloadCommand .Stdout = os .Stdout
@@ -71,46 +75,46 @@ func updateGomodWithTaggedDependencies(tag string, depsRepo []string) (bool, err
71
75
return changed , fmt .Errorf ("error running go mod download for %s: %v" , depPkg , err )
72
76
}
73
77
74
- // check if we have the pseudoVersion published already. if we don't, package it up
78
+ // check if we have the pseudoVersion/tag published already. if we don't, package it up
75
79
// and save to local mod download cache.
76
- if err := packageDepToGoModCache (depPath , depPkg , rev , pseudoVersion , commitTime ); err != nil {
80
+ if err := packageDepToGoModCache (depPath , depPkg , rev , pseudoVersionOrTag , commitTime ); err != nil {
77
81
return changed , fmt .Errorf ("failed to package %s dependency: %v" , depPkg , err )
78
82
}
79
83
80
- requireCommand := exec .Command ("go" , "mod" , "edit" , "-fmt" , "-require" , fmt .Sprintf ("%s@%s" , depPkg , pseudoVersion ))
84
+ requireCommand := exec .Command ("go" , "mod" , "edit" , "-fmt" , "-require" , fmt .Sprintf ("%s@%s" , depPkg , pseudoVersionOrTag ))
81
85
requireCommand .Env = append (os .Environ (), "GO111MODULE=on" )
82
86
requireCommand .Stdout = os .Stdout
83
87
requireCommand .Stderr = os .Stderr
84
88
if err := requireCommand .Run (); err != nil {
85
- return changed , fmt .Errorf ("unable to pin %s in the require section of go.mod to %s: %v" , depPkg , pseudoVersion , err )
89
+ return changed , fmt .Errorf ("unable to pin %s in the require section of go.mod to %s: %v" , depPkg , pseudoVersionOrTag , err )
86
90
}
87
91
88
- replaceCommand := exec .Command ("go" , "mod" , "edit" , "-fmt" , "-replace" , fmt .Sprintf ("%s=%s@%s" , depPkg , depPkg , pseudoVersion ))
92
+ replaceCommand := exec .Command ("go" , "mod" , "edit" , "-fmt" , "-replace" , fmt .Sprintf ("%s=%s@%s" , depPkg , depPkg , pseudoVersionOrTag ))
89
93
replaceCommand .Env = append (os .Environ (), "GO111MODULE=on" )
90
94
replaceCommand .Stdout = os .Stdout
91
95
replaceCommand .Stderr = os .Stderr
92
96
if err := replaceCommand .Run (); err != nil {
93
- return changed , fmt .Errorf ("unable to pin %s in the replace section of go.mod to %s: %v" , depPkg , pseudoVersion , err )
97
+ return changed , fmt .Errorf ("unable to pin %s in the replace section of go.mod to %s: %v" , depPkg , pseudoVersionOrTag , err )
94
98
}
95
99
96
100
downloadCommand2 := exec .Command ("go" , "mod" , "download" )
97
101
downloadCommand2 .Env = append (os .Environ (), "GO111MODULE=on" , fmt .Sprintf ("GOPRIVATE=%s" , depPackages ), "GOPROXY=https://proxy.golang.org" )
98
102
downloadCommand2 .Stdout = os .Stdout
99
103
downloadCommand2 .Stderr = os .Stderr
100
104
if err := downloadCommand2 .Run (); err != nil {
101
- return changed , fmt .Errorf ("error running go mod download for pseudo-version %s for %s: %v" , pseudoVersion , depPkg , err )
105
+ return changed , fmt .Errorf ("error running go mod download for pseudo-version %s for %s: %v" , pseudoVersionOrTag , depPkg , err )
102
106
}
103
107
104
108
tidyCommand := exec .Command ("go" , "mod" , "tidy" )
105
- tidyCommand .Env = append (os .Environ (), "GO111MODULE=on" , "GOPOXY =file://${GOPATH} /pkg/mod/cache/download" )
109
+ tidyCommand .Env = append (os .Environ (), "GO111MODULE=on" , fmt . Sprintf ( "GOPROXY =file://%s /pkg/mod/cache/download", os . Getenv ( "GOPATH" )) )
106
110
tidyCommand .Stdout = os .Stdout
107
111
tidyCommand .Stderr = os .Stderr
108
112
if err := tidyCommand .Run (); err != nil {
109
113
return changed , fmt .Errorf ("unable to run go mod tidy for %s at %s: %v" , depPkg , rev , err )
110
114
}
111
115
112
116
found [dep ] = true
113
- fmt .Printf ("Bumping %s in go.mod to %s\n . " , depPkg , rev )
117
+ fmt .Printf ("Bumping %s in go.mod to %s. \n " , depPkg , rev )
114
118
changed = true
115
119
}
116
120
@@ -146,18 +150,18 @@ type ModuleInfo struct {
146
150
Time string
147
151
}
148
152
149
- func packageDepToGoModCache (depPath , depPkg , commit , pseudoVersion string , commitTime time.Time ) error {
153
+ func packageDepToGoModCache (depPath , depPkg , commit , pseudoVersionOrTag string , commitTime time.Time ) error {
150
154
cacheDir := fmt .Sprintf ("%s/pkg/mod/cache/download/%s/@v" , os .Getenv ("GOPATH" ), depPkg )
151
- goModFile := fmt .Sprintf ("%s/%s.mod" , cacheDir , pseudoVersion )
155
+ goModFile := fmt .Sprintf ("%s/%s.mod" , cacheDir , pseudoVersionOrTag )
152
156
153
157
if _ , err := os .Stat (goModFile ); err == nil {
154
- fmt .Printf ("Pseudo version %s for %s is already packaged up.\n " , pseudoVersion , depPkg )
158
+ fmt .Printf ("%s for %s is already packaged up.\n " , pseudoVersionOrTag , depPkg )
155
159
return nil
156
160
} else if err != nil && ! os .IsNotExist (err ) {
157
161
return fmt .Errorf ("Could not check if %s exists: %v" , goModFile , err )
158
162
}
159
163
160
- fmt .Printf ("Packaging up pseudo version %s for %s into go mod cache.\n " , pseudoVersion , depPkg )
164
+ fmt .Printf ("Packaging up %s for %s into go mod cache.\n " , pseudoVersionOrTag , depPkg )
161
165
162
166
// create the cache if it doesn't exist
163
167
if err := os .MkdirAll (filepath .Dir (goModFile ), os .FileMode (755 )); err != nil {
@@ -173,14 +177,14 @@ func packageDepToGoModCache(depPath, depPkg, commit, pseudoVersion string, commi
173
177
return fmt .Errorf ("failed to checkout %s at %s: %v" , depPkg , commit , err )
174
178
}
175
179
176
- // copy go.mod to pseudoVersion.mod in the cache dir
180
+ // copy go.mod to the cache dir
177
181
if err := copyFile (fmt .Sprintf ("%s/go.mod" , depPath ), goModFile ); err != nil {
178
182
return fmt .Errorf ("unable to copy %s file to %s to gomod cache for %s: %v" , fmt .Sprintf ("%s/go.mod" , depPath ), goModFile , depPkg , err )
179
183
}
180
184
181
- // create pseudoVersion. info file in the cache dir
185
+ // create info file in the cache dir
182
186
moduleInfo := ModuleInfo {
183
- Version : pseudoVersion ,
187
+ Version : pseudoVersionOrTag ,
184
188
Name : commit ,
185
189
Short : commit [:12 ],
186
190
Time : commitTime .UTC ().Format ("2006-01-02T15:04:05Z" ),
@@ -190,17 +194,17 @@ func packageDepToGoModCache(depPath, depPkg, commit, pseudoVersion string, commi
190
194
if err != nil {
191
195
return fmt .Errorf ("error marshaling .info file for %s: %v" , depPkg , err )
192
196
}
193
- if err := ioutil .WriteFile (fmt .Sprintf ("%s/%s.info" , cacheDir , pseudoVersion ), moduleFile , 0644 ); err != nil {
194
- return fmt .Errorf ("failed to write %s file for %s: %v" , fmt .Sprintf ("%s/%s.info" , cacheDir , pseudoVersion ), depPkg , err )
197
+ if err := ioutil .WriteFile (fmt .Sprintf ("%s/%s.info" , cacheDir , pseudoVersionOrTag ), moduleFile , 0644 ); err != nil {
198
+ return fmt .Errorf ("failed to write %s file for %s: %v" , fmt .Sprintf ("%s/%s.info" , cacheDir , pseudoVersionOrTag ), depPkg , err )
195
199
}
196
200
197
- // create the pseudoVersion. zip file in the cache dir. This zip file has the same hash
201
+ // create the zip file in the cache dir. This zip file has the same hash
198
202
// as of the zip file that would have been created by go mod download.
199
- zipCommand := exec .Command ("/gomod-zip" , "--package-name" , depPkg , "--pseudo-version" , pseudoVersion )
203
+ zipCommand := exec .Command ("/gomod-zip" , "--package-name" , depPkg , "--pseudo-version" , pseudoVersionOrTag )
200
204
zipCommand .Stdout = os .Stdout
201
205
zipCommand .Stderr = os .Stderr
202
206
if err := zipCommand .Run (); err != nil {
203
- return fmt .Errorf ("failed to run gomod-zip for %s at %s: %v" , depPkg , pseudoVersion , err )
207
+ return fmt .Errorf ("failed to run gomod-zip for %s at %s: %v" , depPkg , pseudoVersionOrTag , err )
204
208
}
205
209
206
210
// append the pseudoVersion to the list file in the cache dir
@@ -210,7 +214,7 @@ func packageDepToGoModCache(depPath, depPkg, commit, pseudoVersion string, commi
210
214
}
211
215
defer listFile .Close ()
212
216
213
- if _ , err := listFile .WriteString (fmt .Sprintf ("%s\n " , pseudoVersion )); err != nil {
217
+ if _ , err := listFile .WriteString (fmt .Sprintf ("%s\n " , pseudoVersionOrTag )); err != nil {
214
218
return fmt .Errorf ("unable to write to list file in %s: %v" , cacheDir , err )
215
219
}
216
220
0 commit comments