Skip to content

Commit

Permalink
Merge pull request #617 from Jougan-0/introspecFileType
Browse files Browse the repository at this point in the history
introspec file type w/signoff
  • Loading branch information
Jougan-0 authored Nov 19, 2024
2 parents 62f4e18 + 482a5e4 commit 9c570ce
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions utils/detect_pattern_file_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package utils

import (
"archive/tar"
"bytes"
"compress/gzip"
"io"
"regexp"
"strings"

"cuelang.org/go/cue/errors"
"gopkg.in/yaml.v3"
)

// Function to identify the type of input
func IdentifyInputType(data []byte) (string, error) {
if isMesheryDesign(data) {
return "Meshery Design", nil
}
if isDockerCompose(data) {
return "Docker Compose", nil
}
if isHelmChart(data) {
return "Helm Chart", nil
}
if isK8sManifest(data) {
return "Kubernetes Manifest", nil
}
return "", errors.New("unknown type")
}

// Check if the input is a Meshery design
func isMesheryDesign(data []byte) bool {
var mesheryPattern map[string]interface{}
if err := yaml.Unmarshal(data, &mesheryPattern); err != nil {
return false
}
_, exists := mesheryPattern["services"]
return exists
}

// Check if the input is a Docker Compose file
func isDockerCompose(data []byte) bool {
dockerComposeKeys := []string{"version", "services"}
content := string(data)
for _, key := range dockerComposeKeys {
if !strings.Contains(content, key+":") {
return false
}
}
return true
}

// Check if the input is a Helm chart (.tgz file)
func isHelmChart(data []byte) bool {
gzReader, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return false
}
defer gzReader.Close()

tarReader := tar.NewReader(gzReader)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return false
}
if strings.HasSuffix(header.Name, "Chart.yaml") {
return true
}
}
return false
}

// Check if the input is a Kubernetes manifest
func isK8sManifest(data []byte) bool {
k8sRegex := regexp.MustCompile(`(?m)^kind:\s+\w+`)
return k8sRegex.Match(data)
}

0 comments on commit 9c570ce

Please sign in to comment.