|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import argparse |
16 | | -import sys |
17 | 16 | import json |
18 | | -import os |
19 | 17 | import logging |
| 18 | +import os |
| 19 | +import sys |
20 | 20 |
|
21 | 21 | logger = logging.getLogger() |
22 | 22 |
|
23 | 23 | LIBRARIAN = "/librarian" |
24 | 24 |
|
| 25 | + |
25 | 26 | # Helper function that reads a json file path and returns the loaded json content. |
26 | | -# Remove me. |
27 | 27 | def _read_json_file(path): |
28 | | - if not os.path.exists(path): |
29 | | - raise FileNotFoundError(f"Request file not found at '{path}'") |
30 | | - try: |
31 | | - with open(path, 'r') as f: |
32 | | - return json.load(f) |
33 | | - except json.JSONDecodeError: |
34 | | - raise ValueError(f"Invalid JSON in request file '{path}'") |
35 | | - except IOError as e: |
36 | | - raise IOError(f"Invalid JSON in request file '{path}': {e}") |
| 28 | + with open(path, "r") as f: |
| 29 | + return json.load(f) |
37 | 30 |
|
38 | 31 |
|
39 | 32 | def handle_configure(dry_run=False): |
40 | 33 | # TODO(https://github.com/googleapis/librarian/issues/466): Implement configure command. |
41 | 34 | print("'configure' command executed.") |
42 | 35 |
|
| 36 | + |
43 | 37 | def handle_generate(dry_run=False): |
44 | | - # TODO(https://github.com/googleapis/librarian/issues/448): Implement generate command. |
45 | 38 |
|
46 | 39 | # Read a generate-request.json file |
47 | | - request_path = f"{LIBRARIAN}/generate-request.json" |
48 | | - try: |
49 | | - request_data = _read_json_file(request_path) |
50 | | - except Exception as e: |
51 | | - logger.error(e) |
52 | | - sys.exit(1) |
| 40 | + if not dry_run: |
| 41 | + request_path = f"{LIBRARIAN}/generate-request.json" |
| 42 | + try: |
| 43 | + request_data = _read_json_file(request_path) |
| 44 | + except Exception as e: |
| 45 | + logger.error(e) |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | + # Print the data: |
| 49 | + print(json.dumps(request_data, indent=2)) |
53 | 50 |
|
54 | | - # Print the data: |
55 | | - print(json.dumps(request_data, indent=2)) |
| 51 | + # TODO(https://github.com/googleapis/librarian/issues/448): Implement generate command. |
56 | 52 | print("'generate' command executed.") |
57 | 53 |
|
| 54 | + |
58 | 55 | def handle_build(dry_run=False): |
59 | 56 | # TODO(https://github.com/googleapis/librarian/issues/450): Implement build command. |
60 | 57 | print("'build' command executed.") |
61 | 58 |
|
62 | 59 |
|
63 | 60 | if __name__ == "__main__": |
64 | 61 | parser = argparse.ArgumentParser(description="A simple CLI tool.") |
65 | | - subparsers = parser.add_subparsers(dest="command", required=True, help="Available commands") |
| 62 | + subparsers = parser.add_subparsers( |
| 63 | + dest="command", required=True, help="Available commands" |
| 64 | + ) |
66 | 65 |
|
67 | 66 | # Define commands |
68 | 67 | for command_name, help_text in [ |
69 | 68 | ("configure", "Onboard a new library or an api path to Librarian workflow."), |
70 | 69 | ("generate", "generate a python client for an API."), |
71 | | - ("build", "Run unit tests via nox for the generated library.") |
| 70 | + ("build", "Run unit tests via nox for the generated library."), |
72 | 71 | ]: |
73 | | - handler_map = {"configure": handle_configure, "generate": handle_generate, "build": handle_build} |
| 72 | + handler_map = { |
| 73 | + "configure": handle_configure, |
| 74 | + "generate": handle_generate, |
| 75 | + "build": handle_build, |
| 76 | + } |
74 | 77 | parser_cmd = subparsers.add_parser(command_name, help=help_text) |
75 | 78 | parser_cmd.set_defaults(func=handler_map[command_name]) |
76 | 79 |
|
|
0 commit comments