From 26e55210da2b2adca8f124c63f4ef555b0779072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Qi=CE=BC=24hi=D0=AFu=C3=AD?= <39378935+srstack@users.noreply.github.com> Date: Tue, 28 Dec 2021 18:57:49 +0800 Subject: [PATCH] TiUP: `update --self` add some checks. (#1695) --- cmd/update.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/cmd/update.go b/cmd/update.go index fa9f9a87ef..aee84c7426 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -16,6 +16,7 @@ package cmd import ( "fmt" "os" + "path/filepath" "github.com/pingcap/tiup/pkg/environment" "github.com/pingcap/tiup/pkg/utils" @@ -47,6 +48,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 +105,22 @@ 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)) + return fmt.Errorf("If you used some external package manager to install TiUP (e.g., brew), try upgrade with that") + } + + return nil +}