|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import argparse |
| 17 | +import json |
| 18 | +import sys |
| 19 | + |
| 20 | +import yaml |
| 21 | + |
| 22 | +from multistorageclient.config import StorageClientConfig |
| 23 | + |
| 24 | +from .action import Action |
| 25 | + |
| 26 | + |
| 27 | +class ConfigAction(Action): |
| 28 | + """ |
| 29 | + Action for configuration management commands. |
| 30 | + """ |
| 31 | + |
| 32 | + def name(self) -> str: |
| 33 | + return "config" |
| 34 | + |
| 35 | + def help(self) -> str: |
| 36 | + return "Configuration management commands" |
| 37 | + |
| 38 | + def setup_parser(self, parser: argparse.ArgumentParser) -> None: |
| 39 | + # Create subparsers for config subcommands |
| 40 | + subparsers = parser.add_subparsers( |
| 41 | + title="available commands", |
| 42 | + dest="subcommand", |
| 43 | + required=True, |
| 44 | + ) |
| 45 | + |
| 46 | + # Add validate subcommand |
| 47 | + validate_parser = subparsers.add_parser( |
| 48 | + "validate", help="Validate and print configuration used by MSC", add_help=True |
| 49 | + ) |
| 50 | + |
| 51 | + # Add options specific to validate subcommand |
| 52 | + validate_parser.add_argument( |
| 53 | + "--format", |
| 54 | + choices=["json", "yaml"], |
| 55 | + default="yaml", |
| 56 | + help="Output format (default: yaml)", |
| 57 | + ) |
| 58 | + |
| 59 | + validate_parser.add_argument( |
| 60 | + "--config-file", |
| 61 | + help="Path to a specific config file (overrides default search paths)", |
| 62 | + default=None, |
| 63 | + metavar="CONFIG_FILE_PATH", |
| 64 | + ) |
| 65 | + |
| 66 | + validate_parser.epilog = """examples: |
| 67 | + # Validate and print resolved MSC configuration based on default search path |
| 68 | + msc config validate |
| 69 | +
|
| 70 | + # Validate and print resolved MSC configuration based on specific config file |
| 71 | + msc config validate --config-file /path/to/config.yaml |
| 72 | +""" |
| 73 | + |
| 74 | + def run(self, args: argparse.Namespace) -> int: |
| 75 | + """Run the config action with parsed arguments.""" |
| 76 | + if args.subcommand == "validate": |
| 77 | + return self._run_validate(args) |
| 78 | + else: |
| 79 | + print(f"Unknown subcommand: {args.subcommand}") |
| 80 | + return 1 |
| 81 | + |
| 82 | + def _run_validate(self, args: argparse.Namespace) -> int: |
| 83 | + """Handle the 'config validate' subcommand.""" |
| 84 | + try: |
| 85 | + config_file_paths = [args.config_file] if args.config_file else None |
| 86 | + |
| 87 | + # Get the merged and validated config |
| 88 | + config_obj = StorageClientConfig.from_file(config_file_paths=config_file_paths) |
| 89 | + config_dict = config_obj._config_dict |
| 90 | + |
| 91 | + # Output in requested format |
| 92 | + if args.format == "json": |
| 93 | + print(json.dumps(config_dict, indent=2)) |
| 94 | + else: # yaml |
| 95 | + print(yaml.dump(config_dict, default_flow_style=False, sort_keys=False)) |
| 96 | + return 0 |
| 97 | + |
| 98 | + except Exception as e: |
| 99 | + print(f"Error: {str(e)}", file=sys.stderr) |
| 100 | + return 1 |
0 commit comments