Skip to content

Commit

Permalink
add prerelease and metadata flags (#7)
Browse files Browse the repository at this point in the history
* add prerelease and metadata flags

* Update README

* Update dockerfile and actions [bump minor]
  • Loading branch information
treeder authored Feb 20, 2024
1 parent c011d24 commit be2e3f6
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 29 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:
steps:

- name: Check out code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v1
uses: actions/setup-go@v4
with:
go-version: 1.14
go-version: 1
id: go

- name: Build
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# build stage
FROM golang:1.14-alpine AS build-env
FROM golang:1-alpine AS build-env
RUN apk --no-cache add build-base git mercurial gcc
ENV D=/myapp
WORKDIR $D
Expand All @@ -12,7 +12,7 @@ ADD . $D
RUN cd $D && go build -o bump ./cmd && cp bump /tmp/

# final stage
FROM alpine:3.11
FROM alpine
RUN apk add --no-cache ca-certificates curl
WORKDIR /app
COPY --from=build-env /tmp/bump /script/bump
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Bumps version files and other handy version tools.
## Usage

```sh
docker run --rm -it -v $PWD:/app -w /app treeder/bump [--filename FILENAME] [--input STRING] [CMD]
docker run --rm -it -v $PWD:/app -w /app treeder/bump [--filename FILENAME] [--input STRING] [--prerelease alpha1] [--metadata build123] [CMD]
```

You must pass in either `--filename` or `--input`.
Expand All @@ -16,6 +16,10 @@ If using `--input`, it will write the new version to STDOUT so you can pipe that

Use `--index` flag to specify which found versions it should replace. 0 is first one, 2 is second one, -1 is last one, etc.

`--prerelease` will append a `-` + the prerelease value.

`--metadata` will append a `+` + the metadata value.

CMD is optional and can be one of:

* patch - default
Expand Down
56 changes: 49 additions & 7 deletions bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,58 @@ func ReplaceInContent(vbytes []byte, replaceWith string, index int) (old, new st
return replace(vbytes, replaceWith, "", index)
}

func ReplaceInContent2(vbytes []byte, options *Options) (old, new string, loc []int, newcontents []byte, err error) {
return replace2(vbytes, options)
}

func BumpInContent2(vbytes []byte, options *Options) (old, new string, loc []int, newcontents []byte, err error) {
return replace2(vbytes, options)
}

func BumpString(input string, options *Options) (string, error) {
old, new, loc, n2, err := replace2([]byte(input), options)
if err != nil {
return "", err
}
fmt.Println(old, new, loc, n2)
return new, nil
}

type Options struct {
Replace string
Part string
Index int
PreRelease string
Metadata string
}

// if index is set, it will find all matches and choose the one at the given index, -1 means last
func replace(vbytes []byte, replace, part string, index int) (old, new string, loc []int, newcontents []byte, err error) {
options := &Options{
Replace: replace,
Part: part,
Index: index,
}
return replace2(vbytes, options)
}

func replace2(vbytes []byte, options *Options) (old, new string, loc []int, newcontents []byte, err error) {
re := regexp.MustCompile(semverMatcher)
if index == 0 {
if options.Index == 0 {
loc = re.FindIndex(vbytes)
} else {
locs := re.FindAllIndex(vbytes, -1)
if locs == nil {
return "", "", nil, nil, fmt.Errorf("Did not find semantic version")
}
locsLen := len(locs)
if index >= locsLen {
return "", "", nil, nil, fmt.Errorf("semver index to replace out of range. Found %v, want %v", locsLen, index)
if options.Index >= locsLen {
return "", "", nil, nil, fmt.Errorf("semver index to replace out of range. Found %v, want %v", locsLen, options.Index)
}
if index < 0 {
loc = locs[locsLen+index]
if options.Index < 0 {
loc = locs[locsLen+options.Index]
} else {
loc = locs[index]
loc = locs[options.Index]
}
}
// fmt.Println(loc)
Expand All @@ -47,16 +81,24 @@ func replace(vbytes []byte, replace, part string, index int) (old, new string, l
}
vs := string(vbytes[loc[0]:loc[1]])

replace := options.Replace
if replace == "" {
// fmt.Println("bumping", vs, "part", options.Part)
v := semver.New(vs)
switch part {
switch options.Part {
case "major":
v.BumpMajor()
case "minor":
v.BumpMinor()
default:
v.BumpPatch()
}
if options.PreRelease != "" {
v.PreRelease = semver.PreRelease(options.PreRelease)
}
if options.Metadata != "" {
v.Metadata = options.Metadata
}
replace = v.String()
}

Expand Down
55 changes: 50 additions & 5 deletions bump_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,58 @@
package bump

import (
"fmt"
"testing"
)

func TestBumps(t *testing.T) {
// ehhh, TODO, too lazy
// s := "1.2.3"
// c := &cli.Context{}
// c.Set("filename")
// bump()
s := "1.2.3"
s2, err := BumpString(s, &Options{Part: "patch"})
if err != nil {
t.Fatal(err)
}
fmt.Println(s2)
if s2 != "1.2.4" {
t.Fatal("Expected 1.2.4")
}
s = "1.2.3"
s2, err = BumpString(s, &Options{Part: "minor"})
if err != nil {
t.Fatal(err)
}
fmt.Println(s2)
if s2 != "1.3.0" {
t.Fatal("Expected 1.3.0, got", s2)
}

s = "1.2.3-alpha"
s2, err = BumpString(s, &Options{Part: "patch"})
if err != nil {
t.Fatal(err)
}
fmt.Println(s2)
if s2 != "1.2.4" {
t.Fatal("Expected 1.2.4, got", s2)
}

s = "1.2.3-alpha"
s2, err = BumpString(s, &Options{Part: "minor", PreRelease: "alpha"})
if err != nil {
t.Fatal(err)
}
fmt.Println(s2)
if s2 != "1.3.0-alpha" {
t.Fatal("Expected 1.3.0-alpha, got", s2)
}

s = "1.2.3-alpha"
s2, err = BumpString(s, &Options{Part: "minor", PreRelease: "alpha", Metadata: "build123"})
if err != nil {
t.Fatal(err)
}
fmt.Println(s2)
if s2 != "1.3.0-alpha+build123" {
t.Fatal("Expected 1.3.0-alpha+build123, got", s2)
}

}
33 changes: 28 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
Expand All @@ -29,6 +28,10 @@ func main() {
Name: "input",
Usage: "use this if you want to pass in a string to pass, rather than read it from a file. Cannot be used with --filename.",
},
cli.StringFlag{
Name: "part",
Usage: "part to bump, either major, minor or patch. Default is patch.",
},
cli.BoolFlag{
Name: "extract",
Usage: "this will just find the version and return it, does not modify anything. Safe operation.",
Expand All @@ -45,6 +48,14 @@ func main() {
Name: "index",
Usage: "if zero (default), uses first match. If greater than zero, uses nth match. If less than zero, starts at last match and goes backwards, ie: last match is -1.",
},
cli.StringFlag{
Name: "prerelease",
Usage: "adds a prerelease tag to the version per semver spec",
},
cli.StringFlag{
Name: "metadata",
Usage: "adds metadata to the version per semver spec",
},
}
err := app.Run(os.Args)
if err != nil {
Expand Down Expand Up @@ -76,7 +87,7 @@ func bumper(c *cli.Context) error {
if c.IsSet("input") {
vbytes = []byte(c.String("input"))
} else {
vbytes, err = ioutil.ReadFile(filename)
vbytes, err = os.ReadFile(filename)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%v not found. Use either --filename or --input to change where to look for version.", filename)
Expand All @@ -87,14 +98,26 @@ func bumper(c *cli.Context) error {

index := c.Int("index")

options := &bump.Options{Index: index}
options.PreRelease = c.String("prerelease")
options.Metadata = c.String("metadata")
if c.IsSet("part") {
options.Part = c.String("part")
fmt.Println("part is set", options.Part)
} else {
options.Part = arg
}

var old, new string
var newcontent []byte
if c.IsSet("replace") {
// this will just write the passed in version directly
replace := c.String("replace")
old, new, _, newcontent, err = bump.ReplaceInContent(vbytes, replace, index)
options.Replace = replace
options.Part = ""
old, new, _, newcontent, err = bump.ReplaceInContent2(vbytes, options)
} else {
old, new, _, newcontent, err = bump.BumpInContent(vbytes, arg, index)
old, new, _, newcontent, err = bump.BumpInContent2(vbytes, options)
}
if err != nil {
return err
Expand All @@ -107,7 +130,7 @@ func bumper(c *cli.Context) error {
// fmt.Fprintln(os.Stderr, "Old version:", old)
// fmt.Fprintln(os.Stderr, "New version:", new)
if !c.IsSet("input") {
err = ioutil.WriteFile(filename, newcontent, 0644)
err = os.WriteFile(filename, newcontent, 0644)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/treeder/bump

go 1.12
go 1.21

require (
github.com/coreos/go-semver v0.3.0
github.com/coreos/go-semver v0.3.1
github.com/urfave/cli v1.21.0
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
github.com/urfave/cli v1.21.0 h1:wYSSj06510qPIzGSua9ZqsncMmWE3Zr55KBERygyrxE=
github.com/urfave/cli v1.21.0/go.mod h1:lxDj6qX9Q6lWQxIrbrT0nwecwUtRnhVZAJjJZrVUZZQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit be2e3f6

Please sign in to comment.