mirror of
https://github.com/ko-build/ko.git
synced 2025-07-12 23:50:31 +02:00
Remove dependency on cli-runtime (#413)
* first pass: kubectl flags must be passed after '--' * add warning when using non-separated flags * mark flags as deprecated * drop defaultCacheDir and homedir dependency
This commit is contained in:
@ -17,19 +17,20 @@ package commands
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/google/ko/internal"
|
||||
"github.com/google/ko/pkg/commands/options"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
)
|
||||
|
||||
// addCreate augments our CLI surface with apply.
|
||||
func addCreate(topLevel *cobra.Command) {
|
||||
koCreateFlags := []string{}
|
||||
var kf internal.KubectlFlags
|
||||
po := &options.PublishOptions{}
|
||||
fo := &options.FilenameOptions{}
|
||||
so := &options.SelectorOptions{}
|
||||
@ -60,8 +61,11 @@ func addCreate(topLevel *cobra.Command) {
|
||||
ko create --local -f config/
|
||||
|
||||
# Create from stdin:
|
||||
cat config.yaml | ko create -f -`,
|
||||
Args: cobra.NoArgs,
|
||||
cat config.yaml | ko create -f -
|
||||
|
||||
# Any flags passed after '--' are passed to 'kubectl apply' directly:
|
||||
ko apply -f config -- --namespace=foo --kubeconfig=cfg.yaml
|
||||
`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if !isKubectlAvailable() {
|
||||
return errors.New("error: kubectl is not available. kubectl must be installed to use ko create")
|
||||
@ -80,25 +84,19 @@ func addCreate(topLevel *cobra.Command) {
|
||||
return fmt.Errorf("error creating publisher: %v", err)
|
||||
}
|
||||
defer publisher.Close()
|
||||
// Create a set of ko-specific flags to ignore when passing through
|
||||
// kubectl global flags.
|
||||
ignoreSet := make(map[string]struct{})
|
||||
for _, s := range koCreateFlags {
|
||||
ignoreSet[s] = struct{}{}
|
||||
}
|
||||
|
||||
// Filter out ko flags from what we will pass through to kubectl.
|
||||
kubectlFlags := []string{}
|
||||
cmd.Flags().Visit(func(flag *pflag.Flag) {
|
||||
if _, ok := ignoreSet[flag.Name]; !ok {
|
||||
kubectlFlags = append(kubectlFlags, "--"+flag.Name, flag.Value.String())
|
||||
}
|
||||
})
|
||||
|
||||
// Issue a "kubectl create" command reading from stdin,
|
||||
// to which we will pipe the resolved files.
|
||||
argv := []string{"create", "-f", "-"}
|
||||
argv = append(argv, kubectlFlags...)
|
||||
// to which we will pipe the resolved files, and any
|
||||
// remaining flags passed after '--'.
|
||||
argv := []string{"apply", "-f", "-"}
|
||||
if kflags := kf.Values(); len(kflags) != 0 {
|
||||
skflags := strings.Join(kflags, " ")
|
||||
log.Printf(kubectlFlagsWarningTemplate,
|
||||
"create", skflags,
|
||||
"create", skflags)
|
||||
argv = append(argv, kflags...)
|
||||
}
|
||||
argv = append(argv, args...)
|
||||
kubectlCmd := exec.CommandContext(ctx, "kubectl", argv...)
|
||||
|
||||
// Pass through our environment
|
||||
@ -145,17 +143,7 @@ func addCreate(topLevel *cobra.Command) {
|
||||
options.AddFileArg(create, fo)
|
||||
options.AddSelectorArg(create, so)
|
||||
options.AddBuildOptions(create, bo)
|
||||
|
||||
// Collect the ko-specific apply flags before registering the kubectl global
|
||||
// flags so that we can ignore them when passing kubectl global flags through
|
||||
// to kubectl.
|
||||
create.Flags().VisitAll(func(flag *pflag.Flag) {
|
||||
koCreateFlags = append(koCreateFlags, flag.Name)
|
||||
})
|
||||
|
||||
// Register the kubectl global flags.
|
||||
kubeConfigFlags := genericclioptions.NewConfigFlags(false)
|
||||
kubeConfigFlags.AddFlags(create.Flags())
|
||||
internal.AddFlags(&kf, create.Flags())
|
||||
|
||||
topLevel.AddCommand(create)
|
||||
}
|
||||
|
Reference in New Issue
Block a user