Skip to content

Instantly share code, notes, and snippets.

@so0k
Created June 15, 2017 02:40
Show Gist options
  • Save so0k/256cde434cbb0b58702249b31203a357 to your computer and use it in GitHub Desktop.
Save so0k/256cde434cbb0b58702249b31203a357 to your computer and use it in GitHub Desktop.
Print Helm's Values.yaml
import yaml
print_format="| {parameter:<40}| | {default:<50}|"
def walk_dict(d,keys=[],depth=0):
for k,v in sorted(d.items(),key=lambda x: x[0]):
keys.append(k)
if isinstance(v,dict):
walk_dict(v,keys,depth+1)
else:
print(print_format.format(parameter='`{0}`'.format(".".join(keys)),default='`{0}`'.format(v)))
keys.pop()
s = open("./values.yaml")
d = yaml.load(s)
walk_dict(d)
@so0k
Copy link
Author

so0k commented Jun 15, 2017

looking at https://stackoverflow.com/questions/7255885/save-dump-a-yaml-file-with-comments-in-pyyaml to get the comments into the description field

@so0k
Copy link
Author

so0k commented Jun 15, 2017

@so0k
Copy link
Author

so0k commented Oct 6, 2017

to walk over variables.tf and output.tf:

#!/usr/bin/env python

import hcl

print_var_format="|`{parameter:<25}`|`{default:<20}`|{description:<80}|{required:<8}|"
print_out_format="|`{parameter:<25}`|{description:<80}|"

var_header="|{name:^27}|{default:^22}|{description:^80}|{required:^8}|\n|{a:-<27}|{a:-<22}|{a:-<80}|{a:-<8}|".format(
        name="Name",
        default="Default",
        description="Description",
        required="Required",
        a=":"
)

out_header="|{name:<27}|{description:<80}|\n|{a:-<27}|{a:-<80}|".format(
        name="Name",
        description="Description",
        a=":"
)

with open('variables.tf', 'r') as fp:
    d = hcl.load(fp)
    var = d.get("variable")
    if var != None:
        print("## Variables\n")
        print(var_header)
        for k,v in sorted(var.items(),key=lambda x:x[0]):
            required = v.get('default') == None
            if not required and isinstance(v.get('default'),dict):
                for x,y in v['default'].items():
                    s = print_var_format.format(
                            parameter=k+"."+x,
                            default=y,
                            description=v.get('description')+" "+x,
                            required= "No"
                            )
                    print(s)
            else:
                s = print_var_format.format(
                    parameter=k,
                    default=v.get('default'),
                    description=v.get('description'),
                    required= "Yes" if required else "No"
                )
                print(s)
        print("\n")
    out = d.get("output")
    if out != None:
        print("## Outputs\n")
        print(out_header)
        for k,v in sorted(out.items(),key=lambda x:x[0]):
            s = print_out_format.format(
                    parameter=k,
                    description=v.get('description')
                )
            print(s)
        print("\n")

@so0k
Copy link
Author

so0k commented Oct 6, 2017

Sample variables.tf

variable "test" {
  description = "Config map"
  default = {
    foo = "bar"
    baz = "qux"
  }
}

Result:

|           Name            |       Default        |                                  Description                                   |Required|
|:--------------------------|:---------------------|:-------------------------------------------------------------------------------|:-------|
|`test.foo                 `|`bar                 `|Config map foo                                                                  |No      |
|`test.baz                 `|`qux                 `|Config map baz                                                                  |No      |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment