-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
129 lines (108 loc) · 3.54 KB
/
main.go
File metadata and controls
129 lines (108 loc) · 3.54 KB
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
// Package main implements a CLI tool to bump the version in a Go source file,
// stage changes, commit, and tag using git.
package main
import (
"flag"
"fmt"
"os"
"slices"
"strings"
goversion "github.com/bcomnes/goversion/v2/pkg"
)
type arrayFlags []string
func (a *arrayFlags) String() string {
return fmt.Sprint(*a)
}
func (a *arrayFlags) Set(value string) error {
*a = append(*a, value)
return nil
}
func usage() {
msg := `Usage:
goversion [options] <version-bump>
Bumps the version in a Go source file (default: ./version.go), commits the change with the version string (no "v" prefix),
and tags the commit with the version prefixed with "v". For major version bumps >= v2, go.mod and all self references are also updated.
Examples:
goversion minor
goversion 1.2.3
goversion -bump-file package.json -bump-file Cargo.toml patch
goversion -post-bump ./scripts/update-docs.sh -file docs/version.md patch
Positional arguments:
<version-bump> One of: major, minor, patch, premajor, preminor, prepatch, prerelease, from-git, or an explicit version like 1.2.3
Options:
`
fmt.Fprint(os.Stderr, msg)
flag.PrintDefaults()
}
func main() {
// Define flags.
versionFile := flag.String("version-file", "./version.go", "Path to the Go file containing the version declaration")
var extraFiles arrayFlags
flag.Var(&extraFiles, "file", "Additional file to stage and commit. May be repeated.")
var bumpFiles arrayFlags
flag.Var(&bumpFiles, "bump-file", "Additional file to scan for first semver and bump it. May be repeated.")
postBump := flag.String("post-bump", "", "Script to execute after version bump but before git commit. Receives GOVERSION_OLD_VERSION and GOVERSION_NEW_VERSION env vars.")
dryRun := flag.Bool("dry", false, "Perform a dry run without modifying any files or git repository")
showVersion := flag.Bool("version", false, "Show CLI version and exit")
help := flag.Bool("help", false, "Show help message and exit")
flag.Usage = usage
flag.Parse()
if *help {
usage()
os.Exit(0)
}
if *showVersion {
fmt.Println("goversion CLI version", Version)
os.Exit(0)
}
// Guard against misplaced flags after positional args.
for _, arg := range flag.Args() {
if strings.HasPrefix(arg, "-") {
fmt.Fprintln(os.Stderr, "Error: Flags must be specified before the command. Please reorder your arguments.")
usage()
os.Exit(1)
}
}
args := flag.Args()
if len(args) != 1 {
fmt.Fprintln(os.Stderr, "Error: <version-bump> positional argument is required")
usage()
os.Exit(1)
}
versionArg := args[0]
// Make sure versionFile is in extraFiles so it's always staged.
if !slices.Contains(extraFiles, *versionFile) {
extraFiles = append(extraFiles, *versionFile)
}
var meta goversion.VersionMeta
var err error
if *dryRun {
meta, err = goversion.DryRun(*versionFile, versionArg, bumpFiles)
} else {
meta, err = goversion.Run(*versionFile, versionArg, extraFiles, bumpFiles, *postBump)
}
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
// Summary
if *dryRun {
fmt.Println("Dry run complete — no files were modified.")
} else {
fmt.Println("Version bump successful!")
}
fmt.Printf("Old Version: %s\n", meta.OldVersion)
fmt.Printf("New Version: %s\n", meta.NewVersion)
fmt.Printf("Bump Type: %s\n", meta.BumpType)
// Print out exactly which files were (or would be) touched.
if len(meta.UpdatedFiles) > 0 {
if *dryRun {
fmt.Println("Files that would be updated:")
} else {
fmt.Println("Files updated:")
}
for _, f := range meta.UpdatedFiles {
fmt.Printf(" %s\n", f)
}
}
}