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
44 changes: 44 additions & 0 deletions cmd/artifact_specifier.go

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @cotishq, as I can see, this file contains only utility function for parsing file specifier. I think we should not add this in cmd package.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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"
)

// parseImportFileSpecifier parses an import argument of the form:
//
// <path>[:<mainArtifactBool>]
//
// It parses from the right to avoid breaking paths that may contain ':'
// characters (e.g. Windows absolute paths like C:\...).
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
}
41 changes: 41 additions & 0 deletions cmd/artifact_specifier_test.go

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here as well. We can move this test file as well.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 "testing"

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)
}
}
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 @@ -126,21 +125,10 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command
sepSpecificationFiles := strings.Split(specificationFiles, ",")
results := make([]artifactImportResult, 0, len(sepSpecificationFiles))
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 {
return errors.Wrapf(errors.KindUsage, "cannot parse %q as artifact primary flag", 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 {
return err
}
Expand Down Expand Up @@ -173,13 +161,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
1 change: 1 addition & 0 deletions cmd/importURL.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package cmd

import (

"fmt"
"strconv"
"strings"
Expand Down
Loading