mirror of
https://github.com/dhall-lang/dhall-kubernetes.git
synced 2024-11-03 21:05:47 +03:00
805b432df2
This ports the conversion script from Python to Haskell (fix #7) The main reasons for this port are that: - the Python script was really hard to maintain for reasons like "converting from Swagger to Dhall is interleaved with string formatting" - in Haskell we can use the dhall library to generate always syntactically correct Dhall AST. It's also much easier to keep an eye on correctness, because types and pattern matching. It also forces us to deal with things like cyclic imports from the get go. Things happening here: - remove the `api` folder, removing the difference between "raw api" and "nice api" - move defaults from `default` to `defaults` folder, as it is in `dhall-nethack` - transition to the new syntax for `Optional` (fix #49) - add `types.dhall` and `defaults.dhall`, so that one can now easily "pin a version" by just importing these two records at a specific commit/tag. They also make it really easy to access objects, e.g. `let types = https://raw.githubusercontent... sha256:... in types.Deployment` - also add typesUnion.dhall (fix #54), so one is able to send to Kubernetes different objects in the same stream. This is also documented in the README - defaults are resolved recursively (fix #46): if there's an import of a "nullable" record, then it's not marked as Optional, making merging objects much easier - default objects are not lambdas anymore, and instead they just don't include the required fields (that is, the ones that are not nullable records), as suggested in dhall-lang/dhall-lang#382 - for objects that are simple types we used to generate a simple lambda `\(a : Text) -> a` as a default, now we just don't generate a default (e.g. see `io.k8s.apimachinery.pkg.apis.meta.v1.Time`) - autoformat all generated Dhall code - remove cyclic imports (fix #47) - update to dhall-1.22 and dhall-json-1.2.8
74 lines
2.1 KiB
Python
Executable File
74 lines
2.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
#
|
|
# Build YAML files from the example Dhall sources.
|
|
#
|
|
# An optional second argument determines the directory to write the YAML
|
|
# files to. Defaults to the exmaples directory.
|
|
#
|
|
# Also checks that the generated files do not differ from what is
|
|
# checked into source control. We list all generated files that differ
|
|
# and exit with status code 1 if the list is non-empty.
|
|
|
|
import os
|
|
from subprocess import run, DEVNULL
|
|
import sys
|
|
|
|
examples_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', 'examples'))
|
|
examples = [
|
|
'deploymentSimple',
|
|
'deployment',
|
|
'ingress',
|
|
'service'
|
|
]
|
|
|
|
TERM_FAIL = '\033[91m'
|
|
TERM_CLEAR = '\033[0m'
|
|
|
|
|
|
def check_clean(path_1, path_2):
|
|
if path_1 == path_2:
|
|
cmd = ['git', 'diff', '--exit-code', '--', path_1]
|
|
else:
|
|
cmd = ['diff', '--unified', '--', path_1, path_2]
|
|
|
|
result = run(cmd, check=False, stdout=DEVNULL)
|
|
if result.returncode == 0:
|
|
return True
|
|
elif result.returncode == 1:
|
|
return False
|
|
else:
|
|
# Raises a `CalledProcessError`
|
|
result.check_returncode()
|
|
|
|
|
|
def build_yaml (example, out_dir):
|
|
source = os.path.join(examples_dir, example + '.dhall')
|
|
print('Building {}'.format(source))
|
|
target = os.path.join(out_dir, example + '.yaml')
|
|
expected = os.path.join(examples_dir, 'out', example + '.yaml')
|
|
cmd = 'dhall-to-yaml --omitEmpty <<< "./{source}" >{target}'.format(source=source, target=target)
|
|
run(cmd, shell=True, executable='bash', check=True)
|
|
return check_clean(expected, target)
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) >= 2:
|
|
out_dir = sys.argv[1]
|
|
else:
|
|
out_dir = os.path.join(examples_dir, 'out')
|
|
|
|
clean_failures = []
|
|
for example in examples:
|
|
is_clean = build_yaml(example, out_dir)
|
|
if is_clean == False:
|
|
clean_failures.append(example + '.yaml')
|
|
|
|
if len(clean_failures) > 0:
|
|
print(TERM_FAIL + 'Clean check failed. The following files differ' + TERM_CLEAR)
|
|
for failure in clean_failures:
|
|
print(' ' + failure)
|
|
sys.exit(1)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|