From 2e089a28b944512ce308c0a2b243bff53ef957b6 Mon Sep 17 00:00:00 2001 From: nexustar Date: Wed, 15 Sep 2021 20:42:41 +0800 Subject: [PATCH] remove TIUP_WORK_DIR (#1556) --- cmd/help.go | 7 ------- components/cluster/command/root.go | 7 ------- components/ctl/main.go | 7 ------- components/dm/command/root.go | 7 ------- components/playground/main.go | 5 ++++- components/playground/playground_test.go | 5 ++--- pkg/exec/run.go | 6 ------ pkg/localdata/constant.go | 3 --- pkg/tui/cliutil.go | 3 ++- server/package/package.go | 12 ------------ 10 files changed, 8 insertions(+), 54 deletions(-) diff --git a/cmd/help.go b/cmd/help.go index 601fb18d74..da6bf175d7 100644 --- a/cmd/help.go +++ b/cmd/help.go @@ -65,16 +65,9 @@ func externalHelp(env *environment.Environment, spec string, args ...string) { return } - tiupWd, err := os.Getwd() - if err != nil { - fmt.Println(err) - return - } - sd := profile.Path(filepath.Join(localdata.StorageParentDir, strings.Split(spec, ":")[0])) envs := []string{ fmt.Sprintf("%s=%s", localdata.EnvNameHome, profile.Root()), - fmt.Sprintf("%s=%s", localdata.EnvNameWorkDir, tiupWd), fmt.Sprintf("%s=%s", localdata.EnvNameComponentInstallDir, installPath), fmt.Sprintf("%s=%s", localdata.EnvNameComponentDataDir, sd), } diff --git a/components/cluster/command/root.go b/components/cluster/command/root.go index f4c6024bb9..d5d3508eca 100644 --- a/components/cluster/command/root.go +++ b/components/cluster/command/root.go @@ -258,13 +258,6 @@ func Execute() { zap.L().Info("Execute command", zap.String("command", tui.OsArgs())) zap.L().Debug("Environment variables", zap.Strings("env", os.Environ())) - // Switch current work directory if running in TiUP component mode - if wd := os.Getenv(localdata.EnvNameWorkDir); wd != "" { - if err := os.Chdir(wd); err != nil { - zap.L().Warn("Failed to switch work directory", zap.String("working_dir", wd), zap.Error(err)) - } - } - teleReport = new(telemetry.Report) clusterReport = new(telemetry.ClusterReport) teleReport.EventDetail = &telemetry.Report_Cluster{Cluster: clusterReport} diff --git a/components/ctl/main.go b/components/ctl/main.go index 7c453b36fa..71719e2544 100644 --- a/components/ctl/main.go +++ b/components/ctl/main.go @@ -101,13 +101,6 @@ func binaryPath(home, cmd string) (string, error) { } func run(name string, args ...string) error { - // Change the working directory to the same with TiUP working directory - if wd := os.Getenv(localdata.EnvNameWorkDir); wd != "" { - if err := os.Chdir(wd); err != nil { - return err - } - } - // Handle `cdc cli` if strings.Contains(name, " ") { xs := strings.Split(name, " ") diff --git a/components/dm/command/root.go b/components/dm/command/root.go index 689624f487..eced5edaec 100644 --- a/components/dm/command/root.go +++ b/components/dm/command/root.go @@ -218,13 +218,6 @@ func Execute() { zap.L().Info("Execute command", zap.String("command", tui.OsArgs())) zap.L().Debug("Environment variables", zap.Strings("env", os.Environ())) - // Switch current work directory if running in TiUP component mode - if wd := os.Getenv(localdata.EnvNameWorkDir); wd != "" { - if err := os.Chdir(wd); err != nil { - zap.L().Warn("Failed to switch work directory", zap.String("working_dir", wd), zap.Error(err)) - } - } - code := 0 err := rootCmd.Execute() if err != nil { diff --git a/components/playground/main.go b/components/playground/main.go index 04f0ef9610..b59bc9773d 100644 --- a/components/playground/main.go +++ b/components/playground/main.go @@ -546,7 +546,10 @@ func getAbsolutePath(path string) (string, error) { } if !filepath.IsAbs(path) && !strings.HasPrefix(path, "~/") { - wd := os.Getenv(localdata.EnvNameWorkDir) + wd, err := os.Getwd() + if err != nil { + return "", err + } if wd == "" { return "", errors.New("playground running at non-tiup mode") } diff --git a/components/playground/playground_test.go b/components/playground/playground_test.go index 4259dfd9fa..1e7efbbc5e 100644 --- a/components/playground/playground_test.go +++ b/components/playground/playground_test.go @@ -19,17 +19,16 @@ import ( "path/filepath" "testing" - "github.com/pingcap/tiup/pkg/localdata" "github.com/stretchr/testify/assert" ) func TestPlaygroundAbsDir(t *testing.T) { - err := os.Setenv(localdata.EnvNameWorkDir, "/testing") + err := os.Chdir("/usr") assert.Nil(t, err) a, err := getAbsolutePath("./a") assert.Nil(t, err) - assert.Equal(t, "/testing/a", a) + assert.Equal(t, "/usr/a", a) b, err := getAbsolutePath("../b") assert.Nil(t, err) diff --git a/pkg/exec/run.go b/pkg/exec/run.go index 511f42ea92..4470e18b8b 100644 --- a/pkg/exec/run.go +++ b/pkg/exec/run.go @@ -212,11 +212,6 @@ func PrepareCommand(p *PrepareCommandParams) (*exec.Cmd, error) { return nil, err } - tiupWd, err := os.Getwd() - if err != nil { - return nil, err - } - teleMeta, _, err := telemetry.GetMeta(env) if err != nil { return nil, err @@ -224,7 +219,6 @@ func PrepareCommand(p *PrepareCommandParams) (*exec.Cmd, error) { envs := []string{ fmt.Sprintf("%s=%s", localdata.EnvNameHome, profile.Root()), - fmt.Sprintf("%s=%s", localdata.EnvNameWorkDir, tiupWd), fmt.Sprintf("%s=%s", localdata.EnvNameUserInputVersion, p.Version.String()), fmt.Sprintf("%s=%s", localdata.EnvNameTiUPVersion, version.NewTiUPVersion().SemVer()), fmt.Sprintf("%s=%s", localdata.EnvNameInstanceDataDir, p.InstanceDir), diff --git a/pkg/localdata/constant.go b/pkg/localdata/constant.go index eb843658d9..4ea3dac28f 100644 --- a/pkg/localdata/constant.go +++ b/pkg/localdata/constant.go @@ -54,9 +54,6 @@ const ( // EnvNameComponentInstallDir represents the install directory of specific component EnvNameComponentInstallDir = "TIUP_COMPONENT_INSTALL_DIR" - // EnvNameWorkDir represents the work directory of TiUP where user type the command `tiup xxx` - EnvNameWorkDir = "TIUP_WORK_DIR" - // EnvNameUserInputVersion represents the version user specified when running a component by `tiup component:version` EnvNameUserInputVersion = "TIUP_USER_INPUT_VERSION" diff --git a/pkg/tui/cliutil.go b/pkg/tui/cliutil.go index a16a569751..3fdb23887d 100644 --- a/pkg/tui/cliutil.go +++ b/pkg/tui/cliutil.go @@ -47,7 +47,8 @@ func RegisterArg0(s string) { } func args() []string { - if wd := os.Getenv(localdata.EnvNameWorkDir); wd != "" { + // if running in TiUP component mode + if wd := os.Getenv(localdata.EnvNameTiUPVersion); wd != "" { return append([]string{arg0}, os.Args[1:]...) } return os.Args diff --git a/server/package/package.go b/server/package/package.go index cf8aadf091..29978aae28 100644 --- a/server/package/package.go +++ b/server/package/package.go @@ -19,7 +19,6 @@ import ( "os/exec" "runtime" - "github.com/pingcap/tiup/pkg/localdata" "github.com/spf13/cobra" ) @@ -50,9 +49,6 @@ func execute() error { Short: "Package a tiup component and generate package directory", SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { - if err := chwd(); err != nil { - return err - } if len(args) < 1 { return cmd.Help() } @@ -103,11 +99,3 @@ func packTarget(targets []string, options packageOptions) error { } return nil } - -func chwd() error { - pwd, found := os.LookupEnv(localdata.EnvNameWorkDir) - if !found { - return fmt.Errorf("cannot get tiup work directory") - } - return os.Chdir(pwd) -}