Skip to content

Commit

Permalink
check kitty version
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSingee committed Jan 4, 2024
1 parent 2d14cf2 commit e824f5f
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions cmd/kitty/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (

"github.com/ImSingee/go-ex/ee"
"github.com/ImSingee/go-ex/pp"
"github.com/ImSingee/semver"
"github.com/spf13/cobra"
"github.com/ysmood/gson"

"github.com/ImSingee/kitty/internal/config"
"github.com/ImSingee/kitty/internal/ext/format"
Expand Down Expand Up @@ -107,6 +109,11 @@ func main() {
}
}

err := mayUseAnotherKitty()
if err != nil {
return err
}

return nil
}

Expand All @@ -121,6 +128,83 @@ func main() {
}
}

func mayUseAnotherKitty() error {
wd, err := os.Getwd()
if err != nil {
return ee.Wrap(err, "cannot get working directory")
}

gitRoot, err := git.GetRoot(wd)
if err != nil {
return nil // cannot found git root
}

c, err := config.GetKittyConfig(gitRoot)
if err != nil {
if config.IsNotExist(err) {
return nil // no kitty config
} else {
return ee.Wrap(err, "cannot get kitty config")
}
}

requiredVersion := parseRequiredKittyVersion(c)
if requiredVersion == "" {
return nil // no required version
}

// TODO auto download new kitty version
if requiredVersion[0] == '=' {
sv, err := semver.NewVersion(requiredVersion[1:])
if err != nil {
return ee.Wrapf(err, "invalid kitty required version %s", requiredVersion)
}

if !sv.Equal(version.Semver()) {
pp.Println("Please use kitty ", requiredVersion[1:], "to run this command")
pp.Println("Visit https://github.com/ImSingee/kitty/releases/tag/v" + requiredVersion[1:] + " to download")
return ee.Phantom
}
} else {
sv, err := semver.NewVersion(strings.TrimPrefix(requiredVersion, ">"))
if err != nil {
return ee.Wrapf(err, "invalid kitty required version %s", requiredVersion)
}

if version.LessThan(sv) {
pp.Println("Please use kitty ", requiredVersion, "or later to run this command")
pp.Println("Visit https://github.com/ImSingee/kitty/releases to download")
return ee.Phantom
}
}

return nil
}

func parseRequiredKittyVersion(c map[string]gson.JSON) string {
if c == nil {
return ""
}

kitty, kittyExists := c["kitty"]
if !kittyExists {
return ""
}

switch kittyVal := kitty.Val().(type) {
case string:
return kittyVal
case map[string]any:
if v, ok := kittyVal["version"].(string); ok {
return v
}

return ""
default:
return "" // unknown kitty config type
}
}

func runExtension(name string, args []string) error {
root, err := git.GetRoot("")
if err != nil {
Expand Down

0 comments on commit e824f5f

Please sign in to comment.