Skip to content

Commit

Permalink
almost working show VERSION command
Browse files Browse the repository at this point in the history
shows slightly to much (2 characters at the end)
  • Loading branch information
NiclasvanEyk committed Jun 20, 2023
1 parent f65c5b9 commit 07ad321
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 10 deletions.
98 changes: 94 additions & 4 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,33 @@ var shouldShowPlain bool

// showCmd represents the show command
var showCmd = &cobra.Command{
Use: "show",
Use: "show [VERSION|latest|next|unreleased]",
Short: "Displays the contents of the nearest changelog.",
Long: `Displays the contents of the nearest changelog.`,
Long: `Displays the contents of the nearest changelog.
If a VERSION (e.g. "1.2.3") is specified, only the release notes for that given version will be shown.
Instead of a specific version you can also use one of the following aliases:
- "latest" will show the latest release
- "next" or "unreleased" will show the contents of the [Unreleased]
`,
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: func(
cmd *cobra.Command,
args []string,
toComplete string,
) ([]string, cobra.ShellCompDirective) {
matchingVersions, directive := clog.CompleteReleasesAsFirstArgument(cmd, args, toComplete)
if matchingVersions == nil {
return matchingVersions, directive
}

// We also support 'latest' as an alias
matchingVersions = append(matchingVersions, "latest")
matchingVersions = append(matchingVersions, "next")
matchingVersions = append(matchingVersions, "unreleased")

return matchingVersions, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
path, err := clog.ResolvePathToChangelog()
if err != nil {
Expand All @@ -27,12 +51,30 @@ var showCmd = &cobra.Command{
return err
}

if len(args) == 0 {
if shouldShowPlain {
fmt.Print(string(source))
return nil
}

return clog.Show(string(source))
}

changelog := clog.Parse(source)
// TODO: These bounds are always off by two (too much) at the Stop
bounds, err := findReleaseBounds(args[0], &changelog)
if err != nil {
return err
}

contents := changelog.ContentWithin(bounds)

if shouldShowPlain {
fmt.Print(string(source))
fmt.Print(contents)
return nil
}

return clog.Show(string(source))
return clog.Show(contents)
},
}

Expand All @@ -51,3 +93,51 @@ func init() {
// is called directly, e.g.:
// showCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func isAlias(alias string) bool {
versionAliases := []string{
"latest",
"next",
"unreleased",
}
for _, knownAlias := range versionAliases {
if alias == knownAlias {
return true
}
}

return false
}

func findReleaseBounds(versionOrAlias string, changelog *clog.Changelog) (*clog.Bounds, error) {
if isAlias(versionOrAlias) {
alias := versionOrAlias

if alias == "latest" {
if len(changelog.Releases.Past) < 1 {
return nil, fmt.Errorf("Cannot show latest release, since there are none")
}

return &changelog.Releases.Past[0].Bounds, nil
}

if alias == "next" || alias == "unreleased" {
nextRelease := changelog.Releases.Next
if nextRelease == nil {
return nil, fmt.Errorf("Cannot show next release, since there is none")
}

return &nextRelease.Bounds, nil
}

return nil, fmt.Errorf("Unknown version or alias '%s'")
}

version := versionOrAlias
release := changelog.FindRelease(version)
if release == nil {
return nil, fmt.Errorf("Release '%s' not found", version)
}

return &release.Bounds, nil
}
33 changes: 28 additions & 5 deletions cmd/yank.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

Expand All @@ -11,11 +12,33 @@ import (

// yankCmd represents the yank command
var yankCmd = &cobra.Command{
Use: "yank",
Short: "Marks the specified release as yanked",
Long: `As described by https://keepachangelog.com, yanked releases are versions that had to be pulled because of a serious bug or security issue.`,
Args: cobra.ExactArgs(1),
ValidArgsFunction: clog.CompleteReleasesAsFirstArgument,
Use: "yank",
Short: "Marks the specified release as yanked",
Long: `As described by https://keepachangelog.com, yanked releases are versions that had to be pulled because of a serious bug or security issue.`,
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(
cmd *cobra.Command,
args []string,
toComplete string,
) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

changelog, _, err := clog.ResolveChangelog()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

matchingVersions := make([]string, 0)
for _, release := range changelog.Releases.Past {
if !release.Yanked && strings.HasPrefix(release.Version, toComplete) {
matchingVersions = append(matchingVersions, release.Version)
}
}

return matchingVersions, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
changelog, changelogPath, err := clog.ResolveChangelog()
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion internal/changelog/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import (

// Can be passed as ValidArgsFunction to support custom completions if the
// first argument is a released version.
func CompleteReleasesAsFirstArgument(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
func CompleteReleasesAsFirstArgument(
cmd *cobra.Command,
args []string,
toComplete string,
) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
Expand Down
4 changes: 4 additions & 0 deletions internal/changelog/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"github.com/charmbracelet/glamour"
)

func (changelog *Changelog) ContentWithin(bounds *Bounds) string {
return changelog.source[bounds.Start:bounds.Stop]
}

func Show(contents string) error {
renderer, _ := glamour.NewTermRenderer(
// detect background color and pick either the default dark or light theme
Expand Down

0 comments on commit 07ad321

Please sign in to comment.