forked from pingcap/tiup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
155 lines (135 loc) · 5.02 KB
/
help.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// 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"
"os/exec"
"path/filepath"
"strings"
"github.com/pingcap/tiup/pkg/environment"
"github.com/pingcap/tiup/pkg/localdata"
"github.com/pingcap/tiup/pkg/utils"
"github.com/spf13/cobra"
)
func newHelpCmd() *cobra.Command {
return &cobra.Command{
Use: "help [command]",
Short: "Help about any command or component",
Long: `Help provides help for any command or component in the application.
Simply type tiup help <command>|<component> for full details.`,
Run: func(cmd *cobra.Command, args []string) {
env := environment.GlobalEnv()
cmd, n, e := cmd.Root().Find(args)
if (cmd == rootCmd || e != nil) && len(n) > 0 {
externalHelp(env, n[0], n[1:]...)
} else {
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
cmd.HelpFunc()(cmd, args)
}
},
}
}
func externalHelp(env *environment.Environment, spec string, args ...string) {
profile := env.Profile()
component, version := environment.ParseCompVersion(spec)
selectVer, err := env.SelectInstalledVersion(component, version)
if err != nil {
fmt.Println(err)
return
}
binaryPath, err := env.BinaryPath(component, selectVer)
if err != nil {
fmt.Println(err)
return
}
installPath, err := profile.ComponentInstalledPath(component, selectVer)
if err != nil {
fmt.Println(err)
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),
}
envs = append(envs, os.Environ()...)
comp := exec.Command(binaryPath, utils.RebuildArgs(args)...)
comp.Env = envs
comp.Stdout = os.Stdout
comp.Stderr = os.Stderr
if err := comp.Start(); err != nil {
fmt.Printf("Cannot fetch help message from %s failed: %v\n", binaryPath, err)
return
}
if err := comp.Wait(); err != nil {
fmt.Printf("Cannot fetch help message from %s failed: %v\n", binaryPath, err)
}
}
// nolint unused
func rebuildArgs(args []string) []string {
helpFlag := "--help"
argList := []string{}
for _, arg := range args {
if arg == "-h" || arg == "--help" {
helpFlag = arg
} else {
argList = append(argList, arg)
}
}
argList = append(argList, helpFlag)
return argList
}
func usageTemplate(profile *localdata.Profile) string {
installComps := `
Components Manifest:
use "tiup list" to fetch the latest components manifest
`
return `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
{{if not .HasParent}}` + installComps + `{{end}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if not .HasParent}}
Component instances with the same "tag" will share a data directory ($TIUP_HOME/data/$tag):
$ tiup --tag mycluster playground
Examples:
$ tiup playground # Quick start
$ tiup playground nightly # Start a playground with the latest nightly version
$ tiup install <component>[:version] # Install a component of specific version
$ tiup update --all # Update all installed components to the latest version
$ tiup update --nightly # Update all installed components to the nightly version
$ tiup update --self # Update the "tiup" to the latest version
$ tiup list # Fetch the latest supported components list
$ tiup status # Display all running/terminated instances
$ tiup clean <name> # Clean the data of running/terminated instance (Kill process if it's running)
$ tiup clean --all # Clean the data of all running/terminated instances{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`
}