-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
048a638
commit b42c9ad
Showing
6 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
clog "github.com/niclasvaneyk/keepac/internal/changelog" | ||
) | ||
|
||
// 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, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
changelog, changelogPath, err := clog.ResolveChangelog() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
target := args[0] | ||
|
||
newSource, err := changelog.Yank(target) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = os.WriteFile(changelogPath, []byte(newSource), 0774) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Marked '%s' as yanked!\n", target) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(yankCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// yankCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// yankCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package changelog | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// 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) { | ||
if len(args) != 0 { | ||
return nil, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
|
||
changelog, _, err := ResolveChangelog() | ||
if err != nil { | ||
return nil, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
|
||
matchingVersions := make([]string, 0) | ||
for _, release := range changelog.Releases.Past { | ||
if strings.HasPrefix(release.Version, toComplete) { | ||
matchingVersions = append(matchingVersions, release.Version) | ||
} | ||
} | ||
|
||
return matchingVersions, cobra.ShellCompDirectiveNoFileComp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package changelog | ||
|
||
import "fmt" | ||
|
||
func (changelog *Changelog) Yank(version string) (string, error) { | ||
release := changelog.FindRelease(version) | ||
if release == nil { | ||
return "", fmt.Errorf("Release '%s' not found", version) | ||
} | ||
|
||
if release.Yanked { | ||
return "", fmt.Errorf("Release '%s' was already yanked", version) | ||
} | ||
|
||
return changelog.WithAddition(release.HeadlineBounds.Stop, " [YANKED]"), nil | ||
} |