From d2d204a3e0d95a3458ecb3a76ea17a1d019b9725 Mon Sep 17 00:00:00 2001 From: novikov Date: Sun, 8 Sep 2024 23:02:58 +0700 Subject: [PATCH] chore(Makefile): add push_patch and push_minor commands --- Makefile | 6 ++++ tools/go.mod | 5 ++++ tools/go.sum | 2 ++ tools/next_tag/main.go | 64 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 tools/go.mod create mode 100644 tools/go.sum create mode 100644 tools/next_tag/main.go diff --git a/Makefile b/Makefile index b82a19e..a51e3b4 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/tools/go.mod b/tools/go.mod new file mode 100644 index 0000000..d8863f3 --- /dev/null +++ b/tools/go.mod @@ -0,0 +1,5 @@ +module github.com/paragor/simple_cdn/tools + +go 1.23.0 + +require github.com/Masterminds/semver/v3 v3.3.0 diff --git a/tools/go.sum b/tools/go.sum new file mode 100644 index 0000000..2d4775f --- /dev/null +++ b/tools/go.sum @@ -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= diff --git a/tools/next_tag/main.go b/tools/next_tag/main.go new file mode 100644 index 0000000..db1f963 --- /dev/null +++ b/tools/next_tag/main.go @@ -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()) +}