1313# limitations under the License.
1414
1515import argparse
16+ import json
17+ import logging
18+ import os
1619import sys
1720
18- def handle_configure (dry_run = False ):
19- # TODO(https://github.com/googleapis/librarian/issues/466): Implement configure command.
20- print ("'configure' command executed." )
21+ logger = logging .getLogger ()
2122
22- def handle_generate (dry_run = False ):
23- # TODO(https://github.com/googleapis/librarian/issues/448): Implement generate command.
24- print ("'generate' command executed." )
23+ LIBRARIAN_DIR = "librarian"
24+ GENERATE_REQUEST_FILE = "generate-request.json"
2525
26- def handle_build (dry_run = False ):
27- # TODO(https://github.com/googleapis/librarian/issues/450): Implement build command.
28- print ("'build' command executed." )
26+
27+ def _read_json_file (path ):
28+ """Helper function that reads a json file path and returns the loaded json content.
29+
30+ Args:
31+ path (str): The file path to read.
32+
33+ Returns:
34+ dict: The parsed JSON content.
35+
36+ Raises:
37+ FileNotFoundError: If the file is not found at the specified path.
38+ json.JSONDecodeError: If the file does not contain valid JSON.
39+ IOError: If there is an issue reading the file.
40+ """
41+ with open (path , "r" ) as f :
42+ return json .load (f )
43+
44+
45+ def handle_configure ():
46+ # TODO(https://github.com/googleapis/librarian/issues/466): Implement configure command and update docstring.
47+ logger .info ("'configure' command executed." )
48+
49+
50+ def handle_generate ():
51+ """The main coordinator for the code generation process.
52+
53+ This function orchestrates the generation of a client library by reading a
54+ `librarian/generate-request.json` file, determining the necessary Bazel rule for each API, and
55+ (in future steps) executing the build.
56+
57+ Raises:
58+ ValueError: If the `generate-request.json` file is not found or read.
59+ """
60+
61+ # Read a generate-request.json file
62+ try :
63+ request_data = _read_json_file (f"{ LIBRARIAN_DIR } /{ GENERATE_REQUEST_FILE } " )
64+ except Exception as e :
65+ raise ValueError (
66+ f"failed to read { LIBRARIAN_DIR } /{ GENERATE_REQUEST_FILE } "
67+ ) from e
68+
69+ logger .info (json .dumps (request_data , indent = 2 ))
70+
71+ # TODO(https://github.com/googleapis/librarian/issues/448): Implement generate command and update docstring.
72+ logger .info ("'generate' command executed." )
73+
74+
75+ def handle_build ():
76+ # TODO(https://github.com/googleapis/librarian/issues/450): Implement build command and update docstring.
77+ logger .info ("'build' command executed." )
2978
3079
3180if __name__ == "__main__" :
3281 parser = argparse .ArgumentParser (description = "A simple CLI tool." )
33- subparsers = parser .add_subparsers (dest = "command" , required = True , help = "Available commands" )
82+ subparsers = parser .add_subparsers (
83+ dest = "command" , required = True , help = "Available commands"
84+ )
3485
3586 # Define commands
87+ handler_map = {
88+ "configure" : handle_configure ,
89+ "generate" : handle_generate ,
90+ "build" : handle_build ,
91+ }
92+
3693 for command_name , help_text in [
3794 ("configure" , "Onboard a new library or an api path to Librarian workflow." ),
3895 ("generate" , "generate a python client for an API." ),
39- ("build" , "Run unit tests via nox for the generated library." )
96+ ("build" , "Run unit tests via nox for the generated library." ),
4097 ]:
41- handler_map = {"configure" : handle_configure , "generate" : handle_generate , "build" : handle_build }
4298 parser_cmd = subparsers .add_parser (command_name , help = help_text )
4399 parser_cmd .set_defaults (func = handler_map [command_name ])
44100
@@ -47,4 +103,4 @@ def handle_build(dry_run=False):
47103 sys .exit (1 )
48104
49105 args = parser .parse_args ()
50- args .func (args )
106+ args .func ()
0 commit comments