Skip to content

Commit

Permalink
chore(Makefile): add push_patch and push_minor commands
Browse files Browse the repository at this point in the history
  • Loading branch information
paragor committed Sep 8, 2024
1 parent b565efe commit d2d204a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
build_local:
goreleaser release --snapshot --clean

push_patch:
next_tag=$$(cd tools && go run next_tag/main.go patch) && git tag $$next_tag && git push origin tag $$next_tag

push_minor:
next_tag=$$(cd tools && go run next_tag/main.go minor) && git tag $$next_tag && git push origin tag $$next_tag
5 changes: 5 additions & 0 deletions tools/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/paragor/simple_cdn/tools

go 1.23.0

require github.com/Masterminds/semver/v3 v3.3.0
2 changes: 2 additions & 0 deletions tools/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
github.com/Masterminds/semver/v3 v3.3.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
64 changes: 64 additions & 0 deletions tools/next_tag/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"bytes"
"context"
"fmt"
"github.com/Masterminds/semver/v3"
"log"
"os"
"os/exec"
"slices"
"strings"
"time"
)

func main() {
args := os.Args
if len(args) != 2 {
panic("only one arg require")
}
upgradeType := strings.ToLower(args[1])
switch upgradeType {
case "major", "minor", "patch":
default:
log.Fatalln("arg[1] should one of: major, minor, patch")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
gitTag := exec.CommandContext(ctx, "git", "tag")
gitTag.Stdout = stdout
gitTag.Stderr = stderr
if err := gitTag.Run(); err != nil {
log.Fatalf("cant run git tag: %w. stderr: %s", err, stderr.String())
}
rawTags := strings.Split(strings.TrimSpace(stdout.String()), "\n")
tags := []*semver.Version{}
for _, rawTag := range rawTags {
if !strings.HasPrefix(rawTag, "v") {
continue
}
rawTag = strings.TrimPrefix(rawTag, "v")
tag, err := semver.StrictNewVersion(rawTag)
if err != nil {
continue
}
tags = append(tags, tag)
}

slices.SortFunc(tags, func(a, b *semver.Version) int {
return b.Compare(a)
})

newTag := *tags[0]
if strings.ToLower(args[1]) == "major" {
newTag = newTag.IncMajor()
} else if strings.ToLower(args[1]) == "minor" {
newTag = newTag.IncMinor()
} else {
newTag = newTag.IncPatch()
}
fmt.Printf("v%s", newTag.String())
}

0 comments on commit d2d204a

Please sign in to comment.