Skip to content

Commit

Permalink
Add a current kind to the CLI only
Browse files Browse the repository at this point in the history
This new kind on the CLI controls if you just want to print out the
latest version rather than getting the next one.
It makes no changes to the behaviour of the package itself.
  • Loading branch information
Philipp Böschen committed Nov 13, 2021
1 parent 74ade51 commit 0c989ad
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions cmd/semver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
flag.Parse()

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

Expand All @@ -26,17 +26,12 @@ func main() {
os.Exit(1)
}

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

curDir, err := os.Getwd()
if err != nil {
fmt.Printf("Failed to get working directory: %s\n", err)
os.Exit(1)
}

latestVersion, err := sem.GetLatestVersion(curDir, *ignoreNonSemVerTags)
if err != nil {
if errors.Is(err, sem.ErrNoVersionsAvailable) {
Expand All @@ -48,6 +43,20 @@ func main() {
os.Exit(1)
}
}

k := flag.Args()[0]

if k == "current" {
fmt.Println(latestVersion)
os.Exit(0)
}

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

if err := latestVersion.Next(kind); err != nil {
fmt.Printf("Failed to get next version: %s\n", err)
os.Exit(1)
Expand Down

0 comments on commit 0c989ad

Please sign in to comment.