Skip to content

Commit

Permalink
remove TIUP_WORK_DIR (#1556)
Browse files Browse the repository at this point in the history
  • Loading branch information
nexustar authored Sep 15, 2021
1 parent 8c7a48e commit 2e089a2
Show file tree
Hide file tree
Showing 10 changed files with 8 additions and 54 deletions.
7 changes: 0 additions & 7 deletions cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
7 changes: 0 additions & 7 deletions components/cluster/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
7 changes: 0 additions & 7 deletions components/ctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ")
Expand Down
7 changes: 0 additions & 7 deletions components/dm/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion components/playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
5 changes: 2 additions & 3 deletions components/playground/playground_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions pkg/exec/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,13 @@ 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
}

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),
Expand Down
3 changes: 0 additions & 3 deletions pkg/localdata/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
3 changes: 2 additions & 1 deletion pkg/tui/cliutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions server/package/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"os/exec"
"runtime"

"github.com/pingcap/tiup/pkg/localdata"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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)
}

0 comments on commit 2e089a2

Please sign in to comment.