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 197c67f58fc63699ab2fbf3e92bde49adb78d365
Author: Doru Bercea <[email protected]>
AuthorDate: Wed Nov 4 17:50:34 2020 -0500

    Run existing integration locally.
---
 pkg/cmd/local_run.go     | 18 ++++++++-
 pkg/cmd/util_commands.go | 97 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 113 insertions(+), 2 deletions(-)

diff --git a/pkg/cmd/local_run.go b/pkg/cmd/local_run.go
index 8a0bdb2..a76daa3 100644
--- a/pkg/cmd/local_run.go
+++ b/pkg/cmd/local_run.go
@@ -49,7 +49,7 @@ func newCmdLocalRun(rootCmdOptions *RootCmdOptions) 
(*cobra.Command, *localRunCm
                },
        }
 
-       cmd.Flags().StringArrayP("properties-file", "p", nil, "File containing 
the integration properties.")
+       cmd.Flags().StringArrayP("properties-file", "p", nil, "File containing 
integration properties.")
        cmd.Flags().StringArrayP("dependency", "d", nil, `Additional top-level 
dependency with the format:
 <type>:<dependency-name>
 where <type> is one of {`+strings.Join(acceptedDependencyTypes, "|")+`}.`)
@@ -59,11 +59,19 @@ where <type> is one of 
{`+strings.Join(acceptedDependencyTypes, "|")+`}.`)
 
 type localRunCmdOptions struct {
        *RootCmdOptions
-       PropertiesFiles        []string `mapstructure:"properties"`
+       PropertiesFiles        []string `mapstructure:"properties-files"`
        AdditionalDependencies []string `mapstructure:"dependencies"`
 }
 
 func (command *localRunCmdOptions) validate(args []string) error {
+       for _, additionalDependency := range command.AdditionalDependencies {
+               fmt.Printf("Dep: %v\n", additionalDependency)
+       }
+
+       for _, prop := range command.PropertiesFiles {
+               fmt.Printf("Prop: %v\n", prop)
+       }
+
        // Validate additional dependencies specified by the user.
        err := validateIntegrationForDependencies(args, 
command.AdditionalDependencies)
        if err != nil {
@@ -92,5 +100,11 @@ func (command *localRunCmdOptions) run(args []string) error 
{
                return err
        }
 
+       // Run the integration locally.
+       err = RunLocalIntegration(command.PropertiesFiles, dependencies, args)
+       if err != nil {
+               return nil
+       }
+
        return nil
 }
diff --git a/pkg/cmd/util_commands.go b/pkg/cmd/util_commands.go
new file mode 100644
index 0000000..36ae564
--- /dev/null
+++ b/pkg/cmd/util_commands.go
@@ -0,0 +1,97 @@
+/*
+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 (
+       "context"
+       "fmt"
+       "os"
+       "os/exec"
+       "path"
+       "strings"
+)
+
+var (
+       ctx9, cancel9 = context.WithCancel(context.Background()) // preemptive: 
kill subprocess
+       ctx, cancel   = context.WithCancel(ctx9)                 // 
cooperative: wait for subprocess
+)
+
+func formatRoutes(files []string) []string {
+       routes := []string{}
+       for _, route := range files {
+               // Split route path.
+               a := strings.Split(route, ".")
+
+               // Extract extension.
+               extension := a[len(a)-1]
+
+               // Add file if extension is supported.
+               routes = append(routes, "file:"+route+"?language="+extension)
+       }
+
+       return routes
+}
+
+func confDirectories(properties []string) []string {
+       confDirs := []string{}
+
+       for _, propertiesPath := range properties {
+               confDirs = append(confDirs, path.Dir(propertiesPath))
+       }
+
+       return confDirs
+}
+
+func assembleClasspatchArgValue(properties []string, dependencies []string, 
routes []string) string {
+       classpathContents := []string{}
+       classpathContents = append(classpathContents, properties...)
+       classpathContents = append(classpathContents, routes...)
+       classpathContents = append(classpathContents, dependencies...)
+       return strings.Join(classpathContents, ":")
+}
+
+// RunLocalIntegration --
+func RunLocalIntegration(properties []string, dependencies []string, routes 
[]string) error {
+       // Create classpath value.
+       classpathValue := assembleClasspatchArgValue(properties, dependencies, 
routes)
+
+       // Create java command that runs the integration.
+       javaCmd := "java"
+
+       // Create java command arguments.
+       args := make([]string, 0)
+       args = append(args, "-cp")
+       args = append(args, classpathValue)
+       args = append(args, "org.apache.camel.k.main.Application")
+
+       cmd := exec.CommandContext(ctx, javaCmd, args...)
+       cmd.Stderr = os.Stderr
+       cmd.Stdout = os.Stdout
+       // cmd.Dir = ctx.Path
+
+       // TODO: for debug purposes, remove when done.
+       fmt.Printf("executing: %s", strings.Join(cmd.Args, " "))
+
+       // Add directory where the properties file resides.
+       os.Setenv("CAMEL_K_CONF_D", strings.Join(confDirectories(properties), 
","))
+
+       // Add files to the command line under the CAMEL_K_ROUTES flag.
+       os.Setenv("CAMEL_K_ROUTES", strings.Join(formatRoutes(routes), ","))
+
+       return cmd.Run()
+}

Reply via email to