diff --git a/cmd/importDir.go b/cmd/importDir.go index 710897aa..6091b193 100644 --- a/cmd/importDir.go +++ b/cmd/importDir.go @@ -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 @@ -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 @@ -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 +} diff --git a/cmd/importDir_test.go b/cmd/importDir_test.go index f70df3aa..1c88f901 100644 --- a/cmd/importDir_test.go +++ b/cmd/importDir_test.go @@ -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{}