You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2018/12/05 15:57:38 UTC

[camel-k] 02/02: fix: removed `cmd.ParseFlags`

This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit ee8dee2347916162cddfff7d3e22c4afe50fe560
Author: Zoran Regvart <zr...@apache.org>
AuthorDate: Wed Dec 5 15:34:31 2018 +0100

    fix: removed `cmd.ParseFlags`
---
 pkg/client/cmd/install.go | 10 ++--------
 pkg/client/cmd/root.go    | 51 ++++++++++++++++++-----------------------------
 2 files changed, 21 insertions(+), 40 deletions(-)

diff --git a/pkg/client/cmd/install.go b/pkg/client/cmd/install.go
index 98b25c1..5b86a8b 100644
--- a/pkg/client/cmd/install.go
+++ b/pkg/client/cmd/install.go
@@ -20,15 +20,13 @@ package cmd
 import (
 	"fmt"
 
-	"os"
-
 	"github.com/apache/camel-k/pkg/install"
 	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
 	k8serrors "k8s.io/apimachinery/pkg/api/errors"
 )
 
-func newCmdInstall(rootCmdOptions *RootCmdOptions) (*cobra.Command, error) {
+func newCmdInstall(rootCmdOptions *RootCmdOptions) *cobra.Command {
 	options := installCmdOptions{
 		RootCmdOptions: rootCmdOptions,
 	}
@@ -44,12 +42,8 @@ func newCmdInstall(rootCmdOptions *RootCmdOptions) (*cobra.Command, error) {
 	cmd.Flags().StringVar(&options.registry, "registry", "", "A Docker registry that can be used to publish images")
 	cmd.Flags().StringVar(&options.organization, "organization", "", "A organization on the Docker registry that can be used to publish images")
 	cmd.Flags().StringVar(&options.pushSecret, "push-secret", "", "A secret used to push images to the Docker registry")
-	err := cmd.ParseFlags(os.Args)
-	if err != nil {
-		return nil, err
-	}
 
-	return &cmd, nil
+	return &cmd
 }
 
 type installCmdOptions struct {
diff --git a/pkg/client/cmd/root.go b/pkg/client/cmd/root.go
index 5672b60..c69f063 100644
--- a/pkg/client/cmd/root.go
+++ b/pkg/client/cmd/root.go
@@ -19,7 +19,6 @@ package cmd
 
 import (
 	"context"
-	"os"
 	"time"
 
 	"github.com/operator-framework/operator-sdk/pkg/k8sclient"
@@ -52,47 +51,35 @@ func NewKamelCommand(ctx context.Context) (*cobra.Command, error) {
 		Short:                  "Kamel is a awesome client tool for running Apache Camel integrations natively on Kubernetes",
 		Long:                   kamelCommandLongDescription,
 		BashCompletionFunction: bashCompletionFunction,
+		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
+			if options.Namespace == "" {
+				current, err := kubernetes.GetClientCurrentNamespace(options.KubeConfig)
+				if err != nil {
+					return errors.Wrap(err, "cannot get current namespace")
+				}
+				err = cmd.Flag("namespace").Value.Set(current)
+				if err != nil {
+					return err
+				}
+			}
+
+			// Let's use a fast refresh period when running with the CLI
+			k8sclient.ResetCacheEvery(2 * time.Second)
+
+			// Initialize the Kubernetes client to allow using the operator-sdk
+			return kubernetes.InitKubeClient(options.KubeConfig)
+		},
 	}
 
 	cmd.PersistentFlags().StringVar(&options.KubeConfig, "config", "", "Path to the config file to use for CLI requests")
 	cmd.PersistentFlags().StringVarP(&options.Namespace, "namespace", "n", "", "Namespace to use for all operations")
 
-	// Parse the flags before setting the defaults
-	err := cmd.ParseFlags(os.Args)
-	if err != nil {
-		return nil, err
-	}
-
-	if options.Namespace == "" {
-		current, err := kubernetes.GetClientCurrentNamespace(options.KubeConfig)
-		if err != nil {
-			return nil, errors.Wrap(err, "cannot get current namespace")
-		}
-		err = cmd.Flag("namespace").Value.Set(current)
-		if err != nil {
-			return nil, err
-		}
-	}
-
-	// Let's use a fast refresh period when running with the CLI
-	k8sclient.ResetCacheEvery(2 * time.Second)
-
-	// Initialize the Kubernetes client to allow using the operator-sdk
-	err = kubernetes.InitKubeClient(options.KubeConfig)
-	if err != nil {
-		return nil, err
-	}
-
 	cmd.AddCommand(newCmdCompletion(&cmd))
 	cmd.AddCommand(newCmdVersion())
 	cmd.AddCommand(newCmdRun(&options))
 	cmd.AddCommand(newCmdGet(&options))
 	cmd.AddCommand(newCmdDelete(&options))
-	install, err := newCmdInstall(&options)
-	if err != nil {
-		return nil, err
-	}
-	cmd.AddCommand(install)
+	cmd.AddCommand(newCmdInstall(&options))
 	cmd.AddCommand(newCmdLog(&options))
 	cmd.AddCommand(newCmdContext(&options))