Fokko commented on code in PR #5417:
URL: https://github.com/apache/iceberg/pull/5417#discussion_r942607508


##########
python/pyiceberg/cli/console.py:
##########
@@ -0,0 +1,228 @@
+# 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
+from typing import Literal, Optional, Tuple
+
+import click
+
+from pyiceberg.catalog import Catalog
+from pyiceberg.catalog.hive import HiveCatalog
+from pyiceberg.catalog.rest import RestCatalog
+from pyiceberg.cli.output import ConsoleOutput, JsonOutput, Output
+from pyiceberg.exceptions import NoSuchNamespaceError, NoSuchTableError
+
+
[email protected]()
[email protected]("--catalog", default="catalog")
[email protected]("--output", type=click.Choice(["text", "json"]), default="text")
[email protected]("--uri", default=lambda: os.environ.get("PYICEBERG_URI"))
[email protected]("--credential", default=lambda: 
os.environ.get("PYICEBERG_CREDENTIAL"))
[email protected]_context
+def run(ctx, catalog: str, output: str, uri: str, credential: str):
+    ctx.ensure_object(dict)
+    if output == "text":
+        ctx.obj["output"] = ConsoleOutput()
+    elif output == "json":
+        ctx.obj["output"] = JsonOutput()
+    else:
+        raise ValueError(f"Unknown output: {output}")
+
+    if not uri:
+        ctx.obj["output"].exception(
+            f"Missing uri, cannot connect to catalog. Please provide using 
--uri or by setting the environment variable PYICEBERG_URI"
+        )
+
+    if uri.startswith("http"):
+        ctx.obj["catalog"] = RestCatalog(name=catalog, properties={}, uri=uri, 
credential=credential)
+    elif uri.startswith("thrift"):
+        ctx.obj["catalog"] = HiveCatalog(name=catalog, properties={}, uri=uri)
+    else:
+        raise ValueError("Could not determine catalog type from uri. REST 
(http/https) and Hive (thrift) is supported")
+
+
+def _get_catalog_and_output(ctx) -> Tuple[Catalog, Output]:
+    """
+    Small helper to set the types
+    """
+    return ctx.obj["catalog"], ctx.obj["output"]
+
+
[email protected]()
[email protected]_context
[email protected]("parent", required=False)
+def list(ctx, parent: Optional[str]):
+    catalog, output = _get_catalog_and_output(ctx)
+
+    # still wip, will become more beautiful
+    # https://github.com/apache/iceberg/pull/5467/
+    # has been merged
+    if parent:
+        identifiers = catalog.list_tables(parent)
+    else:
+        identifiers = catalog.list_namespaces()
+
+    output.identifiers(identifiers)
+
+
[email protected]()
[email protected]("identifier")
[email protected]_context
+def describe(ctx, identifier: str):
+    """Describes a table"""
+    catalog, output = _get_catalog_and_output(ctx)
+    identifier_tuple = Catalog.identifier_to_tuple(identifier)
+
+    if len(identifier_tuple) > 1:
+        try:
+            table = catalog.load_table(identifier)
+            output.describe_table(table)
+        except NoSuchNamespaceError as exc:
+            output.exception(exc)
+    else:
+        output.exception(NoSuchTableError(f"Expected a namespace, got: 
{identifier}"))

Review Comment:
   I agree, but should we return the properties? 
https://github.com/apache/iceberg/blob/master/open-api/rest-catalog-open-api.yaml#L1809-L1830
 I did that for now



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to