This is an automated email from the ASF dual-hosted git repository. jpoth pushed a commit to branch release-1.9.x in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit c7bcef95a7e3f3dc717b6aa4b5b6a7f3ae50935d Author: John Poth <poth.j...@gmail.com> AuthorDate: Thu May 19 18:18:52 2022 +0200 feat(cli): Add option to specify registry url when uploading artifacts. Move options into file:// URI syntax (cherry picked from commit d545b70be9b7483716bf10c16fad3e05e9c09df7) --- e2e/registry/registry_maven_wagon_test.go | 4 +- pkg/cmd/run.go | 66 ++++++++++++++++++------------- pkg/cmd/run_test.go | 66 ------------------------------- 3 files changed, 41 insertions(+), 95 deletions(-) diff --git a/e2e/registry/registry_maven_wagon_test.go b/e2e/registry/registry_maven_wagon_test.go index 76e2bbc99..7a978d46d 100644 --- a/e2e/registry/registry_maven_wagon_test.go +++ b/e2e/registry/registry_maven_wagon_test.go @@ -89,7 +89,7 @@ func TestImageRegistryIsAMavenRepository(t *testing.T) { Expect(Kamel("run", "files/LaughingRoute.java", "--name", name, "-p", fmt.Sprintf("location=%s", customPath), - "-d", fmt.Sprintf("file://files/laugh.txt:%slaugh.txt", customPath), + "-d", fmt.Sprintf("file://files/laugh.txt?targetPath=%slaugh.txt", customPath), "-n", ns, ).Execute()).To(Succeed()) @@ -104,7 +104,7 @@ func TestImageRegistryIsAMavenRepository(t *testing.T) { Expect(Kamel("run", "files/LaughingRoute.java", "--name", name, "-p", "location=files/", - "-d", fmt.Sprintf("file://files/laughs/:files/"), + "-d", fmt.Sprintf("file://files/laughs/?targetPath=files/"), "-n", ns, ).Execute()).To(Succeed()) diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 4f2235cfb..955b4aaeb 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -34,6 +34,7 @@ import ( "io" "io/fs" "io/ioutil" + "net/url" "os" "os/signal" "path/filepath" @@ -93,7 +94,7 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) (*cobra.Command, *runCmdOptions) cmd.Flags().String("name", "", "The integration name") cmd.Flags().StringArrayP("connect", "c", nil, "A Service that the integration should bind to, specified as [[apigroup/]version:]kind:[namespace/]name") - cmd.Flags().StringArrayP("dependency", "d", nil, "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]\" for local files (experimental)") + cmd.Flags().StringArrayP("dependency", "d", nil, "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>]\" for local files (experimental)") 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 runtime property or properties file (syntax: [my-key=my-value|file:/path/to/my-conf.properties])") @@ -153,6 +154,7 @@ type runCmdOptions struct { Labels []string `mapstructure:"labels" yaml:",omitempty"` Annotations []string `mapstructure:"annotations" yaml:",omitempty"` Sources []string `mapstructure:"sources" yaml:",omitempty"` + RegistryOptions url.Values } func (o *runCmdOptions) decode(cmd *cobra.Command, args []string) error { @@ -789,9 +791,36 @@ func resolvePodTemplate(ctx context.Context, cmd *cobra.Command, templateSrc str return err } +func parseFileURI(uri string) *url.URL { + file := new(url.URL) + file.Scheme = "file" + path := strings.TrimPrefix(uri, "file://") + i := strings.IndexByte(path, '?') + if i > 0 { + file.Path = path[:i] + file.RawQuery = path[i+1:] + } else { + file.Path = path + } + return file +} + +func (o *runCmdOptions) getRegistry(platform *v1.IntegrationPlatform) string { + registry := o.RegistryOptions.Get("registry") + if registry != "" { + return registry + } + return platform.Status.Build.Registry.Address +} + +func (o *runCmdOptions) getTargetPath() string { + return o.RegistryOptions.Get("targetPath") +} + func (o *runCmdOptions) uploadFileOrDirectory(platform *v1.IntegrationPlatform, item string, integrationName string, cmd *cobra.Command, integration *v1.Integration) error { - path := strings.TrimPrefix(item, "file://") - localPath, targetPath := getPaths(path, runtimeos.GOOS, filepath.IsAbs(path)) + 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 { @@ -837,23 +866,6 @@ func (o *runCmdOptions) uploadFileOrDirectory(platform *v1.IntegrationPlatform, }) } -func getPaths(path string, os string, isAbs bool) (localPath string, targetPath string) { - localPath = path - targetPath = "" - parts := strings.Split(path, ":") - if len(parts) > 1 { - if os != "windows" || !isAbs { - localPath = parts[0] - targetPath = parts[1] - } else if isAbs && len(parts) == 3 { - // special case on Windows for absolute paths e.g C:\foo\bar\test.csv:remote/path - localPath = fmt.Sprintf("%s:%s", parts[0], parts[1]) - targetPath = parts[2] - } - } - return localPath, targetPath -} - func getMountPath(targetPath string, dirName string, path string) (string, error) { // if the target path is a file then use that as the exact mount path if filepath.Ext(targetPath) != "" { @@ -966,7 +978,7 @@ func (o *runCmdOptions) extractGav(src *zip.File, localPath string, cmd *cobra.C func (o *runCmdOptions) uploadAsMavenArtifact(dependency maven.Dependency, path string, platform *v1.IntegrationPlatform, ns string, options spectrum.Options, cmd *cobra.Command) error { artifactHTTPPath := getArtifactHTTPPath(dependency, platform, ns) - options.Target = fmt.Sprintf("%s/%s:%s", platform.Status.Build.Registry.Address, artifactHTTPPath, dependency.Version) + options.Target = fmt.Sprintf("%s/%s:%s", o.getRegistry(platform), artifactHTTPPath, dependency.Version) if runtimeos.GOOS == "windows" { // workaround for https://github.com/container-tools/spectrum/issues/8 // work with relative paths instead @@ -981,7 +993,7 @@ func (o *runCmdOptions) uploadAsMavenArtifact(dependency maven.Dependency, path return err } o.PrintfVerboseOutf(cmd, "Uploaded: %s to %s \n", path, options.Target) - return uploadChecksumFiles(path, options, platform, artifactHTTPPath, dependency) + return o.uploadChecksumFiles(path, options, platform, artifactHTTPPath, dependency) } // Deprecated: workaround for https://github.com/container-tools/spectrum/issues/8 @@ -1029,18 +1041,18 @@ func extractGavFromPom(path string, gav maven.Dependency) maven.Dependency { return gav } -func uploadChecksumFiles(path string, options spectrum.Options, platform *v1.IntegrationPlatform, artifactHTTPPath string, dependency maven.Dependency) error { +func (o *runCmdOptions) uploadChecksumFiles(path string, options spectrum.Options, platform *v1.IntegrationPlatform, artifactHTTPPath string, dependency maven.Dependency) error { return util.WithTempDir("camel-k", func(tmpDir string) error { // #nosec G401 - if err := uploadChecksumFile(md5.New(), tmpDir, "_md5", path, options, platform, artifactHTTPPath, dependency); err != nil { + if err := o.uploadChecksumFile(md5.New(), tmpDir, "_md5", path, options, platform, artifactHTTPPath, dependency); err != nil { return err } // #nosec G401 - return uploadChecksumFile(sha1.New(), tmpDir, "_sha1", path, options, platform, artifactHTTPPath, dependency) + return o.uploadChecksumFile(sha1.New(), tmpDir, "_sha1", path, options, platform, artifactHTTPPath, dependency) }) } -func uploadChecksumFile(hash hash.Hash, tmpDir string, ext string, path string, options spectrum.Options, platform *v1.IntegrationPlatform, artifactHTTPPath string, dependency maven.Dependency) error { +func (o *runCmdOptions) uploadChecksumFile(hash hash.Hash, tmpDir string, ext string, path string, options spectrum.Options, platform *v1.IntegrationPlatform, artifactHTTPPath string, dependency maven.Dependency) error { file, err := os.Open(path) if err != nil { return err @@ -1066,7 +1078,7 @@ func uploadChecksumFile(hash hash.Hash, tmpDir string, ext string, path string, if err = writeChecksumToFile(filepath, hash); err != nil { return err } - options.Target = fmt.Sprintf("%s/%s%s:%s", platform.Status.Build.Registry.Address, artifactHTTPPath, ext, dependency.Version) + options.Target = fmt.Sprintf("%s/%s%s:%s", o.getRegistry(platform), artifactHTTPPath, ext, dependency.Version) _, err = spectrum.Build(options, fmt.Sprintf("%s:.", filepath)) return err } diff --git a/pkg/cmd/run_test.go b/pkg/cmd/run_test.go index f2fe2e6b4..b865fb7ce 100644 --- a/pkg/cmd/run_test.go +++ b/pkg/cmd/run_test.go @@ -680,69 +680,3 @@ func TestMissingTrait(t *testing.T) { assert.Equal(t, "Error: bogus.fail=i-must-fail is not a valid trait property\n", output) assert.NotNil(t, err) } - -func TestGetsPaths(t *testing.T) { - tests := []struct { - path string - localPath string - remotePath string - os string - isAbs bool - }{ - { - path: "C:\\USER\\HOME\\:remote/path", - localPath: "C:\\USER\\HOME\\", - remotePath: "remote/path", - os: "windows", - isAbs: true, - }, - { - path: "src\\main\\resources:remote/path", - localPath: "src\\main\\resources", - remotePath: "remote/path", - os: "windows", - }, - { - path: "C:\\USER\\HOME\\", - localPath: "C:\\USER\\HOME\\", - remotePath: "", - os: "windows", - isAbs: true, - }, - { - path: "src\\main\\resources", - localPath: "src\\main\\resources", - remotePath: "", - os: "windows", - }, - { - path: "/home/user/name/dir:/remote/path", - localPath: "/home/user/name/dir", - remotePath: "/remote/path", - os: "linux", - isAbs: true, - }, { - path: "/home/user/name/dir", - localPath: "/home/user/name/dir", - remotePath: "", - os: "linux", - isAbs: true, - }, { - path: "src/main/resources:remote/path", - localPath: "src/main/resources", - remotePath: "remote/path", - os: "linux", - }, { - path: "src/main/resources", - localPath: "src/main/resources", - remotePath: "", - os: "linux", - }, - } - for _, test := range tests { - localPath, targetPath := getPaths(test.path, test.os, test.isAbs) - assert.Equal(t, test.localPath, localPath) - assert.Equal(t, test.remotePath, targetPath) - - } -}