github.com/grahambrereton-form3/tilt@v0.10.18/internal/yaml/utils.go (about)

     1  package yaml
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // TODO(maia): store yaml strings as []string so we don't need to parse strings for separators?
     9  const separator = "---"
    10  
    11  func ConcatYAML(yaml ...string) string {
    12  	if len(yaml) == 0 {
    13  		return ""
    14  	} else if len(yaml) == 1 {
    15  		return yaml[0]
    16  	}
    17  
    18  	result := yaml[0]
    19  	for _, y := range yaml[1:] {
    20  		result = concatYAML(result, y)
    21  	}
    22  	return result
    23  }
    24  
    25  func concatYAML(y1, y2 string) string {
    26  	y1 = strings.TrimSpace(y1)
    27  	y2 = strings.TrimSpace(y2)
    28  	if !hasEndingSeparator(y1) && !hasStartingSeparator(y2) {
    29  		return fmt.Sprintf("%s\n%s\n%s", y1, separator, y2)
    30  	} else if hasEndingSeparator(y1) && hasStartingSeparator(y2) {
    31  		y1 = strings.TrimSpace(strings.TrimSuffix(y1, separator))
    32  	}
    33  	return fmt.Sprintf("%s\n%s", y1, y2)
    34  }
    35  
    36  func hasStartingSeparator(y string) bool {
    37  	return strings.HasPrefix(y, separator)
    38  }
    39  
    40  func hasEndingSeparator(y string) bool {
    41  	return strings.HasSuffix(y, separator)
    42  }