/
cmd.go
137 lines (117 loc) · 4.1 KB
/
cmd.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
// This file is part of MinIO DirectPV
// Copyright (c) 2021, 2022 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"context"
"errors"
"flag"
"github.com/minio/directpv/pkg/client"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/klog/v2"
)
// Version of this plugin populated by `go build`
// e.g. $ go build -ldflags="-X main.Version=v4.0.1"
var Version string
// flags
var (
kubeconfig = ""
identity = consts.GroupName
dryRun = false
// output modes
outputMode = ""
wide = false
jsonOutput = false
yamlOutput = false
noHeaders = false
)
var (
drives, nodes, driveGlobs, nodeGlobs []string
driveSelectorValues, nodeSelectorValues []types.LabelValue
printer func(interface{}) error
)
var pluginCmd = &cobra.Command{
Use: consts.AppName,
Short: "Kubectl Plugin for managing Direct Persistent Volumes",
SilenceUsage: true,
SilenceErrors: false,
Version: Version,
PersistentPreRunE: func(c *cobra.Command, args []string) error {
client.Init()
switch outputMode {
case "":
case "wide":
wide = true
case "yaml":
yamlOutput = true
case "json":
jsonOutput = true
default:
return errors.New("output should be one of wide|json|yaml or empty")
}
printer = printYAML
if jsonOutput {
printer = printJSON
}
return nil
},
}
func init() {
if pluginCmd.Version == "" {
pluginCmd.Version = "dev"
}
viper.AutomaticEnv()
kflags := flag.NewFlagSet("klog", flag.ExitOnError)
klog.InitFlags(kflags)
// parse the go default flagset to get flags for glog and other packages in future
pluginCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
pluginCmd.PersistentFlags().AddGoFlagSet(kflags)
flag.Set("logtostderr", "true")
flag.Set("alsologtostderr", "true")
pluginCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "k", kubeconfig, "path to kubeconfig")
pluginCmd.PersistentFlags().StringVarP(&outputMode, "output", "o", outputMode,
"output format should be one of wide|json|yaml or empty")
pluginCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "", dryRun, "prints the installation yaml")
pluginCmd.PersistentFlags().BoolVarP(&noHeaders, "no-headers", "", noHeaders, "disables table headers")
pluginCmd.PersistentFlags().MarkHidden("alsologtostderr")
pluginCmd.PersistentFlags().MarkHidden("add_dir_header")
pluginCmd.PersistentFlags().MarkHidden("log_file")
pluginCmd.PersistentFlags().MarkHidden("log_file_max_size")
pluginCmd.PersistentFlags().MarkHidden("one_output")
pluginCmd.PersistentFlags().MarkHidden("skip_headers")
pluginCmd.PersistentFlags().MarkHidden("skip_log_headers")
pluginCmd.PersistentFlags().MarkHidden("v")
pluginCmd.PersistentFlags().MarkHidden("log_backtrace_at")
pluginCmd.PersistentFlags().MarkHidden("log_dir")
pluginCmd.PersistentFlags().MarkHidden("logtostderr")
pluginCmd.PersistentFlags().MarkHidden("master")
pluginCmd.PersistentFlags().MarkHidden("stderrthreshold")
pluginCmd.PersistentFlags().MarkHidden("vmodule")
// suppress the incorrect prefix in glog output
flag.CommandLine.Parse([]string{})
viper.BindPFlags(pluginCmd.PersistentFlags())
pluginCmd.AddCommand(infoCmd)
pluginCmd.AddCommand(installCmd)
pluginCmd.AddCommand(uninstallCmd)
// pluginCmd.AddCommand(drivesCmd)
// pluginCmd.AddCommand(volumesCmd)
}
// Execute executes plugin command.
func Execute(ctx context.Context) error {
return pluginCmd.ExecuteContext(ctx)
}