diff --git a/files/sanitization.go b/files/sanitization.go index 33ff6d44..f1d7e138 100644 --- a/files/sanitization.go +++ b/files/sanitization.go @@ -258,3 +258,23 @@ func IsValidJson(data []byte) error { var unMarshalled interface{} return json.Unmarshal(data, &unMarshalled) } + +// StripLeadingComments removes leading comment lines (starting with #) and empty lines from YAML content. +func StripLeadingComments(yamlContent []byte) []byte { + lines := strings.Split(string(yamlContent), "\n") + + // Find the first non-comment, non-empty line + for i, line := range lines { + trimmed := strings.TrimSpace(line) + // Skip empty lines and comment lines + if trimmed == "" || strings.HasPrefix(trimmed, "#") { + continue + } + // Found the first non-comment, non-empty line, return the rest + result := strings.Join(lines[i:], "\n") + return []byte(result) + } + + // All lines were comments or empty, return empty slice + return []byte{} +} diff --git a/generators/github/package.go b/generators/github/package.go index 3bcb80ec..b7c42dc4 100644 --- a/generators/github/package.go +++ b/generators/github/package.go @@ -5,6 +5,7 @@ import ( "fmt" "os" + "github.com/meshery/meshkit/files" "github.com/meshery/meshkit/utils" "github.com/meshery/meshkit/utils/component" "github.com/meshery/meshkit/utils/kubernetes" @@ -47,7 +48,9 @@ func (gp GitHubPackage) GenerateComponents(group string) ([]_component.Component errs := []error{} for _, crd := range manifestBytes { - resource := string(crd) + // Strip leading comments (like copyright headers) from each YAML document + cleanedCrd := files.StripLeadingComments(crd) + resource := string(cleanedCrd) include, err := component.IncludeComponentBasedOnGroup(resource, group) if err != nil { @@ -58,10 +61,10 @@ func (gp GitHubPackage) GenerateComponents(group string) ([]_component.Component continue } - isCrd := kubernetes.IsCRD(string(crd)) + isCrd := kubernetes.IsCRD(resource) if !isCrd { - comps, err := component.GenerateFromOpenAPI(string(crd), gp) + comps, err := component.GenerateFromOpenAPI(resource, gp) if err != nil { errs = append(errs, component.ErrGetSchema(err)) continue @@ -69,7 +72,7 @@ func (gp GitHubPackage) GenerateComponents(group string) ([]_component.Component components = append(components, comps...) } else { - comp, err := component.Generate(string(crd)) + comp, err := component.Generate(resource) if err != nil { continue }