forked from kubernetes-sigs/cli-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (66 loc) · 2.36 KB
/
main.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
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"flag"
"os"
"strings"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/cmd/apply"
"sigs.k8s.io/cli-utils/cmd/destroy"
"sigs.k8s.io/cli-utils/cmd/diff"
"sigs.k8s.io/cli-utils/cmd/initcmd"
"sigs.k8s.io/cli-utils/cmd/preview"
"sigs.k8s.io/cli-utils/cmd/status"
// This is here rather than in the libraries because of
// https://github.com/kubernetes-sigs/kustomize/issues/2060
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
var cmd = &cobra.Command{
Use: "kapply",
Short: "Perform cluster operations using declarative configuration",
Long: "Perform cluster operations using declarative configuration",
}
func main() {
// configure kubectl dependencies and flags
flags := cmd.Flags()
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
kubeConfigFlags.AddFlags(flags)
matchVersionKubeConfigFlags := util.NewMatchVersionFlags(kubeConfigFlags)
matchVersionKubeConfigFlags.AddFlags(cmd.PersistentFlags())
cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
f := util.NewFactory(matchVersionKubeConfigFlags)
ioStreams := genericclioptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr,
}
names := []string{"init", "apply", "preview", "diff", "destroy", "status"}
initCmd := initcmd.NewCmdInit(ioStreams)
updateHelp(names, initCmd)
applyCmd := apply.ApplyCommand(f, ioStreams)
updateHelp(names, applyCmd)
previewCmd := preview.NewCmdPreview(f, ioStreams)
updateHelp(names, previewCmd)
diffCmd := diff.NewCmdDiff(f, ioStreams)
updateHelp(names, diffCmd)
destroyCmd := destroy.NewCmdDestroy(f, ioStreams)
updateHelp(names, destroyCmd)
statusCmd := status.StatusCommand()
updateHelp(names, statusCmd)
cmd.AddCommand(initCmd, applyCmd, diffCmd, destroyCmd, previewCmd, statusCmd)
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
// updateHelp replaces `kubectl` help messaging with `kapply` help messaging
func updateHelp(names []string, c *cobra.Command) {
for i := range names {
name := names[i]
c.Short = strings.ReplaceAll(c.Short, "kubectl "+name, "kapply "+name)
c.Long = strings.ReplaceAll(c.Long, "kubectl "+name, "kapply "+name)
c.Example = strings.ReplaceAll(c.Example, "kubectl "+name, "kapply "+name)
}
}