From 04aa4cdbd3cee99c90237fd8159fda8c821f006c Mon Sep 17 00:00:00 2001 From: Nicola Ferraro Date: Fri, 8 May 2020 12:40:52 +0200 Subject: [PATCH] Fix #1449: fix help command and logs --- cmd/kamel/main.go | 15 ++------------- pkg/cmd/modeline.go | 22 ++++++++++++++++++++-- pkg/cmd/modeline_test.go | 13 +++++++++++++ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/cmd/kamel/main.go b/cmd/kamel/main.go index be385b41f2..9b99fa2567 100644 --- a/cmd/kamel/main.go +++ b/cmd/kamel/main.go @@ -19,7 +19,6 @@ package main import ( "context" - "fmt" "math/rand" "os" "time" @@ -42,18 +41,8 @@ func main() { defer cancel() // Add modeline options to the command - rootCmd, args, err := cmd.NewKamelWithModelineCommand(ctx, os.Args) - if err != nil { - fmt.Printf("Error: %s\n", err.Error()) - exitOnError(err) - } - - // Give a feedback about the actual command that is run - fmt.Fprint(rootCmd.OutOrStdout(), "Executing: kamel ") - for _, a := range args { - fmt.Fprintf(rootCmd.OutOrStdout(), "%s ", a) - } - fmt.Fprintln(rootCmd.OutOrStdout()) + rootCmd, _, err := cmd.NewKamelWithModelineCommand(ctx, os.Args) + exitOnError(err) err = rootCmd.Execute() exitOnError(err) diff --git a/pkg/cmd/modeline.go b/pkg/cmd/modeline.go index 60343caf06..caa1310373 100644 --- a/pkg/cmd/modeline.go +++ b/pkg/cmd/modeline.go @@ -8,6 +8,7 @@ import ( "github.com/apache/camel-k/pkg/util/modeline" "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/spf13/pflag" "path/filepath" ) @@ -33,7 +34,22 @@ var ( func NewKamelWithModelineCommand(ctx context.Context, osArgs []string) (*cobra.Command, []string, error) { processed := make(map[string]bool) - return createKamelWithModelineCommand(ctx, osArgs[1:], processed) + originalFlags := osArgs[1:] + rootCmd, flags, err := createKamelWithModelineCommand(ctx, append([]string(nil), originalFlags...), processed) + if err != nil { + fmt.Printf("Error: %s\n", err.Error()) + return rootCmd, flags, err + } + if len(originalFlags) != len(flags) { + // Give a feedback about the actual command that is run + fmt.Fprintln(rootCmd.OutOrStdout(), "Modeline options have been loaded from source files") + fmt.Fprint(rootCmd.OutOrStdout(), "Full command: kamel ") + for _, a := range flags { + fmt.Fprintf(rootCmd.OutOrStdout(), "%s ", a) + } + fmt.Fprintln(rootCmd.OutOrStdout()) + } + return rootCmd, flags, nil } func createKamelWithModelineCommand(ctx context.Context, args []string, processedFiles map[string]bool) (*cobra.Command, []string, error) { @@ -52,7 +68,9 @@ func createKamelWithModelineCommand(ctx context.Context, args []string, processe } err = target.ParseFlags(flags) - if err != nil { + if err == pflag.ErrHelp { + return rootCmd, args, nil + } else if err != nil { return nil, nil, err } diff --git a/pkg/cmd/modeline_test.go b/pkg/cmd/modeline_test.go index 6eabd1894a..33de320d45 100644 --- a/pkg/cmd/modeline_test.go +++ b/pkg/cmd/modeline_test.go @@ -46,6 +46,19 @@ func TestModelineRunSimple(t *testing.T) { assert.Equal(t, []string{"run", fileName, "--dependency", "mvn:org.my:lib:1.0"}, flags) } +func TestModelineRunHelp(t *testing.T) { + dir, err := ioutil.TempDir("", "camel-k-test-") + assert.NoError(t, err) + defer os.RemoveAll(dir) + // no file created + fileName := path.Join(dir, "simple.groovy") + + cmd, flags, err := NewKamelWithModelineCommand(context.TODO(), []string{"kamel", "run", fileName, "--help"}) + assert.NoError(t, err) + assert.NotNil(t, cmd) + assert.Equal(t, []string{"run", fileName, "--help"}, flags) +} + func TestModelineRunChain(t *testing.T) { dir, err := ioutil.TempDir("", "camel-k-test-") assert.NoError(t, err)