From 64d69eb59bed32206a3c1f8bafada435aa1a4911 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 13 Dec 2024 16:59:14 -0300 Subject: [PATCH] feat(version): adds command to check current gum version (#775) * feat(version): adds command to check current gum version closes #352 Signed-off-by: Carlos Alexandro Becker * Update version/command.go Co-authored-by: Gareth Jones --------- Signed-off-by: Carlos Alexandro Becker Co-authored-by: Gareth Jones --- go.mod | 1 + go.sum | 2 ++ gum.go | 11 +++++++++++ main.go | 1 + version/command.go | 25 +++++++++++++++++++++++++ version/options.go | 6 ++++++ 6 files changed, 46 insertions(+) create mode 100644 version/command.go create mode 100644 version/options.go diff --git a/go.mod b/go.mod index 14d781e..dc913fd 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/charmbracelet/gum go 1.21 require ( + github.com/Masterminds/semver/v3 v3.3.1 github.com/alecthomas/kong v1.6.0 github.com/alecthomas/mango-kong v0.1.0 github.com/charmbracelet/bubbles v0.20.0 diff --git a/go.sum b/go.sum index a1c5b5b..8c72ddd 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= +github.com/Masterminds/semver/v3 v3.3.1 h1:QtNSWtVZ3nBfk8mAOu/B6v7FMJ+NHTIgUPi7rj+4nv4= +github.com/Masterminds/semver/v3 v3.3.1/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM= github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0= github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= github.com/alecthomas/chroma/v2 v2.14.0 h1:R3+wzpnUArGcQz7fCETQBzO5n9IMNi13iIs46aU4V9E= diff --git a/gum.go b/gum.go index 7a59f20..4191314 100644 --- a/gum.go +++ b/gum.go @@ -17,6 +17,7 @@ import ( "github.com/charmbracelet/gum/spin" "github.com/charmbracelet/gum/style" "github.com/charmbracelet/gum/table" + "github.com/charmbracelet/gum/version" "github.com/charmbracelet/gum/write" ) @@ -214,4 +215,14 @@ type Gum struct { // $ gum log --level info "Hello, world!" // Log log.Options `cmd:"" help:"Log messages to output"` + + // VersionCheck provides a command that checks if the current gum version + // matches a given semantic version constraint. + // + // It can be used to check that a minimum gum version is installed in a + // script. + // + // $ gum version-check '~> 0.15' + // + VersionCheck version.Options `cmd:"" help:"Semver check current gum version"` } diff --git a/main.go b/main.go index 75741e2..18fa9f0 100644 --- a/main.go +++ b/main.go @@ -54,6 +54,7 @@ func main() { }), kong.Vars{ "version": version, + "versionNumber": Version, "defaultHeight": "0", "defaultWidth": "0", "defaultAlign": "left", diff --git a/version/command.go b/version/command.go new file mode 100644 index 0000000..5102b34 --- /dev/null +++ b/version/command.go @@ -0,0 +1,25 @@ +package version + +import ( + "fmt" + + "github.com/Masterminds/semver/v3" + "github.com/alecthomas/kong" +) + +// Run check that a given version matches a semantic version constraint. +func (o Options) Run(ctx *kong.Context) error { + c, err := semver.NewConstraint(o.Constraint) + if err != nil { + return fmt.Errorf("could not parse range %s: %w", o.Constraint, err) + } + current := ctx.Model.Vars()["versionNumber"] + v, err := semver.NewVersion(current) + if err != nil { + return fmt.Errorf("could not parse version %s: %w", current, err) + } + if !c.Check(v) { + return fmt.Errorf("gum version %q is not within given range %q", current, o.Constraint) + } + return nil +} diff --git a/version/options.go b/version/options.go new file mode 100644 index 0000000..2dbb19d --- /dev/null +++ b/version/options.go @@ -0,0 +1,6 @@ +package version + +// Options is the set of options that can be used with version. +type Options struct { + Constraint string `arg:"" help:"Semantic version constraint"` +}