Skip to content

Commit 2b53da1

Browse files
author
darkelf21cn
authored
Merge branch 'master' into add_privilege_escalation_methods_to_envinit
2 parents 2c6aeac + 65fdf56 commit 2b53da1

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

cmd/env.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2020 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package cmd
15+
16+
import (
17+
"fmt"
18+
"os"
19+
20+
"github.com/pingcap/tiup/pkg/localdata"
21+
"github.com/spf13/cobra"
22+
)
23+
24+
var envList = []string{
25+
localdata.EnvNameHome,
26+
localdata.EnvNameTelemetryStatus,
27+
localdata.EnvNameTelemetryUUID,
28+
localdata.EnvNameSSHPassPrompt,
29+
localdata.EnvNameSSHPath,
30+
localdata.EnvNameSCPPath,
31+
}
32+
33+
func newEnvCmd() *cobra.Command {
34+
cmd := &cobra.Command{
35+
Use: "env [name1...N]",
36+
Short: "Show the list of system environment variable that related to TiUP",
37+
RunE: func(cmd *cobra.Command, args []string) error {
38+
if len(args) == 0 {
39+
showEnvList(true, envList...)
40+
return nil
41+
}
42+
showEnvList(false, args...)
43+
return nil
44+
},
45+
}
46+
47+
return cmd
48+
}
49+
50+
func showEnvList(withKey bool, names ...string) {
51+
for _, name := range names {
52+
if withKey {
53+
fmt.Printf("%s=\"%s\"\n", name, os.Getenv(name))
54+
} else {
55+
fmt.Printf("%s\n", os.Getenv(name))
56+
}
57+
}
58+
}

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ the latest stable version will be downloaded from the repository.`,
133133
newMirrorCmd(),
134134
newTelemetryCmd(),
135135
newCompletionCmd(),
136+
newEnvCmd(),
136137
)
137138

138139
originHelpFunc := rootCmd.HelpFunc()

pkg/localdata/constant.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ var DefaultTiupHome string
2121
// ProfileDirName is the name of the profile directory to be used
2222
var ProfileDirName = ".tiup"
2323

24+
// Notice: if you try to add a new env name which is notable by the user, shou should
25+
// add it to cmd/env.go:envList so that the command `tiup env` will show that env.
2426
const (
2527
// ComponentParentDir represent the parent directory of all downloaded components
2628
ComponentParentDir = "components"

tests/tiup/test_tiup.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ tiup update tidb
3333
tiup status
3434
tiup clean --all
3535
tiup help tidb
36+
tiup env
37+
TIUP_SSHPASS_PROMPT="password" tiup env TIUP_SSHPASS_PROMPT | grep password
3638
tiup uninstall
3739
tiup uninstall tidb:v3.0.13
3840
tiup uninstall tidb --all

0 commit comments

Comments
 (0)