|
| 1 | +/* |
| 2 | +Copyright © 2025 James Evans |
| 3 | +*/ |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/Masterminds/semver/v3" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +// scriptCmd represents the script command |
| 15 | +var scriptCmd = &cobra.Command{ |
| 16 | + Use: "script", |
| 17 | + Short: "Script utilities for semantic versioning", |
| 18 | + Long: `Provides utilities for scripting with semantic versions. |
| 19 | + |
| 20 | +These commands are designed to be used in shell scripts, returning exit codes |
| 21 | +that can be used in conditionals.`, |
| 22 | +} |
| 23 | + |
| 24 | +// CompareVersions compares two semantic versions and returns: |
| 25 | +// 0 if versions are equal |
| 26 | +// 11 if v1 is greater than v2 (v1 is newer) |
| 27 | +// 12 if v2 is greater than v1 (v2 is newer) |
| 28 | +// Returns an error if either version is invalid |
| 29 | +func CompareVersions(v1string, v2string string) (int, error) { |
| 30 | + v1, err := semver.NewVersion(v1string) |
| 31 | + if err != nil { |
| 32 | + return 0, fmt.Errorf("invalid version: %s", v1string) |
| 33 | + } |
| 34 | + |
| 35 | + v2, err := semver.NewVersion(v2string) |
| 36 | + if err != nil { |
| 37 | + return 0, fmt.Errorf("invalid version: %s", v2string) |
| 38 | + } |
| 39 | + |
| 40 | + if v1.LessThan(v2) { |
| 41 | + return 12, nil // v2 is newer |
| 42 | + } else if v1.Equal(v2) { |
| 43 | + return 0, nil // equal versions |
| 44 | + } else { |
| 45 | + return 11, nil // v1 is newer |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// IsReleased checks if a version is a release version (no prerelease or metadata) |
| 50 | +// Returns true for release versions, false for prerelease versions or those with metadata |
| 51 | +// Returns an error if the version is invalid |
| 52 | +func IsReleased(versionString string) (bool, error) { |
| 53 | + v, err := semver.NewVersion(versionString) |
| 54 | + if err != nil { |
| 55 | + return false, fmt.Errorf("invalid version: %s", versionString) |
| 56 | + } |
| 57 | + |
| 58 | + return v.Prerelease() == "" && v.Metadata() == "", nil |
| 59 | +} |
| 60 | + |
| 61 | +// compareCmd represents the compare subcommand |
| 62 | +var compareCmd = &cobra.Command{ |
| 63 | + Use: "compare <version1> <version2>", |
| 64 | + Short: "Compare two semantic versions", |
| 65 | + Long: `Compare two semantic versions and return an exit code based on the comparison: |
| 66 | + |
| 67 | + 0: version1 = version2 |
| 68 | + 11: version1 > version2 |
| 69 | + 12: version1 < version2 |
| 70 | + |
| 71 | + If there is an error, the command will return 1.`, |
| 72 | + Args: cobra.ExactArgs(2), |
| 73 | + Run: func(cmd *cobra.Command, args []string) { |
| 74 | + result, err := CompareVersions(args[0], args[1]) |
| 75 | + if err != nil { |
| 76 | + fmt.Fprintf(os.Stderr, "%s\n", err) |
| 77 | + os.Exit(1) |
| 78 | + } |
| 79 | + os.Exit(result) |
| 80 | + }, |
| 81 | +} |
| 82 | + |
| 83 | +// releasedCmd represents the released subcommand |
| 84 | +var releasedCmd = &cobra.Command{ |
| 85 | + Use: "released <version>", |
| 86 | + Short: "Check if a version is a release version", |
| 87 | + Long: `Check if a version is a release version (not a prerelease and has no metadata). |
| 88 | + |
| 89 | +Returns exit code 0 if the version is a release version (X.Y.Z only), |
| 90 | +Returns exit code 1 if the version is a prerelease or has metadata.`, |
| 91 | + Args: cobra.ExactArgs(1), |
| 92 | + Run: func(cmd *cobra.Command, args []string) { |
| 93 | + isReleased, err := IsReleased(args[0]) |
| 94 | + if err != nil { |
| 95 | + fmt.Fprintf(os.Stderr, "%s\n", err) |
| 96 | + os.Exit(1) |
| 97 | + } |
| 98 | + |
| 99 | + if isReleased { |
| 100 | + os.Exit(0) |
| 101 | + } else { |
| 102 | + os.Exit(1) |
| 103 | + } |
| 104 | + }, |
| 105 | +} |
| 106 | + |
| 107 | +func init() { |
| 108 | + scriptCmd.AddCommand(compareCmd) |
| 109 | + scriptCmd.AddCommand(releasedCmd) |
| 110 | +} |
0 commit comments