-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-skaffold.sh
74 lines (56 loc) · 2.51 KB
/
parse-skaffold.sh
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
# This script expects one parameter - the file path of a `skaffold render` output file
# This file contains all the skaffold rendered manifests into a single file
# This script processes this file to split all the k8s manifests into separate files
# The script expects the input file to start with a "# Source" path to a file to start to write to
# Lines containing "---" are ignored
# Script can also be invoked using pipes as in
# cat render-file.yaml | parse-skaffold.sh
# When skaffold render is used, skaffold keeps the source path information for every file
# For example : # Source: my-istio/templates/ingressgw-istiooperator.yaml
# This path is extracted and used to separate the manifests into directories
ManifestsDir="manifests"
# The output manifests are generated in the $ManifestsDir dir
rm -rf "$ManifestsDir"
mkdir "$ManifestsDir"
outputPath=""
# IFS is set to empty to include the leading empty spaces in the read -r command
# This is needed to keep the yaml structure correct
# OIFS is a temp var used to hold the previous value of IFS so we can set it back later
OIFS=$IFS
IFS=
# We are processing the input file line by line and we output the lines to output files
# When the delimiter line is found in the form of # Source: ..., we start output to a new file
# The path of the new file is extracted from the delimiter line
while read -r line; do
shouldSwitch=`echo $line | cut -c1-10`
if [[ "$shouldSwitch" == "# Source: " ]]
then
# We have hit the delimiter line. We are extracting the new path
echo ""
#echo ""
# Take the new output path (everything after # Source: )
outputPath=`echo $line | cut -c11-`
# Get the directory from path
outputDir=`dirname $outputPath`
# Remove the /templates suffix from dir. This is specific, might have to be skipped in some cases
# outputDir=${outputDir%/*}
# Create the new dir as $ManifestsDir/dir (if doesn't already exist - to avoid errors when multiple templates in the same dir)
[ ! -d "$ManifestsDir/$outputDir" ] && mkdir -p "$ManifestsDir/$outputDir"
# Get the new output file
outputFile=`basename $outputPath`
printf "%s" "$ManifestsDir/$outputDir/$outputFile"
else
# Skip line if contains "---"
# Otherwise, write line to output file
if [[ "$line" != "---" ]]
then
printf "."
echo $line >> "$ManifestsDir/$outputDir/$outputFile"
fi
fi
# echo "$line"
done < "${1:-/dev/stdin}"
echo ""
# Return the previous value of IFS
IFS=$OIFS