Skip to content

Commit

Permalink
Fix #1449: fix help command and logs
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro committed May 11, 2020
1 parent 7a9b7f8 commit d5e7502
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
15 changes: 2 additions & 13 deletions cmd/kamel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main

import (
"context"
"fmt"
"math/rand"
"os"
"time"
Expand All @@ -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)
Expand Down
22 changes: 20 additions & 2 deletions pkg/cmd/modeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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) {
Expand All @@ -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
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/cmd/modeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d5e7502

Please sign in to comment.