github.com/qsis/helm@v3.0.0-beta.3+incompatible/pkg/releaseutil/manifest.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package releaseutil
    18  
    19  import (
    20  	"fmt"
    21  	"regexp"
    22  	"strings"
    23  )
    24  
    25  // SimpleHead defines what the structure of the head of a manifest file
    26  type SimpleHead struct {
    27  	Version  string `json:"apiVersion"`
    28  	Kind     string `json:"kind,omitempty"`
    29  	Metadata *struct {
    30  		Name        string            `json:"name"`
    31  		Annotations map[string]string `json:"annotations"`
    32  	} `json:"metadata,omitempty"`
    33  }
    34  
    35  var sep = regexp.MustCompile("(?:^|\\s*\n)---\\s*")
    36  
    37  // SplitManifests takes a string of manifest and returns a map contains individual manifests
    38  func SplitManifests(bigFile string) map[string]string {
    39  	// Basically, we're quickly splitting a stream of YAML documents into an
    40  	// array of YAML docs. In the current implementation, the file name is just
    41  	// a place holder, and doesn't have any further meaning.
    42  	tpl := "manifest-%d"
    43  	res := map[string]string{}
    44  	// Making sure that any extra whitespace in YAML stream doesn't interfere in splitting documents correctly.
    45  	bigFileTmp := strings.TrimSpace(bigFile)
    46  	docs := sep.Split(bigFileTmp, -1)
    47  	var count int
    48  	for _, d := range docs {
    49  		if d == "" {
    50  			continue
    51  		}
    52  
    53  		d = strings.TrimSpace(d)
    54  		res[fmt.Sprintf(tpl, count)] = d
    55  		count = count + 1
    56  	}
    57  	return res
    58  }