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 20894493ce2156951d51769412b3448272f56f30 Author: Doru Bercea <gheorghe-teod.ber...@ibm.com> AuthorDate: Wed Nov 18 18:04:08 2020 -0500 Support CLI properties for kamel local run. --- deploy/resources.go | 4 +-- pkg/cmd/local_run.go | 15 ++++++---- pkg/cmd/local_run_test.go | 20 +++++++++++++ pkg/cmd/run.go | 13 ++------- pkg/cmd/util_commands.go | 2 +- pkg/cmd/util_dependencies.go | 69 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 105 insertions(+), 18 deletions(-) diff --git a/deploy/resources.go b/deploy/resources.go index f432da1..a76d437 100644 --- a/deploy/resources.go +++ b/deploy/resources.go @@ -340,9 +340,9 @@ var assets = func() http.FileSystem { "/traits.yaml": &vfsgen۰CompressedFileInfo{ name: "traits.yaml", modTime: time.Time{}, - uncompressedSize: 32828, + uncompressedSize: 33738, - compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x7d\x6d\x73\x1b\x37\x92\xf0\xf7\xfc\x0a\x94\x9e\xa7\x4a\xa2\x8a\x1c\xc9\xd9\xca\x26\xab\xbb\x5c\x4a\xeb\x38\xbb\x72\x62\x5b\x67\x39\xc9\x5d\xe5\xb6\x96\xe0\x4c\x93\x84\x85\x01\x66\x01\x0c\x65\xe6\xea\xfe\xfb\x55\x37\x5e\x06\x33\x1c\x49\x94\x63\xa5\xb4\x55\x57\xf9\x10\x93\x9a\x69\x34\x1a\xfd\xfe\x02\x3a\xc3\x85\xb3\x67\x9f\xcd\x98\xe2\x35\x9c\x31\xbe\x5c\x0a\x25\xdc\xf6\x33\xc6\x1a\xc9\xdd\x52\x9b\xfa\x8c\x2d\xb9\x [...] + compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x7d\x6d\x6f\xdc\xb6\xd6\xe0\xf7\xfe\x0a\xc2\xbb\x80\x3d\xc6\x48\x76\x7a\xd1\xdb\x5e\xef\x76\x0b\x37\x49\xef\x75\xda\x24\xde\x38\x6d\x77\xd1\xbd\xb8\xc3\x91\xce\xcc\x30\xa6\x48\x3d\x24\x35\xce\x74\xb1\xff\x7d\xc1\xc3\x57\x69\x64\x5b\x4e\xe3\xc2\x17\x78\x90\x0f\xb1\xc7\x12\x79\x78\x78\xde\xdf\xc6\x28\xca\x8c\x3e\xfb\xa2\x20\x82\x36\x70\x46\xe8\x6a\xc5\x04\x33\xbb\x2f\x08\x69\x39\x35\x2b\xa9\x9a\x33\xb2\xa2\x5c\x83\x [...] }, "/user-cluster-role.yaml": &vfsgen۰CompressedFileInfo{ name: "user-cluster-role.yaml", diff --git a/pkg/cmd/local_run.go b/pkg/cmd/local_run.go index f2b86f4..4f45493 100644 --- a/pkg/cmd/local_run.go +++ b/pkg/cmd/local_run.go @@ -19,7 +19,6 @@ package cmd import ( "fmt" - "strings" "github.com/spf13/cobra" ) @@ -56,9 +55,8 @@ func newCmdLocalRun(rootCmdOptions *RootCmdOptions) (*cobra.Command, *localRunCm } cmd.Flags().StringArray("property-file", nil, "Add a property file to the integration.") - cmd.Flags().StringArrayP("dependency", "d", nil, `Additional top-level dependency with the format: -<type>:<dependency-name> -where <type> is one of {`+strings.Join(acceptedDependencyTypes, "|")+`}.`) + cmd.Flags().StringArrayP("property", "p", nil, "Add a camel property.") + cmd.Flags().StringArrayP("dependency", "d", nil, additionalDependencyUsageMessage) return &cmd, &options } @@ -66,6 +64,7 @@ where <type> is one of {`+strings.Join(acceptedDependencyTypes, "|")+`}.`) type localRunCmdOptions struct { *RootCmdOptions PropertyFiles []string `mapstructure:"property-files"` + Properties []string `mapstructure:"properties"` AdditionalDependencies []string `mapstructure:"dependencies"` } @@ -77,7 +76,7 @@ func (command *localRunCmdOptions) validate(args []string) error { } // Validate properties file. - err = validateFiles(command.PropertyFiles) + err = validatePropertyFiles(command.PropertyFiles) if err != nil { return nil } @@ -96,6 +95,12 @@ func (command *localRunCmdOptions) run(args []string) error { return err } + // Manage integration properties which may come from files or CLI. + err = updateIntegrationProperties(command) + if err != nil { + return nil + } + // Run the integration locally. err = RunLocalIntegration(command.PropertyFiles, dependencies, args) if err != nil { diff --git a/pkg/cmd/local_run_test.go b/pkg/cmd/local_run_test.go index 72f84f2..95f5657 100644 --- a/pkg/cmd/local_run_test.go +++ b/pkg/cmd/local_run_test.go @@ -57,6 +57,26 @@ func TestLocalRunPropertyFileFlag(t *testing.T) { } } +func TestLocalRunPropertiesFlag(t *testing.T) { + options, rootCmd := kamelTestPreAddCommandInit() + + localRunCmdOptions := addTestLocalRunCmd(options, rootCmd) + + kamelTestPostAddCommandInit(t, rootCmd) + + _, err := test.ExecuteCommand(rootCmd, "local", "run", "route.java", "-p", "prop1=value1", "-p", "prop2=value2") + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + if len(localRunCmdOptions.Properties) != 2 { + t.Fatalf("Additional dependencies expected to contain: \n %v elements\nGot:\n %v elements\n", 2, len(localRunCmdOptions.Properties)) + } + if localRunCmdOptions.Properties[0] != "prop1=value1" || localRunCmdOptions.Properties[1] != "prop2=value2" { + t.Fatalf("Additional dependencies expected to be: \n %v\nGot:\n %v\n", "[prop1=value1, prop2=value2]", localRunCmdOptions.Properties) + } +} + func TestLocalRunAdditionalDependenciesFlag(t *testing.T) { options, rootCmd := kamelTestPreAddCommandInit() diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index e484d39..5904272 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -218,16 +218,9 @@ func (o *runCmdOptions) validate() error { } } - for _, fileName := range o.PropertyFiles { - if !strings.HasSuffix(fileName, ".properties") { - return fmt.Errorf("supported property files must have a .properties extension: %s", fileName) - } - - if file, err := os.Stat(fileName); err != nil { - return errors.Wrapf(err, "unable to access property file %s", fileName) - } else if file.IsDir() { - return fmt.Errorf("property file %s is a directory", fileName) - } + err := validatePropertyFiles(o.PropertyFiles) + if err != nil { + return err } for _, label := range o.Labels { diff --git a/pkg/cmd/util_commands.go b/pkg/cmd/util_commands.go index b44243a..4819935 100644 --- a/pkg/cmd/util_commands.go +++ b/pkg/cmd/util_commands.go @@ -86,7 +86,7 @@ func RunLocalIntegration(properties []string, dependencies []string, routes []st fmt.Printf("executing: %s", strings.Join(cmd.Args, " ")) // Add directory where the properties file resides. - cmd.Env = append(cmd.Env, "CAMEL_K_CONF_D="+strings.Join(confDirectories(properties), ",")) + cmd.Env = append(cmd.Env, "CAMEL_K_CONF_D="+getPropertiesDir()) // Add files to the command line under the CAMEL_K_ROUTES flag. cmd.Env = append(cmd.Env, "CAMEL_K_ROUTES="+strings.Join(formatRoutes(routes), ",")) diff --git a/pkg/cmd/util_dependencies.go b/pkg/cmd/util_dependencies.go index 30722a7..c73f56b 100644 --- a/pkg/cmd/util_dependencies.go +++ b/pkg/cmd/util_dependencies.go @@ -305,6 +305,75 @@ func validateIntegrationForDependencies(args []string, additionalDependencies [] return nil } +func validatePropertyFiles(propertyFiles []string) error { + for _, fileName := range propertyFiles { + if !strings.HasSuffix(fileName, ".properties") { + return fmt.Errorf("supported property files must have a .properties extension: %s", fileName) + } + + if file, err := os.Stat(fileName); err != nil { + return errors.Wrapf(err, "unable to access property file %s", fileName) + } else if file.IsDir() { + return fmt.Errorf("property file %s is a directory", fileName) + } + } + + return nil +} + +func getPropertiesDir() string { + // Directory is created under the maven directory which is removed. + return path.Join(mavenWorkingDirectory, "properties") +} + +func createPropertiesDirectory() error { + // Check directory exists. + directoryExists, err := util.DirectoryExists(getPropertiesDir()) + if err != nil { + return err + } + + if !directoryExists { + err := os.MkdirAll(getPropertiesDir(), 0777) + if err != nil { + return err + } + } + return nil +} + +func updateIntegrationProperties(command *localRunCmdOptions) error { + // Create properties directory under Maven working directory. This ensures that + // property files of different integrations do not clash. + err := createPropertiesDirectory() + if err != nil { + return err + } + + // Relocate properties files to this integration's property directory. + relocatedPropertyFiles := []string{} + for _, propertyFile := range command.PropertyFiles { + relocatedPropertyFile := path.Join(getPropertiesDir(), path.Base(propertyFile)) + util.CopyFile(propertyFile, relocatedPropertyFile) + relocatedPropertyFiles = append(relocatedPropertyFiles, relocatedPropertyFile) + } + + // Output list of properties to property file if any CLI properties were given. + if len(command.Properties) > 0 { + propertyFilePath := path.Join(getPropertiesDir(), "CLI.properties") + err = ioutil.WriteFile(propertyFilePath, []byte(strings.Join(command.Properties, "\n")), 0777) + if err != nil { + return err + } + relocatedPropertyFiles = append(relocatedPropertyFiles, propertyFilePath) + } + + // Update command PropertyFiles. + command.PropertyFiles = relocatedPropertyFiles + + return nil +} + func createMavenWorkingDirectory() error { // Create local Maven context. temporaryDirectory, err := ioutil.TempDir(os.TempDir(), "maven-")