Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lesiw committed Sep 3, 2023
0 parents commit cfbb0e7
Show file tree
Hide file tree
Showing 6 changed files with 394 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
linters:
enable:
- errname
- gocheckcompilerdirectives
- gocyclo
- lll
- makezero
- godot

linters-settings:
gocyclo:
min-complexity: 15
lll:
tab-width: 8
line-length: 99
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2023 Christopher Lesiw

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# bump

`bump` bumps versions.

It accepts a version from standard input and prints the bumped version to
standard output.

`bump` is:

* A small, unix-like utility, designed for use in build scripts.
* Agnostic to the number of segments.
* Agnostic to version prefixes, like "v".

## Usage

```text
Usage of bump:
-s string
index of segment to bump
```

## Examples

```sh
# default
echo "1.0.0" | bump # => 1.0.1
echo "v1.2" | bump # => v1.3
echo "version 42" | bump # => version 43

# by index
echo "1.2.3" | bump -s 0 # => 2.0.0
echo "1.2.3" | bump -s 1 # => 1.3.0
echo "1.2.3" | bump -s 2 # => 1.2.4, same as default

# semver aliases
echo "1.2.3" | bump -s major # => 2.0.0
echo "1.2.3" | bump -s minor # => 1.3.0
echo "1.2.3" | bump -s patch # => 1.2.4, same as default
```

## Cookbook

### Bump git tag

```sh
git tag "$(git describe --abbrev=0 --tags | bump)"
git push origin --tags
```
144 changes: 144 additions & 0 deletions bump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package main

import (
"bufio"
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
"unicode"
)

func main() {
os.Exit(run())
}

func run() int {
segstr := flag.String("s", "", "index of segment to bump")
flag.Parse()

seg, err := parseSegment(segstr)
if err != nil {
fmt.Fprintf(os.Stderr, "parse error: %s\n", err)
}

input, err := readInput(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading stdin: %s\n", err)
}

output, err := bumpVersion(input, seg)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
return 1
}

fmt.Println(output)

return 0
}

func parseSegment(s *string) (int, error) {
if s == nil || *s == "" {
return -1, nil
} else {
var err error
ret, err := strconv.Atoi(*s)
if err != nil {
switch *s {
case "major":
return 0, nil
case "minor":
return 1, nil
case "patch":
return 2, nil
default:
return 0, fmt.Errorf("unrecognized segment: '%s'\n", *s)
}
}
return ret, nil
}
}

func readInput(reader io.Reader) (string, error) {
r := bufio.NewReader(reader)
input, err := r.ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(input), nil
}

func bumpVersion(old string, index int) (string, error) {
prefix, segments, err := parseVersion(old)
if err != nil {
return "", err
}

if len(segments) == 0 {
return "", fmt.Errorf("no version segments found in '%s'", old)
} else if index >= len(segments) {
return "", fmt.Errorf("segment index out of range: %d", index)
} else if index < 0 {
index = len(segments) - 1
}

segments[index]++
for {
index++
if index >= len(segments) {
break
}
segments[index] = 0
}

ret := strings.Builder{}
ret.WriteString(prefix)
for i, seg := range segments {
ret.WriteString(strconv.Itoa(seg))
if i < len(segments)-1 {
ret.WriteRune('.')
}
}

return ret.String(), nil
}

func parseVersion(s string) (string, []int, error) {
var prefixDone bool
var prefix []rune
var segment []rune
var segments []int

for i, r := range s {
if !prefixDone && unicode.IsNumber(r) {
prefixDone = true
} else if !prefixDone {
prefix = append(prefix, r)
continue
}

if unicode.IsNumber(r) {
segment = append(segment, r)
} else if r != '.' {
return "", segments, fmt.Errorf("parse failed: unexpected character: %s",
strconv.QuoteRune(r))
}

if r == '.' || i == len(s)-1 {
if len(segment) == 0 {
return "", segments, fmt.Errorf("parse failed: unexpected '.'")
}
int, err := strconv.Atoi(string(segment))
if err != nil {
return "", segments, fmt.Errorf("parse failed: %w", err)
}
segments = append(segments, int)
segment = []rune{}
}
}

return string(prefix), segments, nil
}
162 changes: 162 additions & 0 deletions bump_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package main

import (
"fmt"
"testing"
)

func TestParseSegment(t *testing.T) {
type testCase struct {
isNil bool
in string
seg int
err bool
}
testCases := []testCase{{
isNil: false,
in: "1",
seg: 1,
err: false,
}, {
isNil: true,
in: "",
seg: -1,
err: false,
}, {
isNil: false,
in: "",
seg: -1,
err: false,
}, {
isNil: false,
in: "x",
seg: 0,
err: true,
}, {
isNil: false,
in: "major",
seg: 0,
err: false,
}, {
isNil: false,
in: "minor",
seg: 1,
err: false,
}, {
isNil: false,
in: "patch",
seg: 2,
err: false,
}}

for _, tc := range testCases {
var s *string
if !tc.isNil {
s = &tc.in
}
test := func(t *testing.T) {
res, err := parseSegment(s)
if !checkErr(err, tc.err) {
t.Errorf("wanted error: %t, but got %t", tc.err, !tc.err)
}
if tc.seg != res {
t.Errorf("want %d, got %d", tc.seg, res)
}
}
var inStr string
if s == nil {
inStr = "nil"
} else if *s == "" {
inStr = "EMPTY"
} else {
inStr = fmt.Sprintf("'%s'", *s)
}
t.Run(fmt.Sprintf("parseseg_%s_%d", inStr, tc.seg), test)
}
}

func TestBumpVersion(t *testing.T) {
type testCase struct {
in string
seg int
out string
}
testCases := []testCase{{
in: "1.0.0",
seg: 1,
out: "1.1.0",
}, {
in: "1.0.0",
seg: 0,
out: "2.0.0",
}, {
in: "1.0.0",
seg: 2,
out: "1.0.1",
}, {
in: "1.2.3",
seg: 1,
out: "1.3.0",
}, {
in: "1.2.3",
seg: 0,
out: "2.0.0",
}, {
in: "100.18.42",
seg: 1,
out: "100.19.0",
}, {
in: "v1.2.3",
seg: 2,
out: "v1.2.4",
}, {
in: "bigprefix13.17.19",
seg: 0,
out: "bigprefix14.0.0",
}, {
in: "v1",
seg: 0,
out: "v2",
}, {
in: "1.2.3.4",
seg: 1,
out: "1.3.0.0",
}, {
in: "1.2.3",
seg: -1,
out: "1.2.4",
}}

for _, tc := range testCases {
test := func(t *testing.T) {
res, err := bumpVersion(tc.in, tc.seg)
if err != nil {
t.Errorf("err: %s", err)
}
if tc.out != res {
t.Errorf("want %s, got %s", tc.out, res)
}
}
t.Run(fmt.Sprintf("%s_bump_%d", tc.in, tc.seg), test)
}
}

func TestBumpVersionOutOfRange(t *testing.T) {
_, err := bumpVersion("1.2.3", 3)
if err == nil {
t.Fatalf("expected error")
}
want := "segment index out of range: 3"
if err.Error() != want {
t.Errorf("want '%s', got '%s'", want, err.Error())
}
}

func checkErr(err error, present bool) bool {
if err == nil && !present {
return true
} else if err != nil && present {
return true
}
return false
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/lesiw/bump

go 1.21.0

0 comments on commit cfbb0e7

Please sign in to comment.