diff --git a/cmd/kitty/main.go b/cmd/kitty/main.go index 43bd561..e4d9a1a 100644 --- a/cmd/kitty/main.go +++ b/cmd/kitty/main.go @@ -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" @@ -107,6 +109,11 @@ func main() { } } + err := mayUseAnotherKitty() + if err != nil { + return err + } + return nil } @@ -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 {