forked from mpdred/semantic-tagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (144 loc) · 3.64 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"errors"
"fmt"
"github.com/sirupsen/logrus"
"semtag/internal"
"semtag/pkg/changelog"
"semtag/pkg/output"
"semtag/pkg/terminal"
"semtag/pkg/version"
"semtag/pkg/versionControl"
)
var (
ErrNotPushMode = errors.New("push to Git skipped: use the `-push` flag to push changes")
)
var GitRepo versionControl.VersionControl = &versionControl.GitRepository{}
func main() {
args := internal.CliArgs{}
args.ParseFlags()
v := setVersion(args)
if args.Push {
if err := versionControl.TrySetGitCredentialsBasicAuth(); err != nil {
output.Logger().Debug(err)
}
}
if args.ExecuteCommand != "" {
for _, val := range v.AsList() {
_, err := terminal.Shellf(args.ExecuteCommand, val)
if err != nil {
output.Logger().Fatal(err)
}
}
}
if args.ShouldTagGit {
hasRelevantChanges, err := versionControl.HasRelevantChanges(args.RelevantPaths)
if err != nil {
output.Logger().Fatal(err)
}
if hasRelevantChanges {
tag := &versionControl.Tag{
Name: v.String(),
}
if err := TagGit(tag, args.Push); err != nil {
output.Logger().Fatal(err)
}
if !args.Push {
output.Logger().Warn(ErrNotPushMode)
}
}
}
shouldTagInFile := len(args.FileName) > 0 && len(args.FileVersionPattern) > 0
if shouldTagInFile {
if err := TagFile(v, args.FileName, args.FileVersionPattern, args.Push); err != nil {
output.Logger().Fatal(err)
}
if !args.Push {
output.Logger().Warn(ErrNotPushMode)
}
}
if args.Changelog {
chLog, err := changelog.NewLog()
if err != nil {
output.Logger().Fatal(err)
}
chLog.Prefix = args.Prefix
chLog.Suffix = args.Suffix
chLog.Regex = args.ChangelogRegex
if err := chLog.Generate(); err != nil {
output.Logger().Fatal(err)
}
}
// print the version to stdout; execute as the last command so that it can be grepped by simple shell scripts
fmt.Print(v.String())
}
func setVersion(args internal.CliArgs) version.Version {
v := version.Version{
Prefix: args.Prefix,
Suffix: args.Suffix,
}
if args.CustomVersion == "" {
if err := v.SetVersionFromGit(); err != nil {
output.Logger().Fatal(err)
}
} else {
if err := v.UseCustomVersion(args.Prefix, args.CustomVersion, args.Suffix); err != nil {
output.Logger().Fatal(err)
}
}
shouldIncrementVersion := args.VersionScope.String() != ""
if shouldIncrementVersion {
if err := v.SetScope(args.VersionScope.String()); err != nil {
output.Logger().Fatal(err)
}
}
return v
}
// TagGit creates and pushes a git tag
func TagGit(tag *versionControl.Tag, pushChanges bool) error {
if pushChanges {
if err := tag.Create(); err != nil {
return err
}
if err := tag.Push(); err != nil {
return err
}
}
output.Logger().WithFields(logrus.Fields{
"tag": tag.Name,
"tagPushed": pushChanges,
}).Info("the git commit has been tagged")
return nil
}
// TagFile updates a substring in a file based on a pattern
func TagFile(ver version.Version, filePath string, versionPattern string, pushChanges bool) error {
const commitMsgVerBump = "chore(version): "
f := version.File{
Path: filePath,
VersionFormat: versionPattern,
Version: ver.String(),
}
newContents, err := f.ReplaceSubstring()
if err != nil {
return err
}
if err := f.Write(newContents); err != nil {
return err
}
if err := GitRepo.Add(filePath); err != nil {
return err
}
if pushChanges {
if err := GitRepo.Commit(commitMsgVerBump + ver.String()); err != nil {
return err
}
if err := GitRepo.Push(""); err != nil {
return err
}
}
output.Logger().WithFields(logrus.Fields{
"file": f.Path,
"fileChangesPushed": pushChanges,
}).Info("the file has been updated")
return nil
}