This is an automated email from the ASF dual-hosted git repository. nferraro pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 184b36fd1d186d52f97b65b81e7b9a6de84effaf Author: Doru Bercea <[email protected]> AuthorDate: Fri Nov 13 13:03:55 2020 -0500 Add local run command. Resolve all modeline options. --- pkg/cmd/local.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++ pkg/cmd/local_run.go | 2 +- pkg/cmd/local_run_test.go | 12 ++++++----- pkg/cmd/modeline.go | 12 ++++++++--- pkg/cmd/root.go | 6 +++++- pkg/cmd/util.go | 3 ++- 6 files changed, 79 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/local.go b/pkg/cmd/local.go new file mode 100644 index 0000000..89b7740 --- /dev/null +++ b/pkg/cmd/local.go @@ -0,0 +1,55 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cmd + +import ( + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +// NewCmdLocal -- Add local kamel subcommand with several other subcommands of its own. +func newCmdLocal(rootCmdOptions *RootCmdOptions) *cobra.Command { + cmd := cobra.Command{ + Use: "local [sub-command]", + Short: "Perform integration actions locally.", + Long: `Perform integration actions locally given a set of input integration files.`, + Annotations: map[string]string{ + offlineCommandLabel: "true", + }, + } + + return &cmd +} + +func addLocalSubCommands(cmd *cobra.Command, options *RootCmdOptions) error { + var localCmd *cobra.Command + for _, c := range cmd.Commands() { + if c.Name() == "local" { + localCmd = c + break + } + } + + if localCmd == nil { + return errors.New("could not find any configured local command") + } + + localCmd.AddCommand(cmdOnly(newCmdLocalRun(options))) + + return nil +} diff --git a/pkg/cmd/local_run.go b/pkg/cmd/local_run.go index 4a68877..f2b86f4 100644 --- a/pkg/cmd/local_run.go +++ b/pkg/cmd/local_run.go @@ -30,7 +30,7 @@ func newCmdLocalRun(rootCmdOptions *RootCmdOptions) (*cobra.Command, *localRunCm } cmd := cobra.Command{ - Use: "local-run [integration files]", + Use: "run [integration files]", Short: "Run integration locally.", Long: `Run integration locally using the input integration files.`, PreRunE: decode(&options), diff --git a/pkg/cmd/local_run_test.go b/pkg/cmd/local_run_test.go index 2858021..72f84f2 100644 --- a/pkg/cmd/local_run_test.go +++ b/pkg/cmd/local_run_test.go @@ -24,14 +24,16 @@ import ( "github.com/spf13/cobra" ) -func addTestLocalRunCmd(options *RootCmdOptions, rootCmd *cobra.Command) *localRunCmdOptions { +func addTestLocalRunCmd(rootCmdOptions *RootCmdOptions, rootCmd *cobra.Command) *localRunCmdOptions { //add a testing version of run Command - localRunCmd, localRunCmdOptions := newCmdLocalRun(options) + localCmd := newCmdLocal(rootCmdOptions) + localRunCmd, localRunCmdOptions := newCmdLocalRun(rootCmdOptions) localRunCmd.RunE = func(c *cobra.Command, args []string) error { return nil } localRunCmd.Args = test.ArbitraryArgs - rootCmd.AddCommand(localRunCmd) + localCmd.AddCommand(localRunCmd) + rootCmd.AddCommand(localCmd) return localRunCmdOptions } @@ -42,7 +44,7 @@ func TestLocalRunPropertyFileFlag(t *testing.T) { kamelTestPostAddCommandInit(t, rootCmd) - _, err := test.ExecuteCommand(rootCmd, "local-run", "route.java", "--property-file", "file1.properties", "--property-file", "file2.properties") + _, err := test.ExecuteCommand(rootCmd, "local", "run", "route.java", "--property-file", "file1.properties", "--property-file", "file2.properties") if err != nil { t.Fatalf("Unexpected error: %v", err) } @@ -62,7 +64,7 @@ func TestLocalRunAdditionalDependenciesFlag(t *testing.T) { kamelTestPostAddCommandInit(t, rootCmd) - _, err := test.ExecuteCommand(rootCmd, "local-run", "route.java", "-d", "mvn:camel-component-1", "-d", "mvn:camel-component-2") + _, err := test.ExecuteCommand(rootCmd, "local", "run", "route.java", "-d", "mvn:camel-component-1", "-d", "mvn:camel-component-2") if err != nil { t.Fatalf("Unexpected error: %v", err) } diff --git a/pkg/cmd/modeline.go b/pkg/cmd/modeline.go index b232110..9e3331f 100644 --- a/pkg/cmd/modeline.go +++ b/pkg/cmd/modeline.go @@ -31,6 +31,7 @@ import ( const ( runCmdName = "run" + localCmdName = "local" runCmdSourcesArgs = "source" ) @@ -98,9 +99,14 @@ func createKamelWithModelineCommand(ctx context.Context, args []string) (*cobra. fg := target.Flags() - additionalSources, err := fg.GetStringArray(runCmdSourcesArgs) - if err != nil { - return nil, nil, err + // Only the run command has source flag (for now). Remove condition when + // local run also supports source. + additionalSources := make([]string, 0) + if target.Name() == runCmdName && target.Parent().Name() != localCmdName { + additionalSources, err = fg.GetStringArray(runCmdSourcesArgs) + if err != nil { + return nil, nil, err + } } files := make([]string, 0, len(fg.Args())+len(additionalSources)) diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index eba43c2..c56d369 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -61,6 +61,10 @@ func NewKamelCommand(ctx context.Context) (*cobra.Command, error) { return cmd, err } + if err := addLocalSubCommands(cmd, &options); err != nil { + return cmd, err + } + err = kamelPostAddCommandInit(cmd) return cmd, err @@ -139,7 +143,7 @@ func addKamelSubcommands(cmd *cobra.Command, options *RootCmdOptions) { cmd.AddCommand(cmdOnly(newCmdInit(options))) cmd.AddCommand(cmdOnly(newCmdDebug(options))) cmd.AddCommand(cmdOnly(newCmdInspect(options))) - cmd.AddCommand(cmdOnly(newCmdLocalRun(options))) + cmd.AddCommand(newCmdLocal(options)) } func addHelpSubCommands(cmd *cobra.Command, options *RootCmdOptions) error { diff --git a/pkg/cmd/util.go b/pkg/cmd/util.go index 8d5ac14..3e364a6 100644 --- a/pkg/cmd/util.go +++ b/pkg/cmd/util.go @@ -24,11 +24,12 @@ import ( "encoding/csv" "encoding/json" "fmt" - "github.com/apache/camel-k/pkg/util/gzip" "log" "reflect" "strings" + "github.com/apache/camel-k/pkg/util/gzip" + "github.com/mitchellh/mapstructure" v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
