Compare commits
10 commits
6cc637519b
...
66695a0474
Author | SHA1 | Date | |
---|---|---|---|
66695a0474 | |||
d12dfef527 | |||
a376658386 | |||
98a35831f0 | |||
2bc4bf8f80 | |||
6948f596fe | |||
88ad9ec55c | |||
920675be1d | |||
|
5d60e0ca4d | ||
|
1cd8607d34 |
4 changed files with 110 additions and 22 deletions
|
@ -1,2 +1,2 @@
|
|||
VERSION_PREFIX=v
|
||||
GIT_SIGN=false
|
||||
GIT_SIGN=true
|
||||
|
|
|
@ -8,7 +8,7 @@ version is implemented using https://github.com/Masterminds/semver to parse and
|
|||
## Installation
|
||||
|
||||
```bash
|
||||
go install github.com/ray1729/git-semver
|
||||
go install github.com/ray1729/git-semver@v0.2.2
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
|
34
main.go
34
main.go
|
@ -132,6 +132,8 @@ func nextVersion(inc string) func(*cli.Context) error {
|
|||
newVer = v.IncMinor()
|
||||
case "major":
|
||||
newVer = v.IncMajor()
|
||||
default:
|
||||
newVer = *v
|
||||
}
|
||||
}
|
||||
if c.IsSet("pre-release") {
|
||||
|
@ -233,11 +235,15 @@ func parseConfig(f io.Reader) (config, error) {
|
|||
}
|
||||
k, v, ok := strings.Cut(t, "=")
|
||||
if !ok {
|
||||
return conf, fmt.Errorf("invalid configuration: %s", t)
|
||||
return conf, fmt.Errorf("error parsing %s: invalid syntax", t)
|
||||
}
|
||||
k, v = strings.TrimSpace(k), strings.TrimSpace(v)
|
||||
if len(v) >= 2 && strings.HasPrefix(v, "\"") && strings.HasSuffix(v, "\"") {
|
||||
v = strings.Trim(v, "\"")
|
||||
unquotedV, err := strconv.Unquote(v)
|
||||
if err != nil {
|
||||
return conf, fmt.Errorf("error parsing %s: invalid quoted string", t)
|
||||
}
|
||||
v = unquotedV
|
||||
}
|
||||
switch strings.ToUpper(k) {
|
||||
case "VERSION_PREFIX":
|
||||
|
@ -245,17 +251,14 @@ func parseConfig(f io.Reader) (config, error) {
|
|||
case "GIT_SIGN":
|
||||
b, err := strconv.ParseBool(v)
|
||||
if err != nil {
|
||||
return conf, fmt.Errorf("error parsing GIT_SIGN %q: %v", v, err)
|
||||
return conf, fmt.Errorf("error parsing %s: invalid boolean value", t)
|
||||
}
|
||||
conf.sign = b
|
||||
default:
|
||||
return conf, fmt.Errorf("unrecognized configuration variable: %s", k)
|
||||
return conf, fmt.Errorf("error parsing %s: unrecognized variable", t)
|
||||
}
|
||||
}
|
||||
if err := s.Err(); err != nil {
|
||||
return conf, err
|
||||
}
|
||||
return conf, nil
|
||||
return conf, s.Err()
|
||||
}
|
||||
|
||||
func createTag(tagName string, sign bool) error {
|
||||
|
@ -263,25 +266,18 @@ func createTag(tagName string, sign bool) error {
|
|||
if sign {
|
||||
signFlag = "-s"
|
||||
}
|
||||
cmd := exec.Command("git", "tag", signFlag, "-m", "Version "+tagName, tagName)
|
||||
stderr, err := cmd.StderrPipe()
|
||||
out, err := exec.Command("git", "tag", signFlag, "-m", "Version "+tagName, tagName).CombinedOutput()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := cmd.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
out, _ := io.ReadAll(stderr)
|
||||
fmt.Fprintf(os.Stderr, string(out))
|
||||
if err := cmd.Wait(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, string(out))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getVersion(versionPrefix string) (*semver.Version, error) {
|
||||
out, err := exec.Command("git", "tag").Output()
|
||||
out, err := exec.Command("git", "tag").CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, string(out))
|
||||
return nil, err
|
||||
}
|
||||
var latest *semver.Version
|
||||
|
|
92
main_test.go
Normal file
92
main_test.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseConfig(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
input string
|
||||
expect config
|
||||
expectErr string
|
||||
}{
|
||||
{
|
||||
input: "VERSION_PREFIX=v\n",
|
||||
expect: config{versionPrefix: "v"},
|
||||
},
|
||||
{
|
||||
input: "GIT_SIGN=1\n",
|
||||
expect: config{sign: true},
|
||||
},
|
||||
{
|
||||
input: "VERSION_PREFIX=v\nGIT_SIGN=t\n",
|
||||
expect: config{versionPrefix: "v", sign: true},
|
||||
},
|
||||
{
|
||||
input: "version_prefix=v\ngit_sign=t\n",
|
||||
expect: config{versionPrefix: "v", sign: true},
|
||||
},
|
||||
{
|
||||
input: "# My config\nversion_prefix=v\ngit_sign=t\n",
|
||||
expect: config{versionPrefix: "v", sign: true},
|
||||
},
|
||||
{
|
||||
input: "VERSION_PREFIX=v\nGIT_SIGN=false\n",
|
||||
expect: config{versionPrefix: "v", sign: false},
|
||||
},
|
||||
{
|
||||
input: "VERSION_PREFIX=\"version/\"\nGIT_SIGN=f\n",
|
||||
expect: config{versionPrefix: "version/", sign: false},
|
||||
},
|
||||
{
|
||||
input: "VERSION_PREFIX=\"foo\\\"bar\"\n",
|
||||
expect: config{versionPrefix: "foo\"bar"},
|
||||
},
|
||||
{
|
||||
input: "foo\n",
|
||||
expectErr: "error parsing foo: invalid syntax",
|
||||
},
|
||||
{
|
||||
input: "bad_key=v\n",
|
||||
expectErr: "error parsing bad_key=v: unrecognized variable",
|
||||
},
|
||||
{
|
||||
input: "GIT_SIGN=wibble\n",
|
||||
expectErr: "error parsing GIT_SIGN=wibble: invalid boolean value",
|
||||
},
|
||||
{
|
||||
input: "VERSION_PREFIX=\"foo\\z\"\n",
|
||||
expectErr: "error parsing VERSION_PREFIX=\"foo\\z\": invalid quoted string",
|
||||
},
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||
res, err := parseConfig(strings.NewReader(tc.input))
|
||||
if tc.expectErr != "" {
|
||||
if err == nil {
|
||||
t.Error("expected error not returned")
|
||||
return
|
||||
}
|
||||
if err.Error() != tc.expectErr {
|
||||
t.Errorf("expected error %q, got %q", tc.expectErr, err.Error())
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("got unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
if res.versionPrefix != tc.expect.versionPrefix {
|
||||
t.Errorf("got versionPrefix %s, expected %s", res.versionPrefix, tc.expect.versionPrefix)
|
||||
}
|
||||
if res.sign != tc.expect.sign {
|
||||
t.Errorf("got sign %t, expected %t", res.sign, tc.expect.sign)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue