77 "strings"
88
99 "github.com/jmelahman/tag/semver"
10- "github.com/sirupsen/logrus"
10+ log "github.com/sirupsen/logrus"
1111)
1212
1313func genTagPattern (prefix , suffix string ) string {
@@ -21,17 +21,17 @@ func genTagPattern(prefix, suffix string) string {
2121 return tagPattern
2222}
2323
24- func GetLatestSemverTag (prefix , suffix string , logger * logrus. Logger ) (string , error ) {
24+ func GetLatestSemverTag (prefix , suffix string ) (string , error ) {
2525 tagPattern := genTagPattern (prefix , suffix )
26- logger .WithFields (logrus .Fields {
26+ log .WithFields (log .Fields {
2727 "pattern" : tagPattern ,
2828 "prefix" : prefix ,
2929 "suffix" : suffix ,
3030 }).Debug ("GetLatestSemverTag" )
3131 cmd := exec .Command ("git" , "describe" , "--tags" , "--abbrev=0" , "--match" , tagPattern )
3232 output , err := cmd .Output ()
3333 if err != nil {
34- logger .Debug ("GetLatestSemverTag: no tags found matching pattern, returning v0.0.0" )
34+ log .Debug ("GetLatestSemverTag: no tags found matching pattern, returning v0.0.0" )
3535 if prefix == "" {
3636 return "v0.0.0" , nil
3737 } else {
@@ -40,19 +40,19 @@ func GetLatestSemverTag(prefix, suffix string, logger *logrus.Logger) (string, e
4040 }
4141
4242 matchedTag := strings .TrimSpace (string (output ))
43- logger .WithField ("matchedTag" , matchedTag ).Debug ("GetLatestSemverTag: git describe matched tag" )
43+ log .WithField ("matchedTag" , matchedTag ).Debug ("GetLatestSemverTag: git describe matched tag" )
4444
4545 tagsAt , err := ListTagsAt (matchedTag )
4646 if err != nil {
47- logger .WithError (err ).WithField ("matchedTag" , matchedTag ).Debug ("GetLatestSemverTag: error listing tags at" )
47+ log .WithError (err ).WithField ("matchedTag" , matchedTag ).Debug ("GetLatestSemverTag: error listing tags at" )
4848 if prefix == "" {
4949 return "v0.0.0" , nil
5050 } else {
5151 return fmt .Sprintf ("%s/v0.0.0" , prefix ), nil
5252 }
5353 }
5454
55- logger .WithField ("count" , len (tagsAt )).WithField ("matchedTag" , matchedTag ).Debug ("GetLatestSemverTag: found tags at" )
55+ log .WithField ("count" , len (tagsAt )).WithField ("matchedTag" , matchedTag ).Debug ("GetLatestSemverTag: found tags at" )
5656
5757 var largestTag string
5858 var largestVersion * semver.Version
@@ -64,7 +64,7 @@ func GetLatestSemverTag(prefix, suffix string, logger *logrus.Logger) (string, e
6464
6565 version , err := semver .ParseSemver (tag )
6666 if err != nil {
67- logger .WithError (err ).WithField ("tag" , tag ).Debug ("GetLatestSemverTag: failed to parse tag" )
67+ log .WithError (err ).WithField ("tag" , tag ).Debug ("GetLatestSemverTag: failed to parse tag" )
6868 continue
6969 }
7070
@@ -74,7 +74,7 @@ func GetLatestSemverTag(prefix, suffix string, logger *logrus.Logger) (string, e
7474 expectedPrefix = prefix + "/"
7575 }
7676 if version .Prefix != expectedPrefix {
77- logger .WithFields (logrus .Fields {
77+ log .WithFields (log .Fields {
7878 "tag" : tag ,
7979 "expectedPrefix" : expectedPrefix ,
8080 "actualPrefix" : version .Prefix ,
@@ -83,23 +83,23 @@ func GetLatestSemverTag(prefix, suffix string, logger *logrus.Logger) (string, e
8383 }
8484
8585 if suffix != "" && version .PreRelease != suffix {
86- logger .WithFields (logrus .Fields {
87- "tag" : tag ,
86+ log .WithFields (log .Fields {
87+ "tag" : tag ,
8888 "expectedSuffix" : suffix ,
89- "actualSuffix" : version .PreRelease ,
89+ "actualSuffix" : version .PreRelease ,
9090 }).Debug ("GetLatestSemverTag: tag suffix mismatch" )
9191 continue
9292 }
9393
9494 if largestVersion == nil {
9595 largestTag = tag
9696 largestVersion = version
97- logger .WithField ("tag" , tag ).Debug ("GetLatestSemverTag: initializing largest tag" )
97+ log .WithField ("tag" , tag ).Debug ("GetLatestSemverTag: initializing largest tag" )
9898 continue
9999 }
100100
101101 if semver .CompareSemver (version , largestVersion ) {
102- logger .WithFields (logrus .Fields {
102+ log .WithFields (log .Fields {
103103 "newerTag" : tag ,
104104 "olderTag" : largestTag ,
105105 }).Debug ("GetLatestSemverTag: found newer tag" )
@@ -108,15 +108,15 @@ func GetLatestSemverTag(prefix, suffix string, logger *logrus.Logger) (string, e
108108 }
109109 }
110110
111- logger .WithField ("latestTag" , largestTag ).Debug ("GetLatestSemverTag: returning latest tag" )
111+ log .WithField ("latestTag" , largestTag ).Debug ("GetLatestSemverTag: returning latest tag" )
112112
113113 return largestTag , nil
114114}
115115
116116// List all git tags
117- func ListTags (prefix , suffix string , logger * logrus. Logger ) ([]string , error ) {
117+ func ListTags (prefix , suffix string ) ([]string , error ) {
118118 tagPattern := genTagPattern (prefix , suffix )
119- logger .WithFields (logrus .Fields {
119+ log .WithFields (log .Fields {
120120 "pattern" : tagPattern ,
121121 "prefix" : prefix ,
122122 "suffix" : suffix ,
@@ -125,12 +125,12 @@ func ListTags(prefix, suffix string, logger *logrus.Logger) ([]string, error) {
125125 cmd .Stderr = os .Stderr
126126 tagsOutput , err := cmd .Output ()
127127 if err != nil {
128- logger .WithError (err ).Debug ("ListTags: error running git tag -l" )
128+ log .WithError (err ).Debug ("ListTags: error running git tag -l" )
129129 return nil , err
130130 }
131131
132132 tagList := strings .Split (strings .TrimSpace (string (tagsOutput )), "\n " )
133- logger .WithField ("count" , len (tagList )).Debug ("ListTags: git returned tags (before filtering)" )
133+ log .WithField ("count" , len (tagList )).Debug ("ListTags: git returned tags (before filtering)" )
134134
135135 // Filter by suffix if specified, since git pattern matching might not be precise enough
136136 if suffix != "" {
@@ -141,28 +141,28 @@ func ListTags(prefix, suffix string, logger *logrus.Logger) ([]string, error) {
141141 }
142142 version , err := semver .ParseSemver (tag )
143143 if err != nil {
144- logger .WithError (err ).WithField ("tag" , tag ).Debug ("ListTags: failed to parse tag" )
144+ log .WithError (err ).WithField ("tag" , tag ).Debug ("ListTags: failed to parse tag" )
145145 continue
146146 }
147147 if version .PreRelease == suffix {
148148 filteredList = append (filteredList , tag )
149- logger .WithField ("tag" , tag ).WithField ("suffix" , suffix ).Debug ("ListTags: tag matches suffix" )
149+ log .WithField ("tag" , tag ).WithField ("suffix" , suffix ).Debug ("ListTags: tag matches suffix" )
150150 } else {
151- logger .WithFields (logrus .Fields {
152- "tag" : tag ,
151+ log .WithFields (log .Fields {
152+ "tag" : tag ,
153153 "expectedSuffix" : suffix ,
154- "actualSuffix" : version .PreRelease ,
154+ "actualSuffix" : version .PreRelease ,
155155 }).Debug ("ListTags: tag suffix mismatch" )
156156 }
157157 }
158- logger .WithFields (logrus .Fields {
159- "count" : len (filteredList ),
158+ log .WithFields (log .Fields {
159+ "count" : len (filteredList ),
160160 "suffix" : suffix ,
161161 }).Debug ("ListTags: after filtering, found tags matching suffix" )
162162 return filteredList , nil
163163 }
164164
165- logger .WithField ("count" , len (tagList )).Debug ("ListTags: returning tags" )
165+ log .WithField ("count" , len (tagList )).Debug ("ListTags: returning tags" )
166166
167167 return tagList , nil
168168}
@@ -180,50 +180,50 @@ func ListTagsAt(ref string) ([]string, error) {
180180 return tagList , nil
181181}
182182
183- func TagExists (tag string , logger * logrus. Logger ) (bool , error ) {
183+ func TagExists (tag string ) (bool , error ) {
184184 tagRef := fmt .Sprintf ("refs/tags/%s" , tag )
185- logger .WithField ("tag" , tag ).Debug ("TagExists: checking if tag exists" )
185+ log .WithField ("tag" , tag ).Debug ("TagExists: checking if tag exists" )
186186 cmd := exec .Command ("git" , "show-ref" , "--tags" , "--quiet" , tagRef )
187187 if err := cmd .Run (); err != nil {
188188 if exitError , ok := err .(* exec.ExitError ); ok && exitError .ExitCode () == 1 {
189- logger .WithField ("tag" , tag ).Debug ("TagExists: tag does not exist" )
189+ log .WithField ("tag" , tag ).Debug ("TagExists: tag does not exist" )
190190 return false , nil
191191 }
192- logger .WithError (err ).WithField ("tag" , tag ).Debug ("TagExists: error checking tag" )
192+ log .WithError (err ).WithField ("tag" , tag ).Debug ("TagExists: error checking tag" )
193193 return false , err
194194 }
195- logger .WithField ("tag" , tag ).Debug ("TagExists: tag exists" )
195+ log .WithField ("tag" , tag ).Debug ("TagExists: tag exists" )
196196 return true , nil
197197}
198198
199- func CreateAndPushTag (tag string , remote string , logger * logrus. Logger ) error {
200- logger .WithField ("tag" , tag ).Debug ("CreateAndPushTag: creating tag" )
199+ func CreateAndPushTag (tag string , remote string ) error {
200+ log .WithField ("tag" , tag ).Debug ("CreateAndPushTag: creating tag" )
201201 cmd := exec .Command ("git" , "tag" , tag )
202202 cmd .Stderr = os .Stderr
203203 if err := cmd .Run (); err != nil {
204- logger .WithError (err ).WithField ("tag" , tag ).Debug ("CreateAndPushTag: error creating tag" )
204+ log .WithError (err ).WithField ("tag" , tag ).Debug ("CreateAndPushTag: error creating tag" )
205205 return fmt .Errorf ("failed to create tag: %w" , err )
206206 }
207207
208- logger .WithFields (logrus .Fields {
208+ log .WithFields (log .Fields {
209209 "tag" : tag ,
210210 "remote" : remote ,
211211 }).Debug ("CreateAndPushTag: pushing tag to remote" )
212212 cmd = exec .Command ("git" , "push" , "--quiet" , remote , tag )
213213 cmd .Stderr = os .Stderr
214214 if err := cmd .Run (); err != nil {
215- logger .WithError (err ).WithFields (logrus .Fields {
215+ log .WithError (err ).WithFields (log .Fields {
216216 "tag" : tag ,
217217 "remote" : remote ,
218218 }).Debug ("CreateAndPushTag: error pushing tag" )
219219 return fmt .Errorf ("failed to push tag to %s: %w" , remote , err )
220220 }
221221
222- logger .WithField ("tag" , tag ).Debug ("CreateAndPushTag: successfully created and pushed tag" )
222+ log .WithField ("tag" , tag ).Debug ("CreateAndPushTag: successfully created and pushed tag" )
223223 return nil
224224}
225225
226- func FetchSemverTags (remote string , prefix , suffix string , logger * logrus. Logger ) error {
226+ func FetchSemverTags (remote string , prefix , suffix string ) error {
227227 // When suffix is specified, greedily fetch all matching tags (git refspecs don't support
228228 // wildcards in the middle like v*-suffix*). We'll filter by suffix in the code.
229229 var tagPattern string
@@ -232,24 +232,24 @@ func FetchSemverTags(remote string, prefix, suffix string, logger *logrus.Logger
232232 } else {
233233 tagPattern = "refs/tags/v*:refs/tags/v*"
234234 }
235- logger .WithFields (logrus .Fields {
236- "remote" : remote ,
237- "refspec" : tagPattern ,
238- "suffix" : suffix ,
235+ log .WithFields (log .Fields {
236+ "remote" : remote ,
237+ "refspec" : tagPattern ,
238+ "suffix" : suffix ,
239239 }).Debug ("FetchSemverTags: fetching from remote (suffix will be filtered later)" )
240240 cmd := exec .Command ("git" , "fetch" , "--quiet" , "--prune" , remote , tagPattern )
241241 cmd .Stderr = os .Stderr
242242 if err := cmd .Run (); err != nil {
243- logger .WithError (err ).Debug ("FetchSemverTags: error fetching tags" )
243+ log .WithError (err ).Debug ("FetchSemverTags: error fetching tags" )
244244 return fmt .Errorf ("failed to fetch tags from %s: %w" , remote , err )
245245 }
246- logger .Debug ("FetchSemverTags: successfully fetched tags" )
246+ log .Debug ("FetchSemverTags: successfully fetched tags" )
247247 return nil
248248}
249249
250- func IsHEADAlreadyTagged (prefix , suffix string , logger * logrus. Logger ) (bool , error ) {
250+ func IsHEADAlreadyTagged (prefix , suffix string ) (bool , error ) {
251251 tagPattern := genTagPattern (prefix , suffix )
252- logger .WithFields (logrus .Fields {
252+ log .WithFields (log .Fields {
253253 "pattern" : tagPattern ,
254254 "prefix" : prefix ,
255255 "suffix" : suffix ,
@@ -258,15 +258,15 @@ func IsHEADAlreadyTagged(prefix, suffix string, logger *logrus.Logger) (bool, er
258258 cmd .Stderr = os .Stderr
259259 output , err := cmd .Output ()
260260 if err != nil {
261- logger .WithError (err ).Debug ("IsHEADAlreadyTagged: error checking tags" )
261+ log .WithError (err ).Debug ("IsHEADAlreadyTagged: error checking tags" )
262262 return false , fmt .Errorf ("failed to check tags for HEAD: %w" , err )
263263 }
264264 tags := strings .TrimSpace (string (output ))
265265 if tags == "" {
266- logger .Debug ("IsHEADAlreadyTagged: no tags found at HEAD" )
266+ log .Debug ("IsHEADAlreadyTagged: no tags found at HEAD" )
267267 return false , nil
268268 }
269- logger .WithField ("tags" , tags ).Debug ("IsHEADAlreadyTagged: found tags at HEAD" )
269+ log .WithField ("tags" , tags ).Debug ("IsHEADAlreadyTagged: found tags at HEAD" )
270270 // If suffix is specified, verify that at least one tag matches the suffix
271271 if suffix != "" {
272272 tagList := strings .Split (tags , "\n " )
@@ -276,25 +276,25 @@ func IsHEADAlreadyTagged(prefix, suffix string, logger *logrus.Logger) (bool, er
276276 }
277277 version , err := semver .ParseSemver (tag )
278278 if err != nil {
279- logger .WithError (err ).WithField ("tag" , tag ).Debug ("IsHEADAlreadyTagged: failed to parse tag" )
279+ log .WithError (err ).WithField ("tag" , tag ).Debug ("IsHEADAlreadyTagged: failed to parse tag" )
280280 continue
281281 }
282282 if version .PreRelease == suffix {
283- logger .WithFields (logrus .Fields {
283+ log .WithFields (log .Fields {
284284 "tag" : tag ,
285285 "suffix" : suffix ,
286286 }).Debug ("IsHEADAlreadyTagged: tag matches suffix" )
287287 return true , nil
288288 }
289- logger .WithFields (logrus .Fields {
290- "tag" : tag ,
289+ log .WithFields (log .Fields {
290+ "tag" : tag ,
291291 "expectedSuffix" : suffix ,
292- "actualSuffix" : version .PreRelease ,
292+ "actualSuffix" : version .PreRelease ,
293293 }).Debug ("IsHEADAlreadyTagged: tag suffix mismatch" )
294294 }
295- logger .WithField ("suffix" , suffix ).Debug ("IsHEADAlreadyTagged: no tags at HEAD match suffix" )
295+ log .WithField ("suffix" , suffix ).Debug ("IsHEADAlreadyTagged: no tags at HEAD match suffix" )
296296 return false , nil
297297 }
298- logger .Debug ("IsHEADAlreadyTagged: HEAD is tagged (suffix not specified)" )
298+ log .Debug ("IsHEADAlreadyTagged: HEAD is tagged (suffix not specified)" )
299299 return true , nil
300300}
0 commit comments