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/07/02 10:18:35 UTC
[camel-k] branch main updated: fix(cmd/run): windows scheme 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
The following commit(s) were added to refs/heads/main by this push:
new 19516e0 fix(cmd/run): windows scheme support
19516e0 is described below
commit 19516e0ebf75ecfb9b33edf5cc07f8755f7b3b02
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Thu Jul 1 15:04:55 2021 +0200
fix(cmd/run): windows scheme support
Introduced a check to verify if the source matches with a supported scheme.
Closes #2475
---
pkg/cmd/run.go | 12 ---------
pkg/cmd/run_test.go | 7 ------
pkg/cmd/util.go | 35 ++++++++++++++++++++++++++
pkg/cmd/util_sources.go | 8 +++---
pkg/cmd/util_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 104 insertions(+), 23 deletions(-)
diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go
index 43164c3..b97dca7 100644
--- a/pkg/cmd/run.go
+++ b/pkg/cmd/run.go
@@ -728,18 +728,6 @@ func (o *runCmdOptions) configureTraits(integration *v1.Integration, options []s
return nil
}
-func isLocalAndFileExists(fileName string) (bool, error) {
- info, err := os.Stat(fileName)
- if err != nil {
- if os.IsNotExist(err) {
- return false, nil
- }
- // If it is a different error (ie, permission denied) we should report it back
- return false, errors.Wrap(err, fmt.Sprintf("file system error while looking for %s", fileName))
- }
- return !info.IsDir(), nil
-}
-
func addIntegrationProperties(props *properties.Properties, spec *v1.IntegrationSpec) error {
for _, k := range props.Keys() {
v, _ := props.Get(k)
diff --git a/pkg/cmd/run_test.go b/pkg/cmd/run_test.go
index bae4c9d..00ca7fb 100644
--- a/pkg/cmd/run_test.go
+++ b/pkg/cmd/run_test.go
@@ -565,13 +565,6 @@ func TestResolveJsonPodTemplate(t *testing.T) {
assert.Equal(t, 2, len(integrationSpec.PodTemplate.Spec.Containers))
}
-func TestIsLocalFileAndExists(t *testing.T) {
- value, err := isLocalAndFileExists("/root/test")
- // must not panic because a permission error
- 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 {
diff --git a/pkg/cmd/util.go b/pkg/cmd/util.go
index b287529..7cc40bf 100644
--- a/pkg/cmd/util.go
+++ b/pkg/cmd/util.go
@@ -23,10 +23,12 @@ import (
"encoding/json"
"fmt"
"log"
+ "os"
"reflect"
"strings"
"github.com/apache/camel-k/pkg/util/gzip"
+ "github.com/pkg/errors"
"github.com/mitchellh/mapstructure"
@@ -42,6 +44,12 @@ import (
const (
offlineCommandLabel = "camel.apache.org/cmd.offline"
+
+ // Supported source schemes
+ gistScheme = "gist"
+ githubScheme = "github"
+ httpScheme = "http"
+ httpsScheme = "https"
)
// DeleteIntegration --
@@ -248,3 +256,30 @@ func compressToString(content []byte) (string, error) {
return string(bytes), nil
}
+
+func isLocalAndFileExists(uri string) (bool, error) {
+ if hasSupportedScheme(uri) {
+ // it's not a local file as it matches one of the supporting schemes
+ return false, nil
+ }
+ info, err := os.Stat(uri)
+ if err != nil {
+ if os.IsNotExist(err) {
+ return false, nil
+ }
+ // If it is a different error (ie, permission denied) we should report it back
+ return false, errors.Wrap(err, fmt.Sprintf("file system error while looking for %s", uri))
+ }
+ return !info.IsDir(), nil
+}
+
+func hasSupportedScheme(uri string) bool {
+ if strings.HasPrefix(strings.ToLower(uri), gistScheme+":") ||
+ strings.HasPrefix(strings.ToLower(uri), githubScheme+":") ||
+ strings.HasPrefix(strings.ToLower(uri), httpScheme+":") ||
+ strings.HasPrefix(strings.ToLower(uri), httpsScheme+":") {
+ return true
+ }
+
+ return false
+}
diff --git a/pkg/cmd/util_sources.go b/pkg/cmd/util_sources.go
index 6368d05..79e085a 100644
--- a/pkg/cmd/util_sources.go
+++ b/pkg/cmd/util_sources.go
@@ -84,7 +84,7 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S
}
switch {
- case u.Scheme == "gist" || strings.HasPrefix(location, "https://gist.github.com/"):
+ case u.Scheme == gistScheme || strings.HasPrefix(location, "https://gist.github.com/"):
var tc *http.Client
if token, ok := os.LookupEnv("GITHUB_TOKEN"); ok {
@@ -133,7 +133,7 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S
}
sources = append(sources, answer)
}
- case u.Scheme == "github":
+ case u.Scheme == githubScheme:
answer := Source{
Name: path.Base(location),
Origin: location,
@@ -149,7 +149,7 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S
return sources, err
}
sources = append(sources, answer)
- case u.Scheme == "http":
+ case u.Scheme == httpScheme:
answer := Source{
Name: path.Base(location),
Origin: location,
@@ -165,7 +165,7 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S
return sources, err
}
sources = append(sources, answer)
- case u.Scheme == "https":
+ case u.Scheme == httpsScheme:
answer := Source{
Name: path.Base(location),
Origin: location,
diff --git a/pkg/cmd/util_test.go b/pkg/cmd/util_test.go
new file mode 100644
index 0000000..b47c071
--- /dev/null
+++ b/pkg/cmd/util_test.go
@@ -0,0 +1,65 @@
+/*
+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 (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestCorrectFileValuesButNotFound(t *testing.T) {
+ value1, err1 := isLocalAndFileExists("c:\\test")
+ value2, err2 := isLocalAndFileExists("path/to/file")
+
+ // they are all not found, but it must not panic
+ assert.Nil(t, err1)
+ assert.False(t, value1)
+ assert.Nil(t, err2)
+ assert.False(t, value2)
+}
+
+func TestPermissionDenied(t *testing.T) {
+ value, err := isLocalAndFileExists("/root/test")
+ // must not panic because a permission error
+ assert.NotNil(t, err)
+ assert.False(t, value)
+}
+
+func TestSupportedScheme(t *testing.T) {
+ gistValue, err1 := isLocalAndFileExists("gist:some/gist/resource")
+ githubValue, err2 := isLocalAndFileExists("github:some/github/resource")
+ httpValue, err3 := isLocalAndFileExists("http://some/http/resource")
+ httpsValue, err4 := isLocalAndFileExists("https://some/https/resource")
+
+ assert.Nil(t, err1)
+ assert.False(t, gistValue)
+ assert.Nil(t, err2)
+ assert.False(t, githubValue)
+ assert.Nil(t, err3)
+ assert.False(t, httpValue)
+ assert.Nil(t, err4)
+ assert.False(t, httpsValue)
+}
+
+func TestUnSupportedScheme(t *testing.T) {
+ value, err := isLocalAndFileExists("bad_scheme:some/bad/resource")
+ // must not report an error
+ assert.Nil(t, err)
+ assert.False(t, value)
+}