MonkeyCanCode commented on code in PR #4465: URL: https://github.com/apache/polaris/pull/4465#discussion_r3285766687
########## 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: Fixed. ########## 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: Fixed. -- 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]
