Skip to content

Commit

Permalink
Add new -i flag that allows to skip non SemVer tags
Browse files Browse the repository at this point in the history
Useful for repositories that just have an old tag that is malformed.

Not sure I like the way the shell docs work now but I have not thought
of a decent way to document flags and args at the same time using just
standard lib ways. The usage prints okay, but the `--help` is still
broken only printing the flag information.
  • Loading branch information
Philipp Böschen committed Nov 12, 2021
1 parent c0c0e48 commit 6a753e3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ $ semver patch
v1.0.1
```

If you are facing issues with a few tags not being compliant with SemVer you can use the `-i` flag to ignore parsing errors.

```sh
# git repository with a tag like v.2.0.0 and a latest "real" tag like v1.0.0
$ semver -i patch
v1.0.1
```

## Sem package usage

Find most of the detailed docs on [pkg.go.dev](https://pkg.go.dev/github.com/Deichindianer/semver-go).
Expand Down
23 changes: 18 additions & 5 deletions cmd/semver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@ package main

import (
"errors"
"flag"
"fmt"
"os"

"github.com/Deichindianer/semver-go/pkg/sem"
)

var (
ignoreNonSemVerTags = flag.Bool("i", false, "Ignore all tags that are not SemVer compliant instead of failing")
)

func main() {
if len(os.Args) != 2 {
fmt.Print("Usage: semver <kind>\nKind can be one of major,minor,patch\n")
flag.Parse()

flag.Usage = func() {
fmt.Fprint(os.Stderr, "Usage: semver <kind>\nKind can be one of major,minor,patch\n")
flag.PrintDefaults()
}

if len(flag.Args()) != 1 {
flag.Usage()
os.Exit(1)
}
kind, err := sem.ParseKind(os.Args[1])

kind, err := sem.ParseKind(flag.Args()[0])
if err != nil {
fmt.Printf("Usage: semver <kind>\nKind can be one of major,minor,patch\n")
flag.Usage()
os.Exit(1)
}

Expand All @@ -24,7 +37,7 @@ func main() {
fmt.Printf("Failed to get working directory: %s\n", err)
os.Exit(1)
}
latestVersion, err := sem.GetLatestVersion(curDir)
latestVersion, err := sem.GetLatestVersion(curDir, *ignoreNonSemVerTags)
if err != nil {
if errors.Is(err, sem.ErrNoVersionsAvailable) {
// This is an opinionated initial version
Expand Down
9 changes: 6 additions & 3 deletions pkg/sem/sem.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func ParseVersion(version string) (*Ver, error) {
}

// GetAllVersion returns a sorted list of all available git tags, if they all are SemVer compliant
func GetAllVersions(dir string) ([]*Ver, error) {
func GetAllVersions(dir string, skipParseErrors bool) ([]*Ver, error) {
if err := os.Chdir(dir); err != nil {
return nil, err
}
Expand All @@ -170,6 +170,9 @@ func GetAllVersions(dir string) ([]*Ver, error) {
}
version, err := ParseVersion(tag)
if err != nil {
if skipParseErrors {
continue
}
return nil, err
}
versionList = append(versionList, version)
Expand All @@ -181,8 +184,8 @@ func GetAllVersions(dir string) ([]*Ver, error) {
var ErrNoVersionsAvailable = errors.New("no versions available")

// GetLatestVersion gets all versions and returns the latest one
func GetLatestVersion(dir string) (*Ver, error) {
versionList, err := GetAllVersions(dir)
func GetLatestVersion(dir string, ignoreNonSemVerTags bool) (*Ver, error) {
versionList, err := GetAllVersions(dir, ignoreNonSemVerTags)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 6a753e3

Please sign in to comment.