diff options
Diffstat (limited to '')
| -rw-r--r-- | main.go | 85 |
1 files changed, 44 insertions, 41 deletions
@@ -1,17 +1,17 @@ package main import ( + "bufio" "errors" "fmt" + "io" "log" "os" + "slices" "sort" "strconv" "strings" - "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/plumbing" - "github.com/spf13/cobra" "github.com/spf13/viper" @@ -100,38 +100,46 @@ func (prf *preReleaseVersion) incPreRelease() (semver.Version, error) { var ( errNoRemoteFound = errors.New("No remotes found") - clonedRepo = &git.Repository{} - localRepo = &git.Repository{} + validArgs = []string{"major", "minor", "patch"} remoteName string preReleaseFmtArgs map[string]string = make(map[string]string) preRelease bool rootCmd = &cobra.Command{ - Use: "semverbump part [major|minor|patch]", + Use: "semverbump part [major|minor|patch] file [-|file]", Short: "A tool for bumping semver git tags.", - ValidArgs: []string{"major", "minor", "patch"}, + ValidArgs: validArgs, Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), Long: `Inspired by the python bump2version tool.`, RunE: func(cmd *cobra.Command, argz []string) error { part := argz[0] - preRelease := viper.GetBool("prerelease") - preReleaseFmtString := viper.GetString("prerelease-fmt") - // remoteName := viper.GetString("remote-name") - repoDir := viper.GetString("repo-dir") - - localRepo, err := git.PlainOpen(repoDir) - if err != nil { + var err error + if err := checkArgPart(part); err != nil { return err } - // finding tags and last version - tags, err := findTags(localRepo) - if err != nil { - return err + inputScanner := scannerFromInput(cmd.InOrStdin()) + tags := []*semver.Version{} + for inputScanner.Scan() { + verText := inputScanner.Text() + ver, err := semver.NewVersion(verText) + if err != nil { + // log.debug + fmt.Print(err) + continue + } + tags = append(tags, ver) } + if len(tags) == 0 { + return errors.New("No semver version found") + } + + preRelease := viper.GetBool("prerelease") + preReleaseFmtString := viper.GetString("prerelease-fmt") + + // remoteName := viper.GetString("remote-name") lastVersion := latestTag(tags) if (lastVersion == &semver.Version{}) { return errors.New("No tags found. Not doing anything") } - versionPart := "" if preRelease { versionPart = part @@ -166,6 +174,7 @@ var ( ) func latestTag(tags []*semver.Version) *semver.Version { + sort.Sort(semver.Collection(tags)) numTags := len(tags) if numTags == 0 { return &semver.Version{} @@ -173,26 +182,19 @@ func latestTag(tags []*semver.Version) *semver.Version { return tags[numTags-1] } -func findTags(repo *git.Repository) ([]*semver.Version, error) { - repoTagsIter, err := repo.Tags() - if err != nil { - return []*semver.Version{}, err - } - repoTags := []*semver.Version{} - if err := repoTagsIter.ForEach(func(ref *plumbing.Reference) error { - rn := plumbing.ReferenceName(ref.Name()) - version, err := semver.NewVersion(rn.Short()) - if err != nil { - // not a semver - return nil - } - repoTags = append(repoTags, version) - return nil - }); err != nil { - return []*semver.Version{}, err +func scannerFromInput(inputReader io.Reader) *bufio.Scanner { + scanner := bufio.NewScanner(inputReader) + scanner.Split(bufio.ScanLines) + return scanner + +} +func checkArgPart(arg string) error { + isValidPart := slices.Contains(validArgs, arg) + if !isValidPart { + return errors.New("A valid arg part is required") + } - sort.Sort(semver.Collection(repoTags)) - return repoTags, nil + return nil } func parseFmtString(toSplit string) []string { @@ -211,9 +213,10 @@ func init() { viper.SetConfigName(".version.toml") viper.SetConfigType("toml") viper.AddConfigPath(".") - err := viper.ReadInConfig() // Find and read the config file - if err != nil { // Handle errors reading the config file - fmt.Println(err) + if err := viper.ReadInConfig(); err != nil { // Find and read the config file + if errors.Is(err, &viper.ConfigFileNotFoundError{}) { // Handle errors reading the config file + fmt.Println(err) + } } cwd, err := os.Getwd() if err != nil { |