Skip to content

Commit

Permalink
feat: expose existing functionality behind a next command (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
purpleclay authored Apr 5, 2023
1 parent 1d09bce commit e2723e9
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ brews:
name: homebrew-tap
folder: Formula
homepage: "https://github.com/purpleclay/nsv"
description: "Next Semantic Version"
description: "Manage your semantic versioning without any config"
license: MIT
install: |
bin.install "nsv"
Expand All @@ -131,7 +131,7 @@ nfpms:
- file_name_template: "{{ .ConventionalFileName }}"
id: packages
homepage: "https://github.com/purpleclay/nsv"
description: "Next Semantic Version"
description: "Manage your semantic versioning without any config"
maintainer: Purple Clay <[email protected]>
license: MIT
vendor: Purple Clay
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# nsv

Calculate the next semantic version without any config. Just type:
Manage your semantic versioning without any config. Just type:

```go
nsv
nsv next
```
4 changes: 2 additions & 2 deletions cmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/spf13/cobra"
)

var longDesc = `A playground for discovering go template support
var formatLongDesc = `A playground for discovering go template support.
Discover ways of formatting your repository tag using the in-built
go template annotations.
Expand All @@ -51,7 +51,7 @@ func formatCmd(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "format <tag>",
Short: "A playground for discovering go template support",
Long: longDesc,
Long: formatLongDesc,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
return env.Parse(&opts)
Expand Down
73 changes: 73 additions & 0 deletions cmd/next.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright (c) 2023 Purple Clay
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.
*/

package cmd

import (
"io"
"os"

"github.com/caarlos0/env/v7"
git "github.com/purpleclay/gitz"
"github.com/purpleclay/nsv/internal/nsv"
"github.com/spf13/cobra"
)

var nextLongDesc = `Generate the next semantic version based on the
conventional commit history of your repository.
Environment Variables:
| Name | Description |
|------------|---------------------------------------------------|
| NSV_FORMAT | set a go template for formatting the provided tag |`

func nextCmd(out io.Writer) *cobra.Command {
opts := nsv.Options{
StdOut: out,
StdErr: os.Stderr,
}

cmd := &cobra.Command{
Use: "next",
Short: "Generate the next semantic version",
Long: nextLongDesc,
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
return env.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
gitc, err := git.NewClient()
if err != nil {
return err
}

return nsv.NextVersion(gitc, opts)
},
}

flags := cmd.Flags()
flags.BoolVar(&opts.Show, "show", false, "show how the next semantic version was generated")
flags.StringVar(&opts.VersionFormat, "format", "", "provide a go template for changing the default version format")

return cmd
}
31 changes: 4 additions & 27 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ import (
"encoding/json"
"fmt"
"io"
"os"
"runtime"

"github.com/caarlos0/env/v7"
git "github.com/purpleclay/gitz"
"github.com/purpleclay/nsv/internal/nsv"
"github.com/spf13/cobra"
)

Expand All @@ -43,37 +39,18 @@ type BuildDetails struct {
}

func Execute(out io.Writer, buildInfo BuildDetails) error {
opts := nsv.Options{
StdOut: out,
StdErr: os.Stderr,
}

cmd := &cobra.Command{
Use: "nsv",
Short: "Generate the next semantic version",
Short: "Manage your semantic versioning without any config",
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := env.Parse(&opts); err != nil {
return err
}

gitc, err := git.NewClient()
if err != nil {
return err
}

return nsv.NextVersion(gitc, opts)
},
}

flags := cmd.Flags()
flags.BoolVar(&opts.Show, "show", false, "show how the next semantic version was calculated")
flags.StringVar(&opts.VersionFormat, "format", "", "provide a go template for changing the default version format")

cmd.AddCommand(versionCmd(out, buildInfo),
manCmd(out),
formatCmd(out))
formatCmd(out),
nextCmd(out))

return cmd.Execute()
}

Expand Down

0 comments on commit e2723e9

Please sign in to comment.