mirror of https://github.com/fluxcd/cli-utils.git
Restructure the cobra commands
This commit is contained in:
parent
34cab2ddb3
commit
b3466789ef
|
@ -3,6 +3,7 @@ main
|
|||
cli-utils
|
||||
dyctl
|
||||
kapply
|
||||
bin
|
||||
|
||||
#intellij
|
||||
.idea/
|
||||
|
|
3
Makefile
3
Makefile
|
@ -32,3 +32,6 @@ test:
|
|||
|
||||
vet:
|
||||
go vet ./...
|
||||
|
||||
build:
|
||||
go build -o bin/kapply sigs.k8s.io/cli-utils/cmd
|
||||
|
|
|
@ -1,74 +1,18 @@
|
|||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package common
|
||||
package apply
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/kubectl/pkg/cmd/diff"
|
||||
"k8s.io/kubectl/pkg/cmd/util"
|
||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||
"k8s.io/kubectl/pkg/util/i18n"
|
||||
"sigs.k8s.io/cli-utils/pkg/apply"
|
||||
)
|
||||
|
||||
// NewKapplyCommand returns a command from kubectl to install
|
||||
func NewKapplyCommand(parent *cobra.Command) *cobra.Command {
|
||||
r := &cobra.Command{
|
||||
Use: "kapply",
|
||||
Short: "Perform cluster operations using declarative configuration",
|
||||
Long: "Perform cluster operations using declarative configuration",
|
||||
}
|
||||
|
||||
// configure kubectl dependencies and flags
|
||||
flags := r.Flags()
|
||||
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
|
||||
kubeConfigFlags.AddFlags(flags)
|
||||
matchVersionKubeConfigFlags := util.NewMatchVersionFlags(kubeConfigFlags)
|
||||
matchVersionKubeConfigFlags.AddFlags(r.PersistentFlags())
|
||||
r.PersistentFlags().AddGoFlagSet(flag.CommandLine)
|
||||
f := util.NewFactory(matchVersionKubeConfigFlags)
|
||||
|
||||
var ioStreams genericclioptions.IOStreams
|
||||
|
||||
if parent != nil {
|
||||
ioStreams.In = parent.InOrStdin()
|
||||
ioStreams.Out = parent.OutOrStdout()
|
||||
ioStreams.ErrOut = parent.ErrOrStderr()
|
||||
} else {
|
||||
ioStreams.In = os.Stdin
|
||||
ioStreams.Out = os.Stdout
|
||||
ioStreams.ErrOut = os.Stderr
|
||||
}
|
||||
|
||||
names := []string{"apply", "diff"}
|
||||
applyCmd := NewCmdApply("kapply", f, ioStreams)
|
||||
updateHelp(names, applyCmd)
|
||||
diffCmd := diff.NewCmdDiff(f, ioStreams)
|
||||
updateHelp(names, diffCmd)
|
||||
|
||||
r.AddCommand(applyCmd, diffCmd)
|
||||
return r
|
||||
}
|
||||
|
||||
// updateHelp replaces `kubectl` help messaging with `kustomize` 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)
|
||||
}
|
||||
}
|
||||
|
||||
// NewCmdApply creates the `apply` command
|
||||
func NewCmdApply(baseName string, f util.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
||||
func NewCmdApply(f util.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
||||
applier := apply.NewApplier(f, ioStreams)
|
||||
printer := &apply.BasicPrinter{
|
||||
IOStreams: ioStreams,
|
|
@ -0,0 +1,12 @@
|
|||
package diff
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/kubectl/pkg/cmd/diff"
|
||||
"k8s.io/kubectl/pkg/cmd/util"
|
||||
)
|
||||
|
||||
func NewCmdDiff(f util.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
||||
return diff.NewCmdDiff(f, ioStreams)
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
// 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"
|
||||
"sigs.k8s.io/cli-utils/cmd/common"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := common.NewKapplyCommand(nil).Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
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/diff"
|
||||
)
|
||||
|
||||
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{"apply", "diff"}
|
||||
applyCmd := apply.NewCmdApply(f, ioStreams)
|
||||
updateHelp(names, applyCmd)
|
||||
diffCmd := diff.NewCmdDiff(f, ioStreams)
|
||||
updateHelp(names, diffCmd)
|
||||
|
||||
cmd.AddCommand(applyCmd, diffCmd)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue