Skip to content

Instantly share code, notes, and snippets.

@discordianfish
Created June 8, 2017 12:23
Show Gist options
  • Save discordianfish/a22ecebe87136a74bfb2637d873337d7 to your computer and use it in GitHub Desktop.
Save discordianfish/a22ecebe87136a74bfb2637d873337d7 to your computer and use it in GitHub Desktop.
manifest-split extracts all documents in a kubernetes manifest to their own files
#!/usr/bin/env python
"""manifest-split extracts all documents in a kubernetes manifest to their own
files
Usage:
manifest-split [manifest.yaml]
If manifest is omitted, read from stdin and use 'default' as file prefix."""
import sys
import fileinput
from os import path
from __future__ import print_function
def write_files(basename):
kind = None
doc = ""
for line in fileinput.input():
if line.strip() == '---':
if kind:
filename = "%s-%s.yaml" % (basename, kind)
with open(filename, 'w') as fileh:
print(doc, file=fileh)
doc = ""
continue
else:
if doc.isspace():
continue
raise "Attribute `kind` is missing in %s", doc
doc += line
parts = line.split(':')
if len(parts) == 2 and parts[0] == 'kind':
k = parts[1].strip()
kind = k[0].lower() + k[1:]
NAME = "default"
if len(sys.argv) > 1:
name, _ = path.splitext(path.basename(sys.argv[1]))
write_files(name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment