Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions cmd/artifact_specifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright The Microcks Authors.
*
* Licensed 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 (
"strconv"
"strings"
)

// parseImportURLSpecifier parses an import-url argument of the form:
// <url>[:<mainArtifactBool>[:<secret>]]
//
// It is intentionally parsed from the right side so that normal URLs containing
// additional ':' characters (scheme, ports, etc.) are preserved unchanged unless
// they end with the supported suffixes.
func parseImportURLSpecifier(spec string) (url string, mainArtifact bool, secret string) {
mainArtifact = true

lastColon := strings.LastIndex(spec, ":")
if lastColon == -1 {
return spec, mainArtifact, ""
}

tail := spec[lastColon+1:]
if b, err := strconv.ParseBool(tail); err == nil {
return spec[:lastColon], b, ""
}

// Might be <url>:<bool>:<secret> — only treat it as such if the second-to-last
// segment parses as bool.
secretCandidate := tail
rest := spec[:lastColon]
secondColon := strings.LastIndex(rest, ":")
if secondColon == -1 {
return spec, mainArtifact, ""
}
boolCandidate := rest[secondColon+1:]
if b, err := strconv.ParseBool(boolCandidate); err == nil {
return rest[:secondColon], b, secretCandidate
}

return spec, mainArtifact, ""
}

// parseImportFileSpecifier parses an import argument of the form:
// <path>[:<mainArtifactBool>]
//
// Like parseImportURLSpecifier, it parses from the right to avoid breaking
// paths that may contain ':' characters.
func parseImportFileSpecifier(spec string) (path string, mainArtifact bool) {
mainArtifact = true

lastColon := strings.LastIndex(spec, ":")
if lastColon == -1 {
return spec, mainArtifact
}

tail := spec[lastColon+1:]
if b, err := strconv.ParseBool(tail); err == nil {
return spec[:lastColon], b
}

return spec, mainArtifact
}

101 changes: 101 additions & 0 deletions cmd/artifact_specifier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package cmd

import "testing"

func TestParseImportURLSpecifier_PreservesPortAndPathWithMainArtifactSuffix(t *testing.T) {
in := "http://localhost:8080/spec.yaml:true"
url, main, secret := parseImportURLSpecifier(in)

if url != "http://localhost:8080/spec.yaml" {
t.Fatalf("url mismatch: got %q", url)
}
if main != true {
t.Fatalf("mainArtifact mismatch: got %v", main)
}
if secret != "" {
t.Fatalf("secret mismatch: got %q", secret)
}
}

func TestParseImportURLSpecifier_PreservesQueryAndFragment(t *testing.T) {
in := "https://example.com:8443/spec.yaml?x=1#frag:false"
url, main, secret := parseImportURLSpecifier(in)

if url != "https://example.com:8443/spec.yaml?x=1#frag" {
t.Fatalf("url mismatch: got %q", url)
}
if main != false {
t.Fatalf("mainArtifact mismatch: got %v", main)
}
if secret != "" {
t.Fatalf("secret mismatch: got %q", secret)
}
}

func TestParseImportURLSpecifier_WithSecretSuffix(t *testing.T) {
in := "http://localhost:8080/spec.yaml:true:mySecret"
url, main, secret := parseImportURLSpecifier(in)

if url != "http://localhost:8080/spec.yaml" {
t.Fatalf("url mismatch: got %q", url)
}
if main != true {
t.Fatalf("mainArtifact mismatch: got %v", main)
}
if secret != "mySecret" {
t.Fatalf("secret mismatch: got %q", secret)
}
}

func TestParseImportURLSpecifier_NoSuffixes_Unchanged(t *testing.T) {
in := "http://localhost:8080/spec.yaml"
url, main, secret := parseImportURLSpecifier(in)

if url != in {
t.Fatalf("url mismatch: got %q", url)
}
if main != true {
t.Fatalf("mainArtifact mismatch: got %v", main)
}
if secret != "" {
t.Fatalf("secret mismatch: got %q", secret)
}
}

func TestParseImportURLSpecifier_PortOnly_NoSuffixes_Unchanged(t *testing.T) {
in := "http://localhost:8080/spec.yaml:1234"
url, main, secret := parseImportURLSpecifier(in)

if url != in {
t.Fatalf("url mismatch: got %q", url)
}
if main != true {
t.Fatalf("mainArtifact mismatch: got %v", main)
}
if secret != "" {
t.Fatalf("secret mismatch: got %q", secret)
}
}

func TestParseImportFileSpecifier_SuffixBool(t *testing.T) {
in := "./specs/openapi.yaml:false"
path, main := parseImportFileSpecifier(in)
if path != "./specs/openapi.yaml" {
t.Fatalf("path mismatch: got %q", path)
}
if main != false {
t.Fatalf("mainArtifact mismatch: got %v", main)
}
}

func TestParseImportFileSpecifier_NoSuffix_Unchanged(t *testing.T) {
in := "./specs/openapi.yaml"
path, main := parseImportFileSpecifier(in)
if path != in {
t.Fatalf("path mismatch: got %q", path)
}
if main != true {
t.Fatalf("mainArtifact mismatch: got %v", main)
}
}

3 changes: 3 additions & 0 deletions cmd/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/microcks/microcks-cli/pkg/config"
Expand Down Expand Up @@ -39,6 +40,8 @@ users:
const testConfigFilePath = "./testdata/local.config"

func TestDeleteContext(t *testing.T) {
require.NoError(t, os.MkdirAll(filepath.Dir(testConfigFilePath), 0o755))

//write the test config file
err := os.WriteFile(testConfigFilePath, []byte(testConfig), os.ModePerm)
require.NoError(t, err)
Expand Down
22 changes: 4 additions & 18 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd
import (
"fmt"
"os"
"strconv"
"strings"

"github.com/microcks/microcks-cli/pkg/config"
Expand Down Expand Up @@ -118,21 +117,10 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command
// Handle multiple specification files separated by comma.
sepSpecificationFiles := strings.Split(specificationFiles, ",")
for _, f := range sepSpecificationFiles {
mainArtifact := true
var err error

// Check if mainArtifact flag is provided.
if strings.Contains(f, ":") {
pathAndMainArtifact := strings.Split(f, ":")
f = pathAndMainArtifact[0]
mainArtifact, err = strconv.ParseBool(pathAndMainArtifact[1])
if err != nil {
fmt.Printf("Cannot parse '%s' as Bool, default to true\n", pathAndMainArtifact[1])
}
}
path, mainArtifact := parseImportFileSpecifier(f)

// Try uploading this artifact.
msg, err := mc.UploadArtifact(f, mainArtifact)
msg, err := mc.UploadArtifact(path, mainArtifact)
if err != nil {
fmt.Printf("Got error when invoking Microcks client importing Artifact: %s", err)
os.Exit(1)
Expand All @@ -155,13 +143,11 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command
}

// Normalize file path to match the watcher fsnotify events format.
if strings.HasPrefix(f, "./") {
f = strings.TrimPrefix(f, "./")
}
path = strings.TrimPrefix(path, "./")

// Upsert entry.
watchCfg.UpsertEntry(config.WatchEntry{
FilePath: f,
FilePath: path,
Context: []string{globalClientOpts.Context},
MainArtifact: mainArtifact,
})
Expand Down
23 changes: 2 additions & 21 deletions cmd/importURL.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd
import (
"fmt"
"os"
"strconv"
"strings"

"github.com/microcks/microcks-cli/pkg/config"
Expand Down Expand Up @@ -97,28 +96,10 @@ func NewImportURLCommand(globalClientOpts *connectors.ClientOptions) *cobra.Comm
}
sepSpecificationFiles := strings.Split(specificationFiles, ",")
for _, f := range sepSpecificationFiles {
mainArtifact := true
secret := ""

// Check if URL starts with https or http
if strings.HasPrefix(f, "https://") || strings.HasPrefix(f, "http://") {
urlAndMainAtrifactAndSecretName := strings.Split(f, ":")
n := len(urlAndMainAtrifactAndSecretName)
f = urlAndMainAtrifactAndSecretName[0] + ":" + urlAndMainAtrifactAndSecretName[1]
if n > 2 {
val, err := strconv.ParseBool(urlAndMainAtrifactAndSecretName[2])
if err != nil {
fmt.Println(err)
}
mainArtifact = val
}
if n > 3 {
secret = urlAndMainAtrifactAndSecretName[3]
}
}
artifactURL, mainArtifact, secret := parseImportURLSpecifier(f)

// Try downloading the artifcat
msg, err := mc.DownloadArtifact(f, mainArtifact, secret)
msg, err := mc.DownloadArtifact(artifactURL, mainArtifact, secret)
if err != nil {
fmt.Printf("Got error when invoking Microcks client importing Artifact: %s", err)
os.Exit(1)
Expand Down
14 changes: 14 additions & 0 deletions documentation/cmd/importURL.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ Imports API specification files (OpenAPI, AsyncAPI, etc.) hosted at a remote URL
microcks import-url <specURL1[:main][:secret]>,<specURL2[:main][:secret]> [flags]
```

### URL suffix parsing
You can optionally append metadata suffixes to each URL:

- `:<main>` where `<main>` is `true` or `false`
- `:<main>:<secret>` to additionally specify a secret name

The CLI parses these suffixes from the **rightmost** `:` characters only, so normal URLs containing `:` (scheme, ports, etc.) are preserved.

### Example
```bash
# Import a single artifact (marked as main)
Expand All @@ -14,6 +22,12 @@ microcks import-url https://example.com/openapi.yaml
# Specify mainArtifact flag for each file
microcks import-url https://example.com/spec1.yaml:true,https://example.com/spec2.yaml:false

# URL with port + :main suffix (port/path are preserved)
microcks import-url http://localhost:8080/spec.yaml:true

# URL with port + :main + :secret
microcks import-url http://localhost:8080/spec.yaml:true:mySecret

# Import specification to microcks without logining to microcks
microcks import-url https://example.com/openapi.yaml \
--microcksURL <microcks-url> \
Expand Down