From 65fdf56b28440506356942167b007315802a7e52 Mon Sep 17 00:00:00 2001 From: SIGSEGV Date: Mon, 21 Sep 2020 14:22:09 +0800 Subject: [PATCH] Support tiup env command (#788) Signed-off-by: lucklove --- cmd/env.go | 58 +++++++++++++++++++++++++++++++++++++++ cmd/root.go | 1 + pkg/localdata/constant.go | 2 ++ tests/tiup/test_tiup.sh | 2 ++ 4 files changed, 63 insertions(+) create mode 100644 cmd/env.go diff --git a/cmd/env.go b/cmd/env.go new file mode 100644 index 0000000000..abfe241151 --- /dev/null +++ b/cmd/env.go @@ -0,0 +1,58 @@ +// Copyright 2020 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "fmt" + "os" + + "github.com/pingcap/tiup/pkg/localdata" + "github.com/spf13/cobra" +) + +var envList = []string{ + localdata.EnvNameHome, + localdata.EnvNameTelemetryStatus, + localdata.EnvNameTelemetryUUID, + localdata.EnvNameSSHPassPrompt, + localdata.EnvNameSSHPath, + localdata.EnvNameSCPPath, +} + +func newEnvCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "env [name1...N]", + Short: "Show the list of system environment variable that related to TiUP", + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + showEnvList(true, envList...) + return nil + } + showEnvList(false, args...) + return nil + }, + } + + return cmd +} + +func showEnvList(withKey bool, names ...string) { + for _, name := range names { + if withKey { + fmt.Printf("%s=\"%s\"\n", name, os.Getenv(name)) + } else { + fmt.Printf("%s\n", os.Getenv(name)) + } + } +} diff --git a/cmd/root.go b/cmd/root.go index bbb0e6326a..68ace4e424 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -133,6 +133,7 @@ the latest stable version will be downloaded from the repository.`, newMirrorCmd(), newTelemetryCmd(), newCompletionCmd(), + newEnvCmd(), ) originHelpFunc := rootCmd.HelpFunc() diff --git a/pkg/localdata/constant.go b/pkg/localdata/constant.go index f875c2c8e3..3825841604 100644 --- a/pkg/localdata/constant.go +++ b/pkg/localdata/constant.go @@ -21,6 +21,8 @@ var DefaultTiupHome string // ProfileDirName is the name of the profile directory to be used var ProfileDirName = ".tiup" +// Notice: if you try to add a new env name which is notable by the user, shou should +// add it to cmd/env.go:envList so that the command `tiup env` will show that env. const ( // ComponentParentDir represent the parent directory of all downloaded components ComponentParentDir = "components" diff --git a/tests/tiup/test_tiup.sh b/tests/tiup/test_tiup.sh index fc0ada2f98..96bf5ce4ce 100755 --- a/tests/tiup/test_tiup.sh +++ b/tests/tiup/test_tiup.sh @@ -33,6 +33,8 @@ tiup update tidb tiup status tiup clean --all tiup help tidb +tiup env +TIUP_SSHPASS_PROMPT="password" tiup env TIUP_SSHPASS_PROMPT | grep password tiup uninstall tiup uninstall tidb:v3.0.13 tiup uninstall tidb --all