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 2022/09/14 07:43:57 UTC
[camel-k] branch 240/http-dependencies updated (b1b9516d5 -> d73d1fa8b)
This is an automated email from the ASF dual-hosted git repository.
nfilotto pushed a change to branch 240/http-dependencies
in repository https://gitbox.apache.org/repos/asf/camel-k.git
discard b1b9516d5 feat(cli/run): Support dependencies defined using HTTP URLs
new d73d1fa8b feat(cli/run): Support dependencies defined using HTTP URLs
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (b1b9516d5)
\
N -- N -- N refs/heads/240/http-dependencies (d73d1fa8b)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
pkg/cmd/run_help.go | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[camel-k] 01/01: feat(cli/run): Support dependencies defined using HTTP URLs
Posted by nf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
nfilotto pushed a commit to branch 240/http-dependencies
in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit d73d1fa8ba11c101862d0393affa42e8dfa779d1
Author: Nicolas Filotto <nf...@talend.com>
AuthorDate: Wed Sep 14 09:34:10 2022 +0200
feat(cli/run): Support dependencies defined using HTTP URLs
---
pkg/cmd/run.go | 23 ++++++++++++++++++-----
pkg/cmd/run_help.go | 35 +++++++++++++++++++++++++++++++++++
pkg/cmd/run_help_test.go | 38 ++++++++++++++++++++++++++++++++++++++
3 files changed, 91 insertions(+), 5 deletions(-)
diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go
index 301aa05b3..7e35af5ac 100644
--- a/pkg/cmd/run.go
+++ b/pkg/cmd/run.go
@@ -77,7 +77,7 @@ import (
"github.com/apache/camel-k/pkg/util/watch"
)
-const usageDependency = `A dependency that should be included, e.g., "-d camel:mail" for a Camel component, "-d mvn:org.my:app:1.0" for a Maven dependency or "file://localPath[?targetPath=<path>®istry=<registry_URL>&skipChecksums=<true>&skipPOM=<true>]" for local files (experimental)`
+const usageDependency = `A dependency that should be included, e.g., "-d camel:mail" for a Camel component, "-d mvn:org.my:app:1.0" for a Maven dependency, "-d http|https://my-repo/my-dependency.jar" for custom dependencies located on an http server or "file://localPath[?targetPath=<path>®istry=<registry_URL>&skipChecksums=<true>&skipPOM=<true>]" for local files (experimental)`
func newCmdRun(rootCmdOptions *RootCmdOptions) (*cobra.Command, *runCmdOptions) {
options := runCmdOptions{
@@ -799,7 +799,7 @@ func (o *runCmdOptions) applyDependencies(cmd *cobra.Command, c client.Client, i
var platform *v1.IntegrationPlatform
for _, item := range o.Dependencies {
// TODO: accept URLs
- if strings.HasPrefix(item, "file://") {
+ if strings.HasPrefix(item, "file://") || strings.HasPrefix(item, "http://") || strings.HasPrefix(item, "https://") {
if platform == nil {
var err error
platform, err = o.getPlatform(cmd, c, it)
@@ -967,9 +967,22 @@ func (o *runCmdOptions) getTargetPath() string {
}
func (o *runCmdOptions) uploadFileOrDirectory(platform *v1.IntegrationPlatform, item string, integrationName string, cmd *cobra.Command, integration *v1.Integration) error {
- uri := parseFileURI(item)
- o.RegistryOptions = uri.Query()
- localPath, targetPath := uri.Path, o.getTargetPath()
+ var targetPath, localPath string
+ if strings.HasPrefix(item, "http://") || strings.HasPrefix(item, "https://") {
+ uri, err := url.Parse(item)
+ if err != nil {
+ return err
+ } else if localPath, err = downloadDependency(*uri); err != nil {
+ return err
+ }
+ // Remove the temporary file
+ defer os.Remove(localPath)
+ } else {
+ uri := parseFileURI(item)
+ o.RegistryOptions = uri.Query()
+ localPath, targetPath = uri.Path, o.getTargetPath()
+
+ }
options := o.getSpectrumOptions(platform, cmd)
dirName, err := getDirName(localPath)
if err != nil {
diff --git a/pkg/cmd/run_help.go b/pkg/cmd/run_help.go
index 328e9be32..d2b3e271b 100644
--- a/pkg/cmd/run_help.go
+++ b/pkg/cmd/run_help.go
@@ -20,7 +20,12 @@ package cmd
import (
"context"
"fmt"
+ "io"
+ "net/http"
+ "net/url"
+ "os"
"path"
+ "path/filepath"
"reflect"
"strings"
@@ -161,3 +166,33 @@ func fromMapToProperties(data interface{}, toString func(reflect.Value) string,
}
return result, nil
}
+
+// downloadDependency downloads the file located at the given URL into a temporary folder and returns the local path to the generated temporary file.
+func downloadDependency(url url.URL) (string, error) {
+ req, err := http.NewRequest(http.MethodGet, url.String(), nil)
+ if err != nil {
+ return "", err
+ }
+ res, err := http.DefaultClient.Do(req)
+ if err != nil {
+ return "", err
+ }
+ defer res.Body.Close()
+ base := path.Base(url.Path)
+ if base == "." || base == "/" || filepath.Ext(base) == "" {
+ base = path.Base(url.String())
+ if base == "." || base == "/" {
+ base = "tmp"
+ }
+ }
+ out, err := os.CreateTemp("", fmt.Sprintf("*.%s", base))
+ if err != nil {
+ return "", err
+ }
+ defer out.Close()
+ _, err = io.Copy(out, res.Body)
+ if err != nil {
+ return "", err
+ }
+ return out.Name(), nil
+}
diff --git a/pkg/cmd/run_help_test.go b/pkg/cmd/run_help_test.go
index 175d4da93..a25b5de43 100644
--- a/pkg/cmd/run_help_test.go
+++ b/pkg/cmd/run_help_test.go
@@ -18,6 +18,9 @@ limitations under the License.
package cmd
import (
+ "net/url"
+ "os"
+ "strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -39,3 +42,38 @@ func TestFilterFileLocation(t *testing.T) {
assert.Equal(t, "app.properties", filteredOptions[1])
assert.Equal(t, "/validfile", filteredOptions[2])
}
+
+func TestDownloadDependencyWithBadURL(t *testing.T) {
+ u, _ := url.Parse("http://foo")
+ _, err := downloadDependency(*u)
+ assert.NotNil(t, err)
+}
+
+func TestDownloadDependencyWithFileNameInURL(t *testing.T) {
+ u, _ := url.Parse("https://repo1.maven.org/maven2/org/apache/camel/camel-core/3.18.2/camel-core-3.18.2.jar")
+ path, err := downloadDependency(*u)
+ t.Cleanup(func() { os.Remove(path) })
+ assert.Nil(t, err)
+ assert.True(t, strings.HasSuffix(path, "camel-core-3.18.2.jar"), "The name of the jar file is expected")
+ _, err = os.Stat(path)
+ assert.Nil(t, err)
+}
+
+func TestDownloadDependencyWithFileNameInQuery(t *testing.T) {
+ u, _ := url.Parse("https://search.maven.org/remotecontent?filepath=org/apache/camel/quarkus/camel-quarkus-file/2.12.0/camel-quarkus-file-2.12.0.jar")
+ path, err := downloadDependency(*u)
+ t.Cleanup(func() { os.Remove(path) })
+ assert.Nil(t, err)
+ assert.True(t, strings.HasSuffix(path, "camel-quarkus-file-2.12.0.jar"), "The name of the jar file is expected")
+ _, err = os.Stat(path)
+ assert.Nil(t, err)
+}
+
+func TestDownloadDependencyWithoutFileName(t *testing.T) {
+ u, _ := url.Parse("https://search.maven.org")
+ path, err := downloadDependency(*u)
+ t.Cleanup(func() { os.Remove(path) })
+ assert.Nil(t, err)
+ _, err = os.Stat(path)
+ assert.Nil(t, err)
+}