From 9808ceb7a805b9e43e089871fa32aa6dadd24fcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8C=85=E5=AD=90?= Date: Mon, 26 Jul 2021 11:43:13 +0800 Subject: [PATCH] feat(cmd/upgrade): compatible with go get and go install (#1255) * feat(cmd/upgrade): compatible with go get and go install --- cmd/kratos/internal/base/install.go | 24 +++++++++++++++++++ .../internal/base/install_compatible.go | 24 +++++++++++++++++++ cmd/kratos/internal/change/change.go | 9 ++++--- cmd/kratos/internal/{base => change}/get.go | 17 +------------ cmd/kratos/internal/upgrade/upgrade.go | 2 +- 5 files changed, 54 insertions(+), 22 deletions(-) create mode 100644 cmd/kratos/internal/base/install.go create mode 100644 cmd/kratos/internal/base/install_compatible.go rename cmd/kratos/internal/{base => change}/get.go (93%) diff --git a/cmd/kratos/internal/base/install.go b/cmd/kratos/internal/base/install.go new file mode 100644 index 000000000..f8e28fe43 --- /dev/null +++ b/cmd/kratos/internal/base/install.go @@ -0,0 +1,24 @@ +//go:build go1.17 +// +build go1.17 + +package base + +import ( + "fmt" + "os" + "os/exec" +) + +// GoInstall go get path. +func GoInstall(path ...string) error { + for _, p := range path { + fmt.Printf("go install %s@latest\n", p) + cmd := exec.Command("go", "install", fmt.Sprintf("%s@latest", p)) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + return err + } + } + return nil +} diff --git a/cmd/kratos/internal/base/install_compatible.go b/cmd/kratos/internal/base/install_compatible.go new file mode 100644 index 000000000..68480340b --- /dev/null +++ b/cmd/kratos/internal/base/install_compatible.go @@ -0,0 +1,24 @@ +//go:build !go1.17 +// +build !go1.17 + +package base + +import ( + "fmt" + "os" + "os/exec" +) + +// GoInstall go get path. +func GoInstall(path ...string) error { + for _, p := range path { + fmt.Printf("go get -u %s\n", p) + cmd := exec.Command("go", "get", "-u", p) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + return err + } + } + return nil +} diff --git a/cmd/kratos/internal/change/change.go b/cmd/kratos/internal/change/change.go index 647a54c9e..2c14d651a 100644 --- a/cmd/kratos/internal/change/change.go +++ b/cmd/kratos/internal/change/change.go @@ -4,7 +4,6 @@ import ( "fmt" "os" - "github.com/go-kratos/kratos/cmd/kratos/v2/internal/base" "github.com/spf13/cobra" ) @@ -27,17 +26,17 @@ func init() { } func run(cmd *cobra.Command, args []string) { - owner, repo := base.ParseGithubUrl(repoURL) - api := base.GithubApi{Owner: owner, Repo: repo, Token: token} + owner, repo := ParseGithubUrl(repoURL) + api := GithubApi{Owner: owner, Repo: repo, Token: token} version := "latest" if len(args) > 0 { version = args[0] } if version == "dev" { info := api.GetCommitsInfo() - fmt.Print(base.ParseCommitsInfo(info)) + fmt.Print(ParseCommitsInfo(info)) } else { info := api.GetReleaseInfo(version) - fmt.Print(base.ParseReleaseInfo(info)) + fmt.Print(ParseReleaseInfo(info)) } } diff --git a/cmd/kratos/internal/base/get.go b/cmd/kratos/internal/change/get.go similarity index 93% rename from cmd/kratos/internal/base/get.go rename to cmd/kratos/internal/change/get.go index 22ed362b2..51031eef3 100644 --- a/cmd/kratos/internal/base/get.go +++ b/cmd/kratos/internal/change/get.go @@ -1,4 +1,4 @@ -package base +package change import ( "encoding/json" @@ -8,26 +8,11 @@ import ( "io/ioutil" "net/http" "os" - "os/exec" "regexp" "strings" "time" ) -// GoGet go get path. -func GoGet(path ...string) error { - for _, p := range path { - fmt.Printf("go get -u %s\n", p) - cmd := exec.Command("go", "get", "-u", p) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - return err - } - } - return nil -} - type ReleaseInfo struct { Author struct { Login string `json:"login"` diff --git a/cmd/kratos/internal/upgrade/upgrade.go b/cmd/kratos/internal/upgrade/upgrade.go index 01d9879fb..d981442e4 100644 --- a/cmd/kratos/internal/upgrade/upgrade.go +++ b/cmd/kratos/internal/upgrade/upgrade.go @@ -18,7 +18,7 @@ var CmdUpgrade = &cobra.Command{ // Run upgrade the kratos tools. func Run(cmd *cobra.Command, args []string) { - err := base.GoGet( + err := base.GoInstall( "github.com/go-kratos/kratos/cmd/kratos/v2", "github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2", "github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2",