flyrain commented on code in PR #4465: URL: https://github.com/apache/polaris/pull/4465#discussion_r3277892738
########## client/python/apache_polaris/cli/command/repl.py: ########## @@ -0,0 +1,146 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +import os +import shlex +import sys +from dataclasses import dataclass +from typing import Optional +from cmd import Cmd +import readline +from apache_polaris.cli.command import Command +from apache_polaris.cli.constants import ( + Commands, + REPL_HISTORY_LENGTH, + REPL_HISTORY_FILE, +) +from apache_polaris.cli.exceptions import CliError +from apache_polaris.cli.options.option_tree import OptionTree +from apache_polaris.cli.options.parser import Parser +from apache_polaris.sdk.management import PolarisDefaultApi +from apache_polaris.sdk.management.exceptions import ApiException +from apache_polaris.cli.command.profiles import ProfilesCommand +from apache_polaris.cli.polaris_cli import PolarisCli +from urllib.parse import urlparse + + +@dataclass +class ReplCommand(Command): + """ + A Command implementation to represent `polaris repl`. This command starts an interactive REPL session. + + Example commands: + * ./polaris repl + """ + + profile: Optional[str] = None + + def validate(self) -> None: + pass + + def execute(self, api: PolarisDefaultApi) -> None: + try: + PolarisRepl(api, profile=self.profile).cmdloop() + except KeyboardInterrupt: + sys.stdout.write("\nExiting REPL session.\n") Review Comment: Not a blocker: Ctrl-C at the prompt raises `KeyboardInterrupt` out of `input()` and exits the whole REPL here. A lot of shells/REPLs treat Ctrl-C as "clear current line or stop the current execution, give me a new prompt" and reserve Ctrl-D for exit. Consider catching `KeyboardInterrupt` inside a `cmdloop` wrapper and re-entering the loop with a fresh prompt. ########## client/python/apache_polaris/cli/command/repl.py: ########## @@ -0,0 +1,146 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +import os +import shlex +import sys +from dataclasses import dataclass +from typing import Optional +from cmd import Cmd +import readline +from apache_polaris.cli.command import Command +from apache_polaris.cli.constants import ( + Commands, + REPL_HISTORY_LENGTH, + REPL_HISTORY_FILE, +) +from apache_polaris.cli.exceptions import CliError +from apache_polaris.cli.options.option_tree import OptionTree +from apache_polaris.cli.options.parser import Parser +from apache_polaris.sdk.management import PolarisDefaultApi +from apache_polaris.sdk.management.exceptions import ApiException +from apache_polaris.cli.command.profiles import ProfilesCommand +from apache_polaris.cli.polaris_cli import PolarisCli +from urllib.parse import urlparse + + +@dataclass +class ReplCommand(Command): + """ + A Command implementation to represent `polaris repl`. This command starts an interactive REPL session. + + Example commands: + * ./polaris repl + """ + + profile: Optional[str] = None + + def validate(self) -> None: + pass + + def execute(self, api: PolarisDefaultApi) -> None: + try: + PolarisRepl(api, profile=self.profile).cmdloop() + except KeyboardInterrupt: + sys.stdout.write("\nExiting REPL session.\n") + + +class PolarisRepl(Cmd): + intro = "\n".join( + [ + "Welcome to the Apache Polaris CLI REPL. Type 'help' for commands, 'exit' or Ctrl-D to quit.", + "Note: global auth flags (--host, --port, --profile, --client-id, --client-secret, ...) are bound " + "at session start and ignored when re-specified inside the REPL.", + ] + ) + + def __init__( + self, + api: PolarisDefaultApi, + profile: Optional[str] = None, + ): + super().__init__() + self.api = api + display_name = profile or urlparse(api.api_client.configuration.host).netloc + self.prompt = f"polaris@{display_name}> " + if readline is not None: + try: + readline.read_history_file(REPL_HISTORY_FILE) + except (FileNotFoundError, OSError): + pass + readline.set_history_length(REPL_HISTORY_LENGTH) + + def default(self, line: str) -> None: + if not line.strip(): + return + try: + args = shlex.split(line) + options = Parser.parse(args) + if options.command == Commands.REPL: + sys.stderr.write("Already in REPL session.\n") + return + command = Command.from_options(options) + if isinstance(command, ProfilesCommand): + command.execute() + else: + command.execute(self.api) + except SystemExit: + pass + except KeyboardInterrupt: + sys.stderr.write("Session interrupted. Type 'exit' to quit.\n") + except ApiException as e: + PolarisCli._try_print_exception(e) Review Comment: Reaching into `PolarisCli._try_print_exception` (underscore-prefixed) couples REPL error formatting to a private API in another module. Promote it to a public helper, or move the api-exception printer to a small util both `polaris_cli.py` and `repl.py` can import without breaking encapsulation. ########## client/python/apache_polaris/cli/constants.py: ########## @@ -361,3 +362,11 @@ class Hints: "~/.polaris" ) CONFIG_FILE = os.path.join(CONFIG_DIR, ".polaris.json") +REPL_HISTORY_FILE = os.path.join(CONFIG_DIR, ".polaris_repl_history") +_DEFULT_REPL_HISTORY_LENGTH = 1000 +try: + REPL_HISTORY_LENGTH = int( + os.environ.get("POLARIS_REPL_HISTORY_LENGTH", _DEFULT_REPL_HISTORY_LENGTH) + ) +except ValueError: + REPL_HISTORY_LENGTH = _DEFULT_REPL_HISTORY_LENGTH Review Comment: `int(...)` accepts negative values like `-1`, which `readline.set_history_length` interprets as unlimited. Either clamp to `>= 0` or document the behavior. ########## site/content/in-dev/unreleased/command-line-interface.md: ########## @@ -1811,6 +1813,10 @@ Command Options: polaris tables delete my_table --catalog my_catalog --namespace ns1 ``` +### REPL + +The `REPL` command is used to start an interactive REPL session for Polaris CLI. Review Comment: One sentence is thin for a new top-level command. Worth covering: history file location (`~/.polaris/.polaris_repl_history`), the `POLARIS_REPL_HISTORY_LENGTH` env var, the built-ins (`exit`, `help`, Ctrl-D), and the note that global auth flags are bound at session start. ########## client/python/apache_polaris/cli/constants.py: ########## @@ -361,3 +362,11 @@ class Hints: "~/.polaris" ) CONFIG_FILE = os.path.join(CONFIG_DIR, ".polaris.json") +REPL_HISTORY_FILE = os.path.join(CONFIG_DIR, ".polaris_repl_history") +_DEFULT_REPL_HISTORY_LENGTH = 1000 Review Comment: typo: `_DEFULT_REPL_HISTORY_LENGTH` -> `_DEFAULT_REPL_HISTORY_LENGTH` (also referenced on lines 369 and 372). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
