Created
June 2, 2020 14:54
-
-
Save ramizpolic/83e9301a5797c32bce16c61ea8973d19 to your computer and use it in GitHub Desktop.
Convertes YAML file containing Windows paths to WSL paths
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
#!/usr/bin/env python | |
# Install requirements | |
# - pip install wsl-path-converter | |
# - pip install PyYAML | |
import yaml | |
import collections | |
import wsl_path_converter | |
def apply_filter(func, obj): | |
'''Applies provided filter function to the passed object''' | |
if isinstance(obj, dict): | |
# if dict, apply to each key | |
return {k: apply_filter(func, v) for k, v in obj.items()} | |
elif isinstance(obj, list): | |
# if list, apply to each element | |
return [apply_filter(func, elem) for elem in obj] | |
else: | |
try: | |
data = func(obj) | |
return data.encode('ascii','ignore') | |
except: | |
return obj | |
def main(): | |
'''Convertes YAML file containing Windows paths to WSL paths''' | |
# Config paths | |
load_path = "/mnt/c/Users/USERNAME/.kube/config" | |
save_path = "/home/USERNAME/.kube/config" | |
# Load file | |
with open(load_path) as file: | |
data = yaml.full_load(file) | |
print("Loaded from %s" % load_path) | |
# Apply filter | |
data = apply_filter(lambda v: wsl_path_converter.convert_u(v), data) | |
# Save file | |
with open(save_path, 'w') as outfile: | |
yaml.dump(data, outfile, default_flow_style=False) | |
print("Saved to %s" % save_path) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment