| 112 | |
| 113 | |
| 114 | def migrate_config(config_file: str, quiet: bool = False) -> int: |
| 115 | with open(config_file) as f: |
| 116 | orig_contents = contents = f.read() |
| 117 | |
| 118 | with cfgv.reraise_as(InvalidConfigError): |
| 119 | with cfgv.validate_context(f'File {config_file}'): |
| 120 | try: |
| 121 | yaml_load(orig_contents) |
| 122 | except Exception as e: |
| 123 | raise cfgv.ValidationError(str(e)) |
| 124 | |
| 125 | contents = _migrate_map(contents) |
| 126 | contents = _migrate_composed(contents) |
| 127 | |
| 128 | if contents != orig_contents: |
| 129 | with open(config_file, 'w') as f: |
| 130 | f.write(contents) |
| 131 | |
| 132 | print('Configuration has been migrated.') |
| 133 | elif not quiet: |
| 134 | print('Configuration is already migrated.') |
| 135 | return 0 |