Restructure the cobra commands

This commit is contained in:
Morten Torkildsen 2020-02-04 10:10:24 -08:00
parent 34cab2ddb3
commit b3466789ef
6 changed files with 76 additions and 77 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@ main
cli-utils
dyctl
kapply
bin
#intellij
.idea/

View File

@ -32,3 +32,6 @@ test:
vet:
go vet ./...
build:
go build -o bin/kapply sigs.k8s.io/cli-utils/cmd

View File

@ -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,

12
cmd/diff/cmddiff.go Normal file
View File

@ -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)
}

View File

@ -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)
}
}

58
cmd/main.go Normal file
View File

@ -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)
}
}