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 7d291c4989badcaf5309d4355993198e493c3df1
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Tue Jun 1 11:42:26 2021 +0200

    fix(cmd): modeline relative file format
    
    Introduced a new list of flags that accept multiple values. If they are 
passed a file: syntax, the value will be expanded with the relative file 
location
---
 pkg/cmd/modeline.go      | 31 ++++++++++++++++++++++++++++++-
 pkg/cmd/modeline_test.go | 35 +++++++++++++++++++++++++++++------
 2 files changed, 59 insertions(+), 7 deletions(-)

diff --git a/pkg/cmd/modeline.go b/pkg/cmd/modeline.go
index f04a741..0f540f8 100644
--- a/pkg/cmd/modeline.go
+++ b/pkg/cmd/modeline.go
@@ -53,10 +53,17 @@ var (
        // file options must be considered relative to the source files they 
belong to
        fileOptions = map[string]bool{
                "resource":      true,
-               "kube-client":   true,
+               "kube-config":   true,
                "open-api":      true,
                "property-file": true,
        }
+
+       // file format options are those options that admit multiple values, 
not only files (ie, key=value|configmap|secret|file syntax)
+       fileFormatOptions = map[string]bool{
+               "config":         true,
+               "property":       true,
+               "build-property": true,
+       }
 )
 
 // NewKamelWithModelineCommand ---
@@ -227,12 +234,34 @@ func extractModelineOptionsFromSource(resolvedSource 
Source) ([]modeline.Option,
                                o.Value = full
                                ops[i] = o
                        }
+               } else if fileFormatOptions[o.Name] && resolvedSource.Local {
+                       baseDir := filepath.Dir(resolvedSource.Origin)
+                       refPath := getRefPathOrProperty(o.Value)
+                       if !filepath.IsAbs(refPath) {
+                               full := getFullPathOrProperty(o.Value, 
path.Join(baseDir, refPath))
+                               o.Value = full
+                               ops[i] = o
+                       }
                }
        }
 
        return ops, nil
 }
 
+func getRefPathOrProperty(pathOrProperty string) string {
+       if strings.HasPrefix(pathOrProperty, "file:") {
+               return strings.Replace(pathOrProperty, "file:", "", 1)
+       }
+       return pathOrProperty
+}
+
+func getFullPathOrProperty(pathOrProperty string, fullPath string) string {
+       if strings.HasPrefix(pathOrProperty, "file:") {
+               return fmt.Sprintf("file:%s", fullPath)
+       }
+       return pathOrProperty
+}
+
 func expandModelineEnvVarOptions(ops []modeline.Option) ([]modeline.Option, 
error) {
        // List of additional command line options with expanded values for 
variables
        // marked as immediate environment variables.
diff --git a/pkg/cmd/modeline_test.go b/pkg/cmd/modeline_test.go
index 8dc2c75..6592939 100644
--- a/pkg/cmd/modeline_test.go
+++ b/pkg/cmd/modeline_test.go
@@ -19,6 +19,7 @@ package cmd
 
 import (
        "context"
+       "fmt"
        "io/ioutil"
        "os"
        "path"
@@ -142,14 +143,14 @@ func TestModelineRunPropertyFiles(t *testing.T) {
        propFile := `
                a=b
        `
-       propFileName := path.Join(dir, "application.properties")
+       propFileName := path.Join(subDir, "application.properties")
        err = ioutil.WriteFile(propFileName, []byte(propFile), 0777)
        assert.NoError(t, err)
 
        cmd, flags, err := NewKamelWithModelineCommand(context.TODO(), 
[]string{"kamel", "run", fileName})
        assert.NoError(t, err)
        assert.NotNil(t, cmd)
-       assert.Equal(t, []string{"run", fileName, 
"--property=file:application.properties"}, flags)
+       assert.Equal(t, []string{"run", fileName, 
fmt.Sprintf("--property=file:%s", propFileName)}, flags)
 }
 
 func TestModelineRunBuildProperty(t *testing.T) {
@@ -193,14 +194,36 @@ func TestModelineRunBuildPropertyFiles(t *testing.T) {
        propFile := `
                a=b
        `
-       propFileName := path.Join(dir, "application.properties")
+       propFileName := path.Join(subDir, "application.properties")
        err = ioutil.WriteFile(propFileName, []byte(propFile), 0777)
        assert.NoError(t, err)
 
        cmd, flags, err := NewKamelWithModelineCommand(context.TODO(), 
[]string{"kamel", "run", fileName})
        assert.NoError(t, err)
        assert.NotNil(t, cmd)
-       assert.Equal(t, []string{"run", fileName, 
"--build-property=file:application.properties"}, flags)
+       assert.Equal(t, []string{"run", fileName, 
fmt.Sprintf("--build-property=file:%s", propFileName)}, flags)
+}
+
+func TestModelineRunConfigConfigmap(t *testing.T) {
+       dir, err := ioutil.TempDir("", "camel-k-test-")
+       assert.NoError(t, err)
+       defer os.RemoveAll(dir)
+
+       subDir := path.Join(dir, "sub")
+       err = os.Mkdir(subDir, 0777)
+       assert.NoError(t, err)
+
+       file := `
+               // camel-k: config=configmap:my-cm
+       `
+       fileName := path.Join(subDir, "simple.groovy")
+       err = ioutil.WriteFile(fileName, []byte(file), 0777)
+       assert.NoError(t, err)
+
+       cmd, flags, err := NewKamelWithModelineCommand(context.TODO(), 
[]string{"kamel", "run", fileName})
+       assert.NoError(t, err)
+       assert.NotNil(t, cmd)
+       assert.Equal(t, []string{"run", fileName, "--config=configmap:my-cm"}, 
flags)
 }
 
 func TestModelineRunConfigSecret(t *testing.T) {
@@ -244,14 +267,14 @@ func TestModelineRunConfigFile(t *testing.T) {
        propFile := `
                a=b
        `
-       propFileName := path.Join(dir, "application.properties")
+       propFileName := path.Join(subDir, "application.properties")
        err = ioutil.WriteFile(propFileName, []byte(propFile), 0777)
        assert.NoError(t, err)
 
        cmd, flags, err := NewKamelWithModelineCommand(context.TODO(), 
[]string{"kamel", "run", fileName})
        assert.NoError(t, err)
        assert.NotNil(t, cmd)
-       assert.Equal(t, []string{"run", fileName, 
"--config=file:application.properties"}, flags)
+       assert.Equal(t, []string{"run", fileName, 
fmt.Sprintf("--config=file:%s", propFileName)}, flags)
 }
 
 func TestModelineInspectSimple(t *testing.T) {

Reply via email to