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 <caarlos0@users.noreply.github.com>

* Update version/command.go

Co-authored-by: Gareth Jones <Jones258@Gmail.com>

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Gareth Jones <Jones258@Gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2024-12-13 16:59:14 -03:00 committed by GitHub
commit 64d69eb59b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 46 additions and 0 deletions

1
go.mod
View file

@ -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

2
go.sum
View file

@ -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=

11
gum.go
View file

@ -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"`
}

View file

@ -54,6 +54,7 @@ func main() {
}),
kong.Vars{
"version": version,
"versionNumber": Version,
"defaultHeight": "0",
"defaultWidth": "0",
"defaultAlign": "left",

25
version/command.go Normal file
View file

@ -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
}

6
version/options.go Normal file
View file

@ -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"`
}