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 2019/12/18 10:30:10 UTC

[camel-k] 10/10: Fixed a reference to command options not being propagated correctly.

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 f217ed0d857ac9ea0d911b4a6eb179719606f8df
Author: Andrea Tarocchi <an...@gmail.com>
AuthorDate: Wed Dec 18 10:15:08 2019 +0100

    Fixed a reference to command options not being propagated correctly.
---
 pkg/cmd/root.go      | 44 +++++++++++++++++++++-----------------------
 pkg/cmd/root_test.go | 24 +++++++++++++-----------
 pkg/cmd/run_test.go  |  6 +++---
 3 files changed, 37 insertions(+), 37 deletions(-)

diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go
index c1ed396..8f1450c 100644
--- a/pkg/cmd/root.go
+++ b/pkg/cmd/root.go
@@ -48,14 +48,14 @@ func NewKamelCommand(ctx context.Context) (*cobra.Command, error) {
 	}
 
 	var err error
-	cmd := kamelPreAddCommandInit(options)
-	cmd = addKamelSubcommands(*cmd, options)
-	cmd, err = kamelPostAddCommandInit(*cmd)
+	cmd := kamelPreAddCommandInit(&options)
+	addKamelSubcommands(cmd, &options)
+	err = kamelPostAddCommandInit(cmd)
 
 	return cmd, err
 }
 
-func kamelPreAddCommandInit(options RootCmdOptions) *cobra.Command {
+func kamelPreAddCommandInit(options *RootCmdOptions) *cobra.Command {
 
 	var cmd = cobra.Command{
 		BashCompletionFunction: bashCompletionFunction,
@@ -71,9 +71,9 @@ func kamelPreAddCommandInit(options RootCmdOptions) *cobra.Command {
 	return &cmd
 }
 
-func kamelPostAddCommandInit(cmd cobra.Command) (*cobra.Command, error) {
-	if err := bindPFlagsHierarchy(&cmd); err != nil {
-		return nil, err
+func kamelPostAddCommandInit(cmd *cobra.Command) error {
+	if err := bindPFlagsHierarchy(cmd); err != nil {
+		return err
 	}
 
 	configName := os.Getenv("KAMEL_CONFIG_NAME")
@@ -92,29 +92,27 @@ func kamelPostAddCommandInit(cmd cobra.Command) (*cobra.Command, error) {
 
 	if err := viper.ReadInConfig(); err != nil {
 		if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
-			return nil, err
+			return err
 		}
 	}
 
-	return &cmd, nil
+	return nil
 }
 
-func addKamelSubcommands(cmd cobra.Command, options RootCmdOptions) *cobra.Command {
-	cmd.AddCommand(newCmdCompletion(&cmd))
+func addKamelSubcommands(cmd *cobra.Command, options *RootCmdOptions) {
+	cmd.AddCommand(newCmdCompletion(cmd))
 	cmd.AddCommand(newCmdVersion())
-	cmd.AddCommand(cmdOnly(newCmdRun(&options)))
-	cmd.AddCommand(cmdOnly(newCmdGet(&options)))
-	cmd.AddCommand(cmdOnly(newCmdDelete(&options)))
-	cmd.AddCommand(cmdOnly(newCmdInstall(&options)))
-	cmd.AddCommand(cmdOnly(newCmdLog(&options)))
-	cmd.AddCommand(newCmdKit(&options))
-	cmd.AddCommand(cmdOnly(newCmdReset(&options)))
-	cmd.AddCommand(newCmdDescribe(&options))
-	cmd.AddCommand(cmdOnly(newCmdRebuild(&options)))
+	cmd.AddCommand(cmdOnly(newCmdRun(options)))
+	cmd.AddCommand(cmdOnly(newCmdGet(options)))
+	cmd.AddCommand(cmdOnly(newCmdDelete(options)))
+	cmd.AddCommand(cmdOnly(newCmdInstall(options)))
+	cmd.AddCommand(cmdOnly(newCmdLog(options)))
+	cmd.AddCommand(newCmdKit(options))
+	cmd.AddCommand(cmdOnly(newCmdReset(options)))
+	cmd.AddCommand(newCmdDescribe(options))
+	cmd.AddCommand(cmdOnly(newCmdRebuild(options)))
 	cmd.AddCommand(newCmdOperator())
-	cmd.AddCommand(cmdOnly(newCmdBuilder(&options)))
-
-	return &cmd
+	cmd.AddCommand(cmdOnly(newCmdBuilder(options)))
 }
 
 func (command *RootCmdOptions) preRun(cmd *cobra.Command, _ []string) error {
diff --git a/pkg/cmd/root_test.go b/pkg/cmd/root_test.go
index 1bf886b..a050a5f 100644
--- a/pkg/cmd/root_test.go
+++ b/pkg/cmd/root_test.go
@@ -28,20 +28,22 @@ import (
 	"github.com/spf13/viper"
 )
 
-func kamelTestPostAddCommandInit(rootCmd *cobra.Command) *cobra.Command {
-	rootCmd, _ = kamelPostAddCommandInit(*rootCmd)
-	return rootCmd
+func kamelTestPostAddCommandInit(t *testing.T, rootCmd *cobra.Command) {
+	err := kamelPostAddCommandInit(rootCmd)
+	if err != nil {
+		t.Fatalf("Unexpected error: %v", err)
+	}
 }
 
-func kamelTestPreAddCommandInit() (RootCmdOptions, *cobra.Command) {
+func kamelTestPreAddCommandInit() (*RootCmdOptions, *cobra.Command) {
 	fakeClient, _ := test.NewFakeClient()
 	options := RootCmdOptions{
 		Context: context.Background(),
 		_client: fakeClient,
 	}
-	rootCmd := kamelPreAddCommandInit(options)
+	rootCmd := kamelPreAddCommandInit(&options)
 	rootCmd.Run = test.EmptyRun
-	return options, rootCmd
+	return &options, rootCmd
 }
 
 func TestLoadFromCommandLine(t *testing.T) {
@@ -49,7 +51,7 @@ func TestLoadFromCommandLine(t *testing.T) {
 
 	runCmdOptions := addTestRunCmd(options, rootCmd)
 
-	rootCmd = kamelTestPostAddCommandInit(rootCmd)
+	kamelTestPostAddCommandInit(t, rootCmd)
 
 	const VAR2 = "VAR2=value2"
 	_, err := test.ExecuteCommand(rootCmd, "run", "route.java", "--env", "VAR1=value,othervalue", "--env", VAR2)
@@ -73,7 +75,7 @@ func TestLoadFromEnvVar(t *testing.T) {
 
 	runCmdOptions := addTestRunCmd(options, rootCmd)
 
-	rootCmd = kamelTestPostAddCommandInit(rootCmd)
+	kamelTestPostAddCommandInit(t, rootCmd)
 
 	_, err := test.ExecuteCommand(rootCmd, "run", "route.java")
 	if err != nil {
@@ -97,7 +99,7 @@ func TestLoadFromFile(t *testing.T) {
 
 	runCmdOptions := addTestRunCmd(options, rootCmd)
 
-	rootCmd = kamelTestPostAddCommandInit(rootCmd)
+	kamelTestPostAddCommandInit(t, rootCmd)
 
 	_, err := test.ExecuteCommand(rootCmd, "run", "route.java")
 	if err != nil {
@@ -121,7 +123,7 @@ func TestPrecedenceEnvVarOverFile(t *testing.T) {
 
 	runCmdOptions := addTestRunCmd(options, rootCmd)
 
-	rootCmd = kamelTestPostAddCommandInit(rootCmd)
+	kamelTestPostAddCommandInit(t, rootCmd)
 
 	_, err := test.ExecuteCommand(rootCmd, "run", "route.java")
 	if err != nil {
@@ -145,7 +147,7 @@ func TestPrecedenceCommandLineOverEverythingElse(t *testing.T) {
 
 	runCmdOptions := addTestRunCmd(options, rootCmd)
 
-	rootCmd = kamelTestPostAddCommandInit(rootCmd)
+	kamelTestPostAddCommandInit(t, rootCmd)
 
 	_, err := test.ExecuteCommand(rootCmd, "run", "route.java", "--env", "VAR3=commandLine")
 	if err != nil {
diff --git a/pkg/cmd/run_test.go b/pkg/cmd/run_test.go
index f33fbee..1fcc673 100644
--- a/pkg/cmd/run_test.go
+++ b/pkg/cmd/run_test.go
@@ -24,9 +24,9 @@ import (
 	"github.com/spf13/cobra"
 )
 
-func addTestRunCmd(options RootCmdOptions, rootCmd *cobra.Command) *runCmdOptions {
+func addTestRunCmd(options *RootCmdOptions, rootCmd *cobra.Command) *runCmdOptions {
 	//add a testing version of run Command
-	runCmd, runCmdOptions := newCmdRun(&options)
+	runCmd, runCmdOptions := newCmdRun(options)
 	runCmd.RunE = func(c *cobra.Command, args []string) error {
 		return nil
 	}
@@ -40,7 +40,7 @@ func TestRunPropertyFlag(t *testing.T) {
 
 	runCmdOptions := addTestRunCmd(options, rootCmd)
 
-	rootCmd = kamelTestPostAddCommandInit(rootCmd)
+	kamelTestPostAddCommandInit(t, rootCmd)
 
 	_, err := test.ExecuteCommand(rootCmd, "run", "route.java", "--property", "key1=value,othervalue", "--property", "key2=value2")
 	if err != nil {