-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (97 loc) · 2.72 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"errors"
"flag"
repositoryModel "github.com/iakunin/git-version-manager/models/repository"
tagModel "github.com/iakunin/git-version-manager/models/tag"
log "github.com/sirupsen/logrus"
"os"
)
func main() {
workDir, err := os.Getwd()
if err != nil {
log.Panicf("Unable to get currenc workDir: '%s'", err)
}
repoDir := flag.String("repoDir", workDir, "git-repo directory (current workDir by default)")
prefix := flag.String("prefix", "", "tag prefix")
suffix := flag.String("suffix", "", "tag suffix")
isVerbose := flag.Bool("verbose", false, "enable verbose logging")
bumpStrategy := flag.String(
"bumpStrategy",
string(tagModel.Patch),
"available values: `patch`, `minor`, `major`",
)
successfulMessage := flag.String(
"successfulMessage",
"",
"message which will be written to console in the case of successful tagging",
)
flag.Parse()
if *isVerbose {
log.SetLevel(log.TraceLevel)
}
repository, err := repositoryModel.Open(*repoDir)
if err != nil {
log.Panicf("Unable to open the repo: '%s'", err)
}
rawTags, err := repository.GetRawTags()
if err != nil {
log.Panicf("Unable to getRawTags: '%s'", err)
}
for _, t := range rawTags {
log.Debugf("This is a rawTag: '%s'", *t)
}
tags, err := createTags(rawTags, *prefix, *suffix)
if err != nil {
log.Panicf("Unable to createTags: '%s'", err)
}
for _, t := range tags {
log.Debugf("This is a tag (before bump): '%s'\n", t.String())
}
maxTag, _ := findMaxTag(tags)
log.Infof("This is a MAX tag: '%s'", maxTag.String())
maxTag.Bump(tagModel.BumpStrategy(*bumpStrategy))
log.Infof("This is a BUMPED tag: '%s'\n", maxTag.String())
for _, t := range tags {
log.Debugf("This is a tag (after bump): '%s'\n", t.String())
}
err = repository.SetTag(maxTag.String())
if err != nil {
log.Panicf("Unable to set tag: '%s'", err)
}
// @TODO: push tags using `repository.PushTags()`
// @TODO: fetch tags using `repository.FetchTags()`
if *successfulMessage != "" {
log.Infoln(*successfulMessage)
}
}
func createTags(rawTags []*string, prefix string, suffix string) ([]*tagModel.Tag, error) {
var result []*tagModel.Tag
for i := range rawTags {
t, err := tagModel.New(*rawTags[i], prefix, suffix)
if err != nil {
log.Debugf("skipping a tag '%s'", *rawTags[i])
continue
}
result = append(result, t)
}
if len(result) == 0 {
result = append(result, tagModel.Empty(prefix, suffix))
}
return result, nil
}
func findMaxTag(tags []*tagModel.Tag) (*tagModel.Tag, error) {
length := len(tags)
if length == 0 {
return nil, errors.New("got empty `tags` array")
} else if length == 1 {
return tags[0], nil
}
max := tags[0]
for _, t := range tags[1:] {
if max.LessThan(*t) {
max = t
}
}
return max, nil
}