From bcdae4598863338b3dbf8a893e6b03408faccd43 Mon Sep 17 00:00:00 2001 From: srstack Date: Thu, 23 Dec 2021 18:52:51 +0800 Subject: [PATCH 1/5] tiup: update --self need to add some judgments. --- cmd/update.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/cmd/update.go b/cmd/update.go index fa9f9a87ef..76f0fce588 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -16,7 +16,10 @@ package cmd import ( "fmt" "os" + "path/filepath" + "strings" + "github.com/fatih/color" "github.com/pingcap/tiup/pkg/environment" "github.com/pingcap/tiup/pkg/utils" "github.com/spf13/cobra" @@ -47,6 +50,10 @@ latest version. All other flags will be ignored if the flag --self is given. env := environment.GlobalEnv() if self { + if err := checkTiUPBinary(env); err != nil { + return err + } + originFile := env.LocalPath("bin", "tiup") renameFile := env.LocalPath("bin", "tiup.tmp") if err := os.Rename(originFile, renameFile); err != nil { @@ -100,3 +107,29 @@ func updateComponents(env *environment.Environment, components []string, nightly return env.UpdateComponents(components, nightly, force) } + +// checkTiUPBinary check if TiUP exists in TiUP_HOME +func checkTiUPBinary(env *environment.Environment) error { + tiUPHomePath, _ := filepath.Abs(env.LocalPath("bin", "tiup")) + + realTiUPPath, err := os.Executable() + if err != nil { + // Ignore the problem that the execution directory cannot be obtained + return nil + } + realTiUPPath, _ = filepath.Abs(realTiUPPath) + + if utils.IsNotExist(tiUPHomePath) || tiUPHomePath != realTiUPPath { + fmt.Printf("Tiup install directory is: %s\n", filepath.Dir(realTiUPPath)) + if strings.Contains(strings.ToLower(realTiUPPath), "homebrew") { + fmt.Printf("Maybe you are using `%s` TiUP installed, you can try to use `%s` to upgrade.\n", + color.HiYellowString("brew install pingcap/brew/tiup"), + color.HiYellowString("brew upgrade pingcap/brew/tiup"), + ) + } + + return fmt.Errorf("failed to upgrade TiUP, please use the install method to upgrade") + } + + return nil +} From 30594b0599c7fd05e7e6d9bb0a5998f041226b42 Mon Sep 17 00:00:00 2001 From: srstack Date: Thu, 23 Dec 2021 18:58:12 +0800 Subject: [PATCH 2/5] perfect tips --- cmd/update.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/update.go b/cmd/update.go index 76f0fce588..b1aa7597a6 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -122,7 +122,7 @@ func checkTiUPBinary(env *environment.Environment) error { if utils.IsNotExist(tiUPHomePath) || tiUPHomePath != realTiUPPath { fmt.Printf("Tiup install directory is: %s\n", filepath.Dir(realTiUPPath)) if strings.Contains(strings.ToLower(realTiUPPath), "homebrew") { - fmt.Printf("Maybe you are using `%s` TiUP installed, you can try to use `%s` to upgrade.\n", + fmt.Printf("Maybe you used `%s` to install TiUP, you can try to use `%s` to upgrade.\n", color.HiYellowString("brew install pingcap/brew/tiup"), color.HiYellowString("brew upgrade pingcap/brew/tiup"), ) From bdb74f9591ce1da829153c67eee68db4de99cf89 Mon Sep 17 00:00:00 2001 From: srstack Date: Mon, 27 Dec 2021 15:53:52 +0800 Subject: [PATCH 3/5] perfect tips --- cmd/update.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/update.go b/cmd/update.go index b1aa7597a6..513c848cea 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -122,7 +122,7 @@ func checkTiUPBinary(env *environment.Environment) error { if utils.IsNotExist(tiUPHomePath) || tiUPHomePath != realTiUPPath { fmt.Printf("Tiup install directory is: %s\n", filepath.Dir(realTiUPPath)) if strings.Contains(strings.ToLower(realTiUPPath), "homebrew") { - fmt.Printf("Maybe you used `%s` to install TiUP, you can try to use `%s` to upgrade.\n", + fmt.Printf("Maybe you used `%s` to install TiUP, try to use `%s` to upgrade.\n", color.HiYellowString("brew install pingcap/brew/tiup"), color.HiYellowString("brew upgrade pingcap/brew/tiup"), ) From f2eb12f40d50b4c1ef158ba40e01a419be200b2e Mon Sep 17 00:00:00 2001 From: srstack Date: Tue, 28 Dec 2021 16:21:50 +0800 Subject: [PATCH 4/5] perfect tips --- cmd/update.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/cmd/update.go b/cmd/update.go index 513c848cea..4f352933c5 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -17,9 +17,7 @@ import ( "fmt" "os" "path/filepath" - "strings" - "github.com/fatih/color" "github.com/pingcap/tiup/pkg/environment" "github.com/pingcap/tiup/pkg/utils" "github.com/spf13/cobra" @@ -121,14 +119,7 @@ func checkTiUPBinary(env *environment.Environment) error { if utils.IsNotExist(tiUPHomePath) || tiUPHomePath != realTiUPPath { fmt.Printf("Tiup install directory is: %s\n", filepath.Dir(realTiUPPath)) - if strings.Contains(strings.ToLower(realTiUPPath), "homebrew") { - fmt.Printf("Maybe you used `%s` to install TiUP, try to use `%s` to upgrade.\n", - color.HiYellowString("brew install pingcap/brew/tiup"), - color.HiYellowString("brew upgrade pingcap/brew/tiup"), - ) - } - - return fmt.Errorf("failed to upgrade TiUP, please use the install method to upgrade") + return fmt.Errorf("If you used some external package manager to install TiUP (e.g., brew), try upgrade with that.") } return nil From 9beb6af1dc13989694c4624bbcad02ce0a13d8cd Mon Sep 17 00:00:00 2001 From: srstack Date: Tue, 28 Dec 2021 16:27:04 +0800 Subject: [PATCH 5/5] perfect tips --- cmd/update.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/update.go b/cmd/update.go index 4f352933c5..aee84c7426 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -119,7 +119,7 @@ func checkTiUPBinary(env *environment.Environment) error { if utils.IsNotExist(tiUPHomePath) || tiUPHomePath != realTiUPPath { fmt.Printf("Tiup install directory is: %s\n", filepath.Dir(realTiUPPath)) - return fmt.Errorf("If you used some external package manager to install TiUP (e.g., brew), try upgrade with that.") + return fmt.Errorf("If you used some external package manager to install TiUP (e.g., brew), try upgrade with that") } return nil