Skip to content

Commit

Permalink
feat(version): adds command to check current gum version
Browse files Browse the repository at this point in the history
closes #352

Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Dec 13, 2024
1 parent d1bfd56 commit 9bdbe2a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
11 changes: 11 additions & 0 deletions gum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"`
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func main() {
}),
kong.Vars{
"version": version,
"versionNumber": Version,
"defaultHeight": "0",
"defaultWidth": "0",
"defaultAlign": "left",
Expand Down
25 changes: 25 additions & 0 deletions version/command.go
Original file line number Diff line number Diff line change
@@ -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 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 changes: 6 additions & 0 deletions version/options.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit 9bdbe2a

Please sign in to comment.