You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2021/05/27 10:35:31 UTC
[camel-k] 03/11: feat(cmd/run): build-property file support
This is an automated email from the ASF dual-hosted git repository.
nferraro pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit d5910edf0bfc4f5a3e25d942182a22ad87e3100e
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Mon May 24 14:16:48 2021 +0200
feat(cmd/run): build-property file support
Ref #2003
---
pkg/cmd/run.go | 46 ++++++++++++++++++++++++++++++++++++++++++++--
pkg/cmd/run_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 87 insertions(+), 2 deletions(-)
diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go
index 1c341d9..fc0516e 100644
--- a/pkg/cmd/run.go
+++ b/pkg/cmd/run.go
@@ -79,7 +79,7 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) (*cobra.Command, *runCmdOptions)
cmd.Flags().BoolP("wait", "w", false, "Wait for the integration to be running")
cmd.Flags().StringP("kit", "k", "", "The kit used to run the integration")
cmd.Flags().StringArrayP("property", "p", nil, "Add a camel property")
- cmd.Flags().StringArray("build-property", nil, "Add a build time property")
+ cmd.Flags().StringArray("build-property", nil, "Add a build time property (syntax, my-key=my-value | file:/path/to/my-conf.properties)")
cmd.Flags().StringArray("configmap", nil, "Add a ConfigMap")
cmd.Flags().StringArray("secret", nil, "Add a Secret")
cmd.Flags().StringArray("maven-repository", nil, "Add a maven repository")
@@ -229,6 +229,12 @@ func (o *runCmdOptions) validate() error {
return err
}
+ buildPropertyFiles := filterBuildPropertyFiles(o.BuildProperties)
+ err = validatePropertyFiles(buildPropertyFiles)
+ if err != nil {
+ return err
+ }
+
for _, label := range o.Labels {
parts := strings.Split(label, "=")
if len(parts) != 2 {
@@ -239,6 +245,17 @@ func (o *runCmdOptions) validate() error {
return nil
}
+func filterBuildPropertyFiles(maybePropertyFiles []string) []string {
+ var propertyFiles []string
+ for _, maybePropertyFile := range maybePropertyFiles {
+ if strings.HasPrefix(maybePropertyFile, "file:") {
+ propertyFiles = append(propertyFiles, strings.Replace(maybePropertyFile, "file:", "", 1))
+ }
+ }
+
+ return propertyFiles
+}
+
func (o *runCmdOptions) run(cmd *cobra.Command, args []string) error {
c, err := o.GetCmdClient()
if err != nil {
@@ -554,7 +571,18 @@ func (o *runCmdOptions) updateIntegrationCode(c client.Client, sources []string,
}
// convert each build configuration to a builder trait property
for _, item := range o.BuildProperties {
- o.Traits = append(o.Traits, fmt.Sprintf("builder.properties=%s", item))
+ props, err := extractProperties(item)
+ if err != nil {
+ return nil, err
+ }
+ for _, k := range props.Keys() {
+ v, ok := props.Get(k)
+ if ok {
+ o.Traits = append(o.Traits, fmt.Sprintf("builder.properties=%s", escapePropertyFileItem(k)+"="+escapePropertyFileItem(v)))
+ } else {
+ return nil, err
+ }
+ }
}
for _, item := range o.LoggingLevels {
integration.Spec.AddConfiguration("property", "logging.level."+item)
@@ -642,6 +670,20 @@ func (o *runCmdOptions) updateIntegrationCode(c client.Client, sources []string,
return &integration, nil
}
+// The function parse the value and if it is a file (file:/path/), it will parse as property file
+// otherwise return a single property built from the item passed as `key=value`
+func extractProperties(value string) (*properties.Properties, error) {
+ if !strings.HasPrefix(value, "file:") {
+ return keyValueProps(value)
+ }
+ // we already validated the existence of files during validate()
+ return loadPropertyFile(strings.Replace(value, "file:", "", 1))
+}
+
+func keyValueProps(value string) (*properties.Properties, error) {
+ return properties.Load([]byte(value), properties.UTF8)
+}
+
func binaryOrTextResource(fileName string, data []byte, contentType string, base64Compression bool) (v1.ResourceSpec, error) {
resourceSpec := v1.ResourceSpec{
DataSpec: v1.DataSpec{
diff --git a/pkg/cmd/run_test.go b/pkg/cmd/run_test.go
index f11e157..7f45795 100644
--- a/pkg/cmd/run_test.go
+++ b/pkg/cmd/run_test.go
@@ -568,3 +568,46 @@ func TestIsLocalFileAndExists(t *testing.T) {
assert.NotNil(t, err)
assert.False(t, value)
}
+
+func TestExtractProperties_SingleKeyValue(t *testing.T) {
+ correctValues := []string{"key=val", "key = val", "key= val", " key = val"}
+ for _, val := range correctValues {
+ prop, err := extractProperties(val)
+ assert.Nil(t, err)
+ value, ok := prop.Get("key")
+ assert.True(t, ok)
+ assert.Equal(t, "val", value)
+ }
+}
+
+func TestExtractProperties_FromFile(t *testing.T) {
+ var tmpFile1 *os.File
+ var err error
+ if tmpFile1, err = ioutil.TempFile("", "camel-k-*.properties"); err != nil {
+ t.Error(err)
+ }
+
+ assert.Nil(t, tmpFile1.Close())
+ assert.Nil(t, ioutil.WriteFile(tmpFile1.Name(), []byte(`
+ key=value
+ #key2=value2
+ my.key=value
+ `), 0644))
+
+ props, err := extractProperties("file:" + tmpFile1.Name())
+ assert.Nil(t, err)
+ assert.Equal(t, 2, props.Len())
+ for _, prop := range props.Keys() {
+ value, ok := props.Get(prop)
+ assert.True(t, ok)
+ assert.Equal(t, "value", value)
+ }
+}
+
+func TestFilterBuildPropertyFiles(t *testing.T) {
+ inputValues := []string{"file:/tmp/test", "key=val"}
+ outputValues := filterBuildPropertyFiles(inputValues)
+
+ assert.Equal(t, len(outputValues), 1)
+ assert.Equal(t, outputValues[0], "/tmp/test")
+}