File: yaml.go

package info (click to toggle)
kubernetes-split-yaml 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 372 kB
  • sloc: sh: 79; makefile: 14
file content (33 lines) | stat: -rw-r--r-- 703 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main

import (
	"fmt"
	"strings"

	"gopkg.in/yaml.v2"
)

func getYamlInfo(yamlContent string) (*KubernetesAPI, error) {

	// Start by removing all lines with templating in to create sane yaml
	cleanYaml := ""
	for _, line := range strings.Split(yamlContent, "\n") {
		if !strings.Contains(line, "{{") {
			cleanYaml += line + "\n"
		}
	}

	var m KubernetesAPI
	err := yaml.Unmarshal([]byte(cleanYaml), &m)
	if err != nil {
		return nil, fmt.Errorf("Could not unmarshal: %v \n---\n%v", err, yamlContent)
	}

	if m.Kind == "" {
		return nil, fmt.Errorf("yaml file with kind missing")
	} else if m.Metadata.Name == "" {
		return nil, fmt.Errorf("yaml file with name missing")
	}

	return &m, nil
}