Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions internal/svu/svu.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package svu

import (
"encoding/json"
"fmt"
"log"
"regexp"
Expand Down Expand Up @@ -39,6 +40,18 @@ type Options struct {
TagMode string
Always bool
KeepV0 bool
Json bool
}

type VersionInfo struct {
Version string `json:"version"`
Major uint64 `json:"major"`
Minor uint64 `json:"minor"`
Patch uint64 `json:"patch"`
Prefix string `json:"prefix,omitempty"`
Metadata string `json:"metadata,omitempty"`
Prerelease string `json:"prerelease,omitempty"`
Build string `json:"build,omitempty"`
}

func Version(opts Options) (string, error) {
Expand All @@ -57,6 +70,10 @@ func Version(opts Options) (string, error) {
return "", fmt.Errorf("could not get next tag: '%s': %w", tag, err)
}

if opts.Json {
return jsonOutput(result, opts)
}

return opts.Prefix + result.String(), nil
}

Expand Down Expand Up @@ -243,3 +260,28 @@ func findNext(current *semver.Version, changes []git.Commit, opts Options) semve
}
return *current
}

func jsonOutput(v semver.Version, opts Options) (string, error) {
info := VersionInfo{
Prefix: opts.Prefix,
Version: opts.Prefix + v.String(),
Major: v.Major(),
Minor: v.Minor(),
Patch: v.Patch(),
Metadata: v.Metadata(),
Prerelease: v.Prerelease(),
}

// Split prerelease into prerelease + build if it has a dot
if release := strings.SplitN(v.Prerelease(), ".", 2); len(release) == 2 {
info.Prerelease = release[0]
info.Build = release[1]
}

b, err := json.Marshal(info)
if err != nil {
return "", fmt.Errorf("failed to convert version to json: %w", err)
}

return string(b), nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func main() {
prereleaseCmd,
} {
// init does not share these flags.
cmd.Flags().BoolVar(&opts.Json, "json", false, "output version as json")
cmd.Flags().StringVar(&opts.Pattern, "tag.pattern", "", "ignore tags that do not match the given pattern")
cmd.Flags().StringVar(&opts.Prefix, "tag.prefix", "v", "sets a tag custom prefix")
cmd.Flags().StringVar(&opts.TagMode, "tag.mode", git.TagModeAll, "determine if it should look for tags in all branches, or just the current one")
Expand Down