Skip to content
Merged
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
4 changes: 2 additions & 2 deletions files/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

"github.com/meshery/meshkit/errors"
coreV1 "github.com/meshery/schemas/models/core"
"github.com/meshery/schemas/models/core"
)

var (
Expand Down Expand Up @@ -175,7 +175,7 @@ func ErrFailedToExtractArchive(fileName string, err error) error {
return errors.New(ErrFailedToExtractTarCode, errors.Critical, sdescription, ldescription, probableCause, remedy)
}

func ErrFailedToIdentifyFile(fileName string, fileExt string, identificationTrace map[coreV1.IaCFileTypes]error) error {
func ErrFailedToIdentifyFile(fileName string, fileExt string, identificationTrace map[core.IaCFileTypes]error) error {

validTypes := slices.Collect(maps.Keys(identificationTrace))

Expand Down
26 changes: 13 additions & 13 deletions files/identification.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/meshery/meshkit/utils"
"github.com/meshery/meshkit/utils/kubernetes/kompose"
"github.com/meshery/meshkit/utils/walker"
coreV1 "github.com/meshery/schemas/models/core"
"github.com/meshery/schemas/models/core"
"github.com/meshery/schemas/models/v1beta1"
"github.com/meshery/schemas/models/v1beta1/pattern"

Expand All @@ -38,7 +38,7 @@ import (
)

type IdentifiedFile struct {
Type coreV1.IaCFileTypes
Type core.IaCFileTypes

// pattern.PatternFile (meshery-design),
// []runtime.Object (k8s manifest) ,
Expand All @@ -53,52 +53,52 @@ func IdentifyFile(sanitizedFile SanitizedFile) (IdentifiedFile, error) {
var parsed interface{}

// Map to store identification errors for each file type
identificationErrorsTrace := map[coreV1.IaCFileTypes]error{}
identificationErrorsTrace := map[core.IaCFileTypes]error{}

// Attempt to parse the file as a Meshery design
if parsed, err = ParseFileAsMesheryDesign(sanitizedFile); err == nil {
return IdentifiedFile{
Type: coreV1.MesheryDesign,
Type: core.MesheryDesign,
ParsedFile: parsed,
}, nil
}
identificationErrorsTrace[coreV1.MesheryDesign] = err
identificationErrorsTrace[core.MesheryDesign] = err

// Attempt to parse the file as a Kubernetes manifest
if parsed, err = ParseFileAsKubernetesManifest(sanitizedFile); err == nil {
return IdentifiedFile{
Type: coreV1.K8sManifest,
Type: core.K8sManifest,
ParsedFile: parsed,
}, nil
}
identificationErrorsTrace[coreV1.K8sManifest] = err
identificationErrorsTrace[core.K8sManifest] = err

// Attempt to parse the file as a Helm chart
if parsed, err = ParseFileAsHelmChart(sanitizedFile); err == nil {
return IdentifiedFile{
Type: coreV1.HelmChart,
Type: core.HelmChart,
ParsedFile: parsed,
}, nil
}
identificationErrorsTrace[coreV1.HelmChart] = err
identificationErrorsTrace[core.HelmChart] = err

// Attempt to parse the file as a Docker Compose file
if parsed, err = ParseFileAsDockerCompose(sanitizedFile); err == nil {
return IdentifiedFile{
Type: coreV1.DockerCompose,
Type: core.DockerCompose,
ParsedFile: parsed,
}, nil
}
identificationErrorsTrace[coreV1.DockerCompose] = err
identificationErrorsTrace[core.DockerCompose] = err

// Attempt to parse the file as a Kustomization file
if parsed, err = ParseFileAsKustomization(sanitizedFile); err == nil {
return IdentifiedFile{
Type: coreV1.K8sKustomize,
Type: core.K8sKustomize,
ParsedFile: parsed,
}, nil
}
identificationErrorsTrace[coreV1.K8sKustomize] = err
identificationErrorsTrace[core.K8sKustomize] = err

// If no file type matched, return a detailed error with the identification trace
return IdentifiedFile{}, ErrFailedToIdentifyFile(sanitizedFile.FileName, sanitizedFile.FileExt, identificationErrorsTrace)
Expand Down
22 changes: 11 additions & 11 deletions files/tests/sanitization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"archive/zip"
"github.com/meshery/meshkit/errors"
"github.com/meshery/meshkit/files"
coreV1 "github.com/meshery/schemas/models/core"
"github.com/meshery/schemas/models/core"
"github.com/stretchr/testify/assert"
)

Expand All @@ -22,7 +22,7 @@ func TestSanitizeFile(t *testing.T) {
expectError bool
expectedErrCode string
expectedContent map[string]interface{}
expectedType coreV1.IaCFileTypes
expectedType core.IaCFileTypes
}{
{
name: "Valid JSON",
Expand Down Expand Up @@ -77,59 +77,59 @@ func TestSanitizeFile(t *testing.T) {
name: "Can Identify Design",
filePath: "./samples/valid_design.yml",
expectedExt: ".yml",
expectedType: coreV1.MesheryDesign,
expectedType: core.MesheryDesign,
},

{
name: "Can Identify Designs packaged as OCI images",
filePath: "./samples/valid-design-oci.tar",
expectedExt: ".tar",
expectedType: coreV1.MesheryDesign,
expectedType: core.MesheryDesign,
},
{
name: "Can Identify Kubernetes Manifest",
filePath: "./samples/valid_manifest.yml",
expectedExt: ".yml",
expectedType: coreV1.K8sManifest,
expectedType: core.K8sManifest,
},

{
name: "Can Identify Kubernetes Manifest With Crds",
filePath: "./samples/manifest-with-crds.yml",
expectedExt: ".yml",
expectedType: coreV1.K8sManifest,
expectedType: core.K8sManifest,
},

{
name: "Can Identify HelmChart",
filePath: "./samples/valid-helm.tgz",
expectedExt: ".tgz",
expectedType: coreV1.HelmChart,
expectedType: core.HelmChart,
},
{
name: "Can Identify Kustomize archive (tar.gz)",
filePath: "./samples/wordpress-kustomize.tar.gz",
expectedExt: ".gz",
expectedType: coreV1.K8sKustomize,
expectedType: core.K8sKustomize,
},
{
name: "Can Identify Kustomize archive (zip)",
filePath: "./samples/wordpress-kustomize.zip",
expectedExt: ".zip",
expectedType: coreV1.K8sKustomize,
expectedType: core.K8sKustomize,
},
{
name: "Can Identify Docker Compose",
filePath: "./samples/valid-docker-compose.yml",
expectedExt: ".yml",
expectedType: coreV1.DockerCompose,
expectedType: core.DockerCompose,
},

{
name: "Can Identify Docker Compose v2",
filePath: "./samples/valid-compose-2.yml",
expectedExt: ".yml",
expectedType: coreV1.DockerCompose,
expectedType: core.DockerCompose,
},

// {
Expand Down
4 changes: 2 additions & 2 deletions registry/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/meshery/meshkit/utils/csv"
"github.com/meshery/meshkit/utils/manifests"
"github.com/meshery/schemas"
corev1alpha1 "github.com/meshery/schemas/models/core"
"github.com/meshery/schemas/models/core"
schmeaVersion "github.com/meshery/schemas/models/v1beta1"
"github.com/meshery/schemas/models/v1beta1/capability"
"github.com/meshery/schemas/models/v1beta1/component"
Expand Down Expand Up @@ -181,7 +181,7 @@ func (c *ComponentCSV) UpdateCompDefinition(compDef *component.ComponentDefiniti
for _, key := range compStyleValues {
if c.Shape != "" {
shape := c.Shape
compDefStyles.Shape = (*corev1alpha1.Shape)(&shape)
compDefStyles.Shape = (*core.Shape)(&shape)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This assignment is redundant as it is executed in every iteration of the loop starting at line 181, despite not depending on the loop variable key. For improved efficiency, consider moving the if c.Shape != "" block outside of the loop. Furthermore, the pointer cast (*core.Shape)(&shape) is non-idiomatic; if core.Shape is a defined type (e.g., type Shape string) rather than a type alias, this will cause a compilation error. A more idiomatic approach is to perform a type conversion: s := core.Shape(shape); compDefStyles.Shape = &s.

}
if c.PrimaryColor != "" {

Expand Down
Loading