nicolaferraro commented on a change in pull request #1805: URL: https://github.com/apache/camel-k/pull/1805#discussion_r519841132
########## File path: pkg/cmd/local_run.go ########## @@ -0,0 +1,110 @@ +/* +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 ( + "fmt" + "strings" + + "github.com/spf13/cobra" +) + +func newCmdLocalRun(rootCmdOptions *RootCmdOptions) (*cobra.Command, *localRunCmdOptions) { + options := localRunCmdOptions{ + RootCmdOptions: rootCmdOptions, + } + + cmd := cobra.Command{ + Use: "local-run [files to inspect]", Review comment: Probably `kamel local run` would be better. So that `kamel local` can have other subcommands if needed in the future. ########## File path: pkg/cmd/local_run.go ########## @@ -0,0 +1,110 @@ +/* +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 ( + "fmt" + "strings" + + "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().StringArrayP("properties-file", "p", nil, "File containing integration properties.") Review comment: I'd use the same options as in the `kamel run` command. Not needed to implement all them, but they should have the same syntax. Shortcut `-p` indicates a single property `k=v` in `kamel run` and it should be the same here (with this PR or later on). ########## File path: pkg/cmd/local_run.go ########## @@ -0,0 +1,110 @@ +/* +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 ( + "fmt" + "strings" + + "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), Review comment: We should also think to load modeline options from the file as well. We can open an issue about modeline in local run. ########## File path: 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), ",")) Review comment: This can be better set on `cmd.Env`. ########## File path: 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), ",")) Review comment: Same ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
