/
config.go
65 lines (51 loc) · 1.99 KB
/
config.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
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/kubectl/cmd/config"
cmdconfig "github.com/openshift/origin/pkg/cmd/cli/config"
)
const (
configLong = `
Manage the client config files
The client stores configuration in the current user's home directory (under the .kube directory as
config). When you login the first time, a new config file is created, and subsequent project changes with the
'project' command will set the current context. These subcommands allow you to manage the config directly.
Reference: https://github.com/kubernetes/kubernetes/blob/master/docs/user-guide/kubeconfig-file.md`
configExample = ` # Change the config context to use
%[1]s %[2]s use-context my-context
# Set the value of a config preference
%[1]s %[2]s set preferences.some true`
)
func NewCmdConfig(parentName, name string) *cobra.Command {
pathOptions := &config.PathOptions{
GlobalFile: cmdconfig.RecommendedHomeFile,
EnvVar: cmdconfig.OpenShiftConfigPathEnvVar,
ExplicitFileFlag: cmdconfig.OpenShiftConfigFlagName,
GlobalFileSubpath: cmdconfig.OpenShiftConfigHomeDirFileName,
LoadingRules: cmdconfig.NewOpenShiftClientConfigLoadingRules(),
}
pathOptions.LoadingRules.DoNotResolvePaths = true
cmd := config.NewCmdConfig(pathOptions, os.Stdout)
cmd.Short = "Change configuration files for the client"
cmd.Long = configLong
cmd.Example = fmt.Sprintf(configExample, parentName, name)
adjustCmdExamples(cmd, parentName, name)
return cmd
}
func adjustCmdExamples(cmd *cobra.Command, parentName string, name string) {
for _, subCmd := range cmd.Commands() {
adjustCmdExamples(subCmd, parentName, cmd.Name())
}
cmd.Example = strings.Replace(cmd.Example, "$ kubectl", "$ "+parentName, -1)
tabbing := " "
examples := []string{}
scanner := bufio.NewScanner(strings.NewReader(cmd.Example))
for scanner.Scan() {
examples = append(examples, tabbing+strings.TrimSpace(scanner.Text()))
}
cmd.Example = strings.Join(examples, "\n")
}