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 5e827721eb9e166fa92c511988549f7a43e3e955
Author: Doru Bercea <[email protected]>
AuthorDate: Tue Nov 3 10:29:00 2020 -0500

    Add local run command.
---
 pkg/cmd/inspect.go   |   2 +-
 pkg/cmd/local_run.go | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++
 pkg/cmd/root.go      |   1 +
 3 files changed, 133 insertions(+), 1 deletion(-)

diff --git a/pkg/cmd/inspect.go b/pkg/cmd/inspect.go
index 1c2cbf4..02eed21 100644
--- a/pkg/cmd/inspect.go
+++ b/pkg/cmd/inspect.go
@@ -18,7 +18,6 @@ limitations under the License.
 package cmd
 
 import (
-       "errors"
        "fmt"
        "io/ioutil"
        "os"
@@ -32,6 +31,7 @@ import (
        "github.com/apache/camel-k/pkg/util/camel"
        "github.com/apache/camel-k/pkg/util/defaults"
        "github.com/apache/camel-k/pkg/util/maven"
+       "github.com/pkg/errors"
        "github.com/scylladb/go-set/strset"
        "github.com/spf13/cobra"
 )
diff --git a/pkg/cmd/local_run.go b/pkg/cmd/local_run.go
new file mode 100644
index 0000000..70af154
--- /dev/null
+++ b/pkg/cmd/local_run.go
@@ -0,0 +1,131 @@
+/*
+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 (
+       "errors"
+       "fmt"
+       "path"
+
+       v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+       "github.com/apache/camel-k/pkg/trait"
+       "github.com/apache/camel-k/pkg/util"
+       "github.com/apache/camel-k/pkg/util/camel"
+       "github.com/scylladb/go-set/strset"
+       "github.com/spf13/cobra"
+)
+
+func newCmdLocalRun(rootCmdOptions *RootCmdOptions) (*cobra.Command, 
*localRunCmdOptions) {
+       options := localRunCmdOptions{
+               RootCmdOptions: rootCmdOptions,
+       }
+
+       cmd := cobra.Command{
+               Use:     "local-run [files to inspect]",
+               Short:   "Run a Camel integration locally.",
+               Long:    `Run a Camel integration locally using existing 
integration files.`,
+               PreRunE: decode(&options),
+               RunE: func(_ *cobra.Command, args []string) error {
+                       if err := options.validate(args); err != nil {
+                               return err
+                       }
+                       if err := options.run(args); err != nil {
+                               fmt.Println(err.Error())
+                       }
+
+                       return nil
+               },
+               Annotations: map[string]string{
+                       offlineCommandLabel: "true",
+               },
+       }
+
+       cmd.Flags().StringP("properties", "p", "", "Output format. One of: 
json|yaml")
+
+       return &cmd, &options
+}
+
+type localRunCmdOptions struct {
+       *RootCmdOptions
+       Properties string `mapstructure:"properties"`
+}
+
+func (command *localRunCmdOptions) validate(args []string) error {
+       // If no source files have been provided there is nothing to inspect.
+       if len(args) == 0 {
+               return errors.New("no integration files have been provided, 
nothing to inspect")
+       }
+
+       // Ensure source files exist.
+       for _, arg := range args {
+               // fmt.Printf("Validating file: %v\n", arg)
+               fileExists, err := util.FileExists(arg)
+
+               // Report any error.
+               if err != nil {
+                       return err
+               }
+
+               // Signal file not found.
+               if !fileExists {
+                       return errors.New("input file " + arg + " file does not 
exist")
+               }
+       }
+
+       return nil
+}
+
+func (command *localRunCmdOptions) run(args []string) error {
+       // Attempt to reuse existing Camel catalog if one is present.
+       catalog, err := camel.MainCatalog()
+       if err != nil {
+               return err
+       }
+
+       // Generate catalog if one was not found.
+       if catalog == nil {
+               catalog, err = generateCatalog()
+               if err != nil {
+                       return err
+               }
+       }
+
+       // List of top-level dependencies.
+       dependencies := strset.New()
+
+       // Invoke the dependency inspector code for each source file.
+       for _, source := range args {
+               data, _, err := loadContent(source, false, false)
+               if err != nil {
+                       return err
+               }
+
+               sourceSpec := v1.SourceSpec{
+                       DataSpec: v1.DataSpec{
+                               Name:        path.Base(source),
+                               Content:     data,
+                               Compression: false,
+                       },
+               }
+
+               // Extract list of top-level dependencies.
+               dependencies.Merge(trait.AddSourceDependencies(sourceSpec, 
catalog))
+       }
+
+       return nil
+}
diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go
index 2899a30..eba43c2 100644
--- a/pkg/cmd/root.go
+++ b/pkg/cmd/root.go
@@ -139,6 +139,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)))
 }
 
 func addHelpSubCommands(cmd *cobra.Command, options *RootCmdOptions) error {

Reply via email to