| 504 | |
| 505 | |
| 506 | class PackConfigCommand(resource.ResourceCommand): |
| 507 | def __init__(self, resource, *args, **kwargs): |
| 508 | super(PackConfigCommand, self).__init__( |
| 509 | resource, |
| 510 | "config", |
| 511 | "Configure a %s based on config schema." |
| 512 | % resource.get_display_name().lower(), |
| 513 | *args, |
| 514 | **kwargs, |
| 515 | ) |
| 516 | |
| 517 | self.parser.add_argument( |
| 518 | "name", |
| 519 | help="Name of the %s(s) to configure." |
| 520 | % resource.get_display_name().lower(), |
| 521 | ) |
| 522 | |
| 523 | @add_auth_token_to_kwargs_from_cli |
| 524 | def run(self, args, **kwargs): |
| 525 | schema = self.app.client.managers["ConfigSchema"].get_by_ref_or_id( |
| 526 | args.name, **kwargs |
| 527 | ) |
| 528 | |
| 529 | if not schema: |
| 530 | msg = "%s \"%s\" doesn't exist or doesn't have a config schema defined." |
| 531 | raise resource.ResourceNotFoundError( |
| 532 | msg % (self.resource.get_display_name(), args.name) |
| 533 | ) |
| 534 | |
| 535 | config = interactive.InteractiveForm(schema.attributes).initiate_dialog() |
| 536 | |
| 537 | message = "---\nDo you want to preview the config in an editor before saving?" |
| 538 | description = "Secrets will be shown in plain text." |
| 539 | preview_dialog = interactive.Question( |
| 540 | message, {"default": "y", "description": description} |
| 541 | ) |
| 542 | if preview_dialog.read() == "y": |
| 543 | contents = yaml.safe_dump(config, indent=4, default_flow_style=False) |
| 544 | modified = editor.editor(text=contents) |
| 545 | config = yaml.safe_load(modified) |
| 546 | |
| 547 | message = "---\nDo you want me to save it?" |
| 548 | save_dialog = interactive.Question(message, {"default": "y"}) |
| 549 | if save_dialog.read() == "n": |
| 550 | raise OperationFailureException("Interrupted") |
| 551 | |
| 552 | config_item = Config(pack=args.name, values=config) |
| 553 | result = self.app.client.managers["Config"].update(config_item, **kwargs) |
| 554 | |
| 555 | return result |
| 556 | |
| 557 | def run_and_print(self, args, **kwargs): |
| 558 | try: |
| 559 | instance = self.run(args, **kwargs) |
| 560 | if not instance: |
| 561 | raise Exception("Configuration failed") |
| 562 | self.print_output( |
| 563 | instance, |