This is an automated email from the ASF dual-hosted git repository. tsato pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 70b488f91803244855bc7c423cdeda4e1ebb41eb Author: Tadayoshi Sato <[email protected]> AuthorDate: Fri Jul 29 15:05:01 2022 +0900 chore(cli): further refactor kamel local cmds --- pkg/cmd/local_run.go | 2 +- pkg/cmd/local_util_container.go | 103 +++++++++++++++++++++++++++++- pkg/cmd/util_commands.go | 134 ---------------------------------------- 3 files changed, 103 insertions(+), 136 deletions(-) diff --git a/pkg/cmd/local_run.go b/pkg/cmd/local_run.go index d424a8e8c..74027a11c 100644 --- a/pkg/cmd/local_run.go +++ b/pkg/cmd/local_run.go @@ -179,7 +179,7 @@ func (o *localRunCmdOptions) run(cmd *cobra.Command, args []string) error { } // Run integration locally. - return RunLocalIntegrationRunCommand(o.Context, propertyFiles, dependencies, routes, o.getPropertiesDir(), + return runLocalIntegration(o.Context, propertyFiles, dependencies, routes, o.getPropertiesDir(), cmd.OutOrStdout(), cmd.ErrOrStderr()) } diff --git a/pkg/cmd/local_util_container.go b/pkg/cmd/local_util_container.go index a10c49025..109ca030d 100644 --- a/pkg/cmd/local_util_container.go +++ b/pkg/cmd/local_util_container.go @@ -180,7 +180,7 @@ func createAndBuildIntegrationImage(ctx context.Context, containerRegistry strin // Get integration run command to be run inside the container. This means the command // has to be created with the paths which will be valid inside the container. - containerCmd, err := GetContainerIntegrationRunCommand(ctx, propertyFiles, dependencies, routes, stdout, stderr) + containerCmd, err := getContainerIntegrationRunCommand(ctx, propertyFiles, dependencies, routes, stdout, stderr) if err != nil { return err } @@ -245,3 +245,104 @@ func runIntegrationImage(ctx context.Context, image string, stdout, stderr io.Wr return nil } + +// getContainerIntegrationRunCommand returns the integration command which will be run inside the container. +func getContainerIntegrationRunCommand(ctx context.Context, properties []string, dependencies []string, routes []string, + stdout, stderr io.Writer) (*exec.Cmd, error) { + // All paths need to be valid container paths. + // Update property file paths. + containerProperties := docker.ContainerizeFilePaths(properties, docker.GetContainerPropertiesDir()) + containerDependencies := docker.ContainerizeDependencyPaths(dependencies, docker.GetContainerDependenciesDir()) + containerRoutes := docker.ContainerizeFilePaths(routes, docker.GetContainerRoutesDir()) + + return assembleIntegrationRunCommand(ctx, containerProperties, containerDependencies, containerRoutes, + docker.GetContainerPropertiesDir(), stdout, stderr, false) +} + +func runLocalIntegration(ctx context.Context, properties []string, dependencies []string, routes []string, + propertiesDir string, stdout, stderr io.Writer) error { + cmd, err := assembleIntegrationRunCommand(ctx, properties, dependencies, routes, + propertiesDir, stdout, stderr, true) + if err != nil { + return err + } + + // Output command we are about to run. + fmt.Fprintln(cmd.Stdout, "Executing:", strings.Join(cmd.Args, " ")) + + // Run integration locally. + return cmd.Run() +} + +func assembleIntegrationRunCommand(ctx context.Context, properties []string, dependencies []string, routes []string, + propertiesDir string, stdout, stderr io.Writer, local bool) (*exec.Cmd, error) { + // Create classpath value. + classpathValue := assembleClasspathArgValue(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, "io.quarkus.bootstrap.runner.QuarkusEntryPoint") + + cmd := exec.CommandContext(ctx, javaCmd, args...) + + // Add directory where the properties files reside. The directory is the local properties directory + // or the properties directory inside the container. + cmd.Env = append(cmd.Env, "CAMEL_K_CONF_D="+propertiesDir) + + // Add files to the command line under the CAMEL_K_ROUTES flag. + cmd.Env = append(cmd.Env, "CAMEL_K_ROUTES="+strings.Join(formatRoutes(routes), ",")) + + // Add any lazily evaluated environment variables. + if local { + // If we are running locally then this is as late as we can evaluate the + // lazy environment variables since we are going to run the command + // immediately after the generation of these arguments. + setEnvVars, err := util.EvaluateCLIAndLazyEnvVars() + if err != nil { + return nil, err + } + cmd.Env = append(cmd.Env, setEnvVars...) + } else { + // If we are running in containerized or just building an image, we should + // not evaluate the variables at this point since we are only generating the + // run command and not actually running it. + for _, lazyEnvVar := range util.ListOfLazyEvaluatedEnvVars { + cmd.Env = append(cmd.Env, lazyEnvVar+"={{env:"+lazyEnvVar+"}}") + } + } + + // Set stdout and stderr. + cmd.Stderr = stderr + cmd.Stdout = stdout + + return cmd, nil +} + +func assembleClasspathArgValue(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, string(os.PathListSeparator)) +} + +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 with extension. + routes = append(routes, "file:"+route+"?language="+extension) + } + + return routes +} diff --git a/pkg/cmd/util_commands.go b/pkg/cmd/util_commands.go deleted file mode 100644 index 7f0252a3f..000000000 --- a/pkg/cmd/util_commands.go +++ /dev/null @@ -1,134 +0,0 @@ -/* -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" - "io" - "os" - "os/exec" - "strings" - - "github.com/apache/camel-k/pkg/util" - "github.com/apache/camel-k/pkg/util/docker" -) - -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 with extension. - routes = append(routes, "file:"+route+"?language="+extension) - } - - return routes -} - -func assembleClasspathArgValue(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, string(os.PathListSeparator)) -} - -func assembleIntegrationRunCommand(ctx context.Context, properties []string, dependencies []string, routes []string, propertiesDir string, stdout, stderr io.Writer, local bool) (*exec.Cmd, error) { - // Create classpath value. - classpathValue := assembleClasspathArgValue(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, "io.quarkus.bootstrap.runner.QuarkusEntryPoint") - - cmd := exec.CommandContext(ctx, javaCmd, args...) - - // Add directory where the properties files reside. The directory is the local properties directory - // or the properties directory inside the container. - cmd.Env = append(cmd.Env, "CAMEL_K_CONF_D="+propertiesDir) - - // Add files to the command line under the CAMEL_K_ROUTES flag. - cmd.Env = append(cmd.Env, "CAMEL_K_ROUTES="+strings.Join(formatRoutes(routes), ",")) - - // Add any lazily evaluated environment variables. - if local { - // If we are running locally then this is as late as we can evaluate the - // lazy environment variables since we are going to run the command - // immediately after the generation of these arguments. - setEnvVars, err := util.EvaluateCLIAndLazyEnvVars() - if err != nil { - return nil, err - } - cmd.Env = append(cmd.Env, setEnvVars...) - } else { - // If we are running in containerized or just building an image, we should - // not evaluate the variables at this point since we are only generating the - // run command and not actually running it. - for _, lazyEnvVar := range util.ListOfLazyEvaluatedEnvVars { - cmd.Env = append(cmd.Env, lazyEnvVar+"={{env:"+lazyEnvVar+"}}") - } - } - - // Set stdout and stderr. - cmd.Stderr = stderr - cmd.Stdout = stdout - - return cmd, nil -} - -// RunLocalIntegrationRunCommand --. -func RunLocalIntegrationRunCommand(ctx context.Context, properties []string, dependencies []string, routes []string, propertiesDir string, stdout, stderr io.Writer) error { - cmd, err := assembleIntegrationRunCommand(ctx, properties, dependencies, routes, propertiesDir, stdout, stderr, true) - if err != nil { - return err - } - - // Output command we are about to run. - fmt.Fprintln(cmd.Stdout, "Executing:", strings.Join(cmd.Args, " ")) - - // Run integration locally. - err = cmd.Run() - if err != nil { - return err - } - - return nil -} - -// GetContainerIntegrationRunCommand --. -func GetContainerIntegrationRunCommand(ctx context.Context, properties []string, dependencies []string, routes []string, stdout, stderr io.Writer) (*exec.Cmd, error) { - // This is the integration command which will be run inside the container. Therefore all paths need to - // be valid container paths. - - // Update property file paths. - containerProperties := docker.ContainerizeFilePaths(properties, docker.GetContainerPropertiesDir()) - containerDependencies := docker.ContainerizeDependencyPaths(dependencies, docker.GetContainerDependenciesDir()) - containerRoutes := docker.ContainerizeFilePaths(routes, docker.GetContainerRoutesDir()) - - return assembleIntegrationRunCommand(ctx, containerProperties, containerDependencies, containerRoutes, docker.GetContainerPropertiesDir(), stdout, stderr, false) -}
