-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from ContainerSolutions/pretty-kustomization-yaml
Pretty print kustomization.yaml
- Loading branch information
Showing
4 changed files
with
116 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package generators | ||
|
||
var commentsMapping = map[string]string{ | ||
"namespace": "# Adds namespace to all resources.", | ||
"namePrefix": "# Value of this field is prepended to the\n" + | ||
"# names of all resources", | ||
"commonLabels": "# Labels to add to all resources and selectors.", | ||
"commonAnnotations": "# Annotations (non-identifying metadata)\n" + | ||
"# to add to all resources. Like labels,\n" + | ||
"# these are key value pairs.", | ||
"resources": "# List of resource files that kustomize reads, modifies\n" + | ||
"# and emits as a YAML string", | ||
"configMapGenerator": "# Each entry in this list results in the creation of\n" + | ||
"# one ConfigMap resource (it's a generator of n maps).", | ||
"secretGenerator": "# Each entry in this list results in the creation of\n" + | ||
"# one Secret resource (it's a generator of n secrets).", | ||
"generatorOptions": "# generatorOptions modify behavior of all ConfigMap\n" + | ||
"# and Secret generators", | ||
"patches": "# Each entry in this list should resolve to\n" + | ||
"# a partial or complete resource definition file.", | ||
"patchesJson6902": "# Each entry in this list should resolve to\n" + | ||
"# a kubernetes object and a JSON patch that will be applied\n" + | ||
"# to the object.", | ||
"crds": "# Each entry in this list should be a relative path to\n" + | ||
"# a file for custom resource definition(CRD).", | ||
"vars": "# Vars are used to insert values from resources that cannot\n" + | ||
"# be referenced otherwise.", | ||
"imageTags": "# ImageTags modify the tags for images without\n" + | ||
"# creating patches.", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package generators | ||
|
||
import ( | ||
"bufio" | ||
"io/ioutil" | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/ghodss/yaml" | ||
"github.com/golang/glog" | ||
) | ||
|
||
// Pattern used to detect if a line contains a YAML key | ||
var yamlKeyPattern = regexp.MustCompile("^[^ :]*:") | ||
|
||
// writeYamlFile write a given interface into yaml | ||
func writeYamlFile(filePath string, data interface{}) error { | ||
output, err := yaml.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return writeFile(filePath, output, 0644) | ||
} | ||
|
||
// writeAndFormatKustomizationConfig adds line break and comments | ||
func writeAndFormatKustomizationConfig(filePath string, comments bool) error { | ||
glog.V(4).Infof("Formatting %s", filePath) | ||
|
||
file, err := os.Open(filePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer file.Close() | ||
|
||
var output []string | ||
|
||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
|
||
// add line break before comment except for if this is the first line | ||
if yamlKeyPattern.MatchString(line) && len(output) > 0 { | ||
output = append(output, "") | ||
} | ||
|
||
// add comments | ||
if comments { | ||
for key, value := range commentsMapping { | ||
if strings.HasPrefix(line, key+":") { | ||
output = append(output, value) | ||
} | ||
} | ||
} | ||
|
||
output = append(output, line) | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return err | ||
} | ||
|
||
return writeFile(filePath, []byte(strings.Join(output, "\n")), 0644) | ||
} | ||
|
||
// writeFile writes data to a file named by filename. | ||
func writeFile(filePath string, data []byte, perm os.FileMode) error { | ||
glog.V(4).Infof("Writing %s", filePath) | ||
|
||
err := ioutil.WriteFile(filePath, data, perm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |