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
25 changes: 25 additions & 0 deletions cmd/importDir.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/microcks/microcks-cli/pkg/connectors"
"github.com/microcks/microcks-cli/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

// MicrocksClient interface for dependency injection
Expand Down Expand Up @@ -306,6 +307,10 @@ func detectFileType(filePath string) FileType {
fileName := strings.ToLower(filepath.Base(filePath))
ext := filepath.Ext(filePath)

if isPrimary, known := detectPrimaryFromContent(filePath); known {
return FileType{Extension: ext, IsPrimary: isPrimary}
}

// Default to primary for most files
isPrimary := true

Expand All @@ -323,3 +328,23 @@ func detectFileType(filePath string) FileType {
IsPrimary: isPrimary,
}
}

func detectPrimaryFromContent(filePath string) (isPrimary bool, known bool) {
data, err := os.ReadFile(filePath)
if err != nil {
return false, false
}
var doc map[string]interface{}
if err := yaml.Unmarshal(data, &doc); err != nil {
return false, false
}
for _, key := range []string{"openapi", "swagger", "asyncapi"} {
if _, ok := doc[key]; ok {
return true, true
}
}
if apiVersion, ok := doc["apiVersion"].(string); ok && strings.HasPrefix(apiVersion, "mocks.microcks.io/") {
return false, true
}
return false, false
}
62 changes: 62 additions & 0 deletions cmd/importDir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,68 @@ func TestDetectFileTypeWithLogic(t *testing.T) {
}
}

func TestDetectFileTypeUsesFileContent(t *testing.T) {
tests := []struct {
fileName string
content string
isPrimary bool
}{
{
fileName: "payments-examples.yaml",
content: "openapi: 3.0.2\ninfo:\n title: Payments\n",
isPrimary: true,
},
{
fileName: "user-metadata-api.json",
content: `{"openapi": "3.1.0", "info": {"title": "Users"}}`,
isPrimary: true,
},
{
fileName: "legacy-collection.json",
content: `{"swagger": "2.0", "info": {"title": "Legacy"}}`,
isPrimary: true,
},
{
fileName: "events-collection.yaml",
content: "asyncapi: '2.6.0'\ninfo:\n title: Events\n",
isPrimary: true,
},
{
fileName: "openapi-examples.yaml",
content: "apiVersion: mocks.microcks.io/v1alpha1\nkind: APIExamples\n",
isPrimary: false,
},
{
fileName: "swagger-metadata.yml",
content: "apiVersion: mocks.microcks.io/v1alpha1\nkind: APIMetadata\n",
isPrimary: false,
},
{
fileName: "orders-postman.json",
content: `{"info": {"name": "Orders", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"}}`,
isPrimary: false,
},
{
fileName: "service.yaml",
content: "apiVersion: v1\nkind: Service\n",
isPrimary: true,
},
}

dir := t.TempDir()
for _, tt := range tests {
t.Run(tt.fileName, func(t *testing.T) {
path := filepath.Join(dir, tt.fileName)
require.NoError(t, os.WriteFile(path, []byte(tt.content), 0o600))

result := detectFileType(path)

assert.Equal(t, filepath.Ext(tt.fileName), result.Extension)
assert.Equal(t, tt.isPrimary, result.IsPrimary)
})
}
}

// TestNewImportDirCommand tests command creation
func TestNewImportDirCommand(t *testing.T) {
clientOpts := &connectors.ClientOptions{}
Expand Down
Loading