jerryshao commented on code in PR #5058: URL: https://github.com/apache/gravitino/pull/5058#discussion_r1810753663
########## clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java: ########## @@ -0,0 +1,210 @@ +/* + * 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. + */ + +package org.apache.gravitino.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Options; +import org.apache.gravitino.cli.commands.AllMetalakeDetails; +import org.apache.gravitino.cli.commands.CatalogDetails; +import org.apache.gravitino.cli.commands.ClientVersion; +import org.apache.gravitino.cli.commands.ListCatalogs; +import org.apache.gravitino.cli.commands.ListColumns; +import org.apache.gravitino.cli.commands.ListMetalakes; +import org.apache.gravitino.cli.commands.ListSchema; +import org.apache.gravitino.cli.commands.ListTables; +import org.apache.gravitino.cli.commands.MetalakeDetails; +import org.apache.gravitino.cli.commands.SchemaDetails; +import org.apache.gravitino.cli.commands.ServerVersion; +import org.apache.gravitino.cli.commands.TableDetails; + +/* Gravitino Command line */ +public class GravitinoCommandLine { + + private CommandLine line; + private Options options; + private String entity; + private String command; + private String urlEnv; Review Comment: Add `final` to all of the class variables if they're mutable. ########## clients/cli/build.gradle.kts: ########## @@ -0,0 +1,59 @@ +/* + * 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. + */ +plugins { + `maven-publish` + id("java") + id("idea") +} + +dependencies { + implementation(project(":clients:client-java")) + implementation(project(":api")) + implementation(project(":common")) + implementation(libs.slf4j.api) + implementation(libs.slf4j.simple) + implementation(libs.commons.cli.new) Review Comment: Make these dependencies alphabetical ordering. ########## clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListMetalakes.java: ########## @@ -0,0 +1,58 @@ +/* + * 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. + */ + +package org.apache.gravitino.cli.commands; + +import org.apache.gravitino.Metalake; +import org.apache.gravitino.client.GravitinoAdminClient; + +/** Lists all metalakes. */ +public class ListMetalakes extends Command { + + /** + * List all metalakes. + * + * @param url The URL of the Gravitino server. + */ + public ListMetalakes(String url) { + super(url); + } + + /** Lists all metalakes. */ + public void handle() { + Metalake[] metalakes = new Metalake[0]; + try { + GravitinoAdminClient client = buildAdminClient(); + metalakes = client.listMetalakes(); + } catch (Exception exp) { + System.err.println(exp.getMessage()); + return; + } + + StringBuilder all = new StringBuilder(); + for (int i = 0; i < metalakes.length; i++) { + if (i > 0) { + all.append(","); + } + all.append(metalakes[i].name()); + } Review Comment: You can use Java or Guava's `Joiner` to simplify your append here and in other places. ########## clients/cli/src/main/java/org/apache/gravitino/cli/commands/TableDetails.java: ########## @@ -0,0 +1,60 @@ +/* + * 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. + */ + +package org.apache.gravitino.cli.commands; + +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.rel.Table; + +/** Displays the details of a table. */ +public class TableDetails extends TableCommand { + + protected String schema; + protected String table; + + /** + * Displays the details of a table. + * + * @param url The URL of the Gravitino server. + * @param metalake The name of the metalake. + * @param catalog The name of the catalog. + * @param schema The name of the schenma. + * @param table The name of the table. + */ + public TableDetails(String url, String metalake, String catalog, String schema, String table) { + super(url, metalake, catalog); + this.schema = schema; + this.table = table; + } + + /** Displays the details of a table. */ + public void handle() { + Table gTable = null; + + try { + NameIdentifier name = NameIdentifier.of(table); + gTable = tableCatalog().loadTable(name); + } catch (Exception exp) { + System.err.println(exp.getMessage()); + return; + } + + System.out.println(gTable.name() + "," + gTable.comment()); Review Comment: Sometimes the `comment` will get null, did you test the null value? Also, it would be better to add more output for a table detail, not just only name and comment. ########## clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java: ########## @@ -0,0 +1,210 @@ +/* + * 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. + */ + +package org.apache.gravitino.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Options; +import org.apache.gravitino.cli.commands.AllMetalakeDetails; +import org.apache.gravitino.cli.commands.CatalogDetails; +import org.apache.gravitino.cli.commands.ClientVersion; +import org.apache.gravitino.cli.commands.ListCatalogs; +import org.apache.gravitino.cli.commands.ListColumns; +import org.apache.gravitino.cli.commands.ListMetalakes; +import org.apache.gravitino.cli.commands.ListSchema; +import org.apache.gravitino.cli.commands.ListTables; +import org.apache.gravitino.cli.commands.MetalakeDetails; +import org.apache.gravitino.cli.commands.SchemaDetails; +import org.apache.gravitino.cli.commands.ServerVersion; +import org.apache.gravitino.cli.commands.TableDetails; + +/* Gravitino Command line */ +public class GravitinoCommandLine { + + private CommandLine line; + private Options options; + private String entity; + private String command; + private String urlEnv; + + public static final String CMD = "gcli"; // recommended name + public static final String DEFAULT_URL = "http://localhost:8090"; + + /** + * Gravitino Command line. + * + * @param line Parsed command line object. + * @param options Available options for the CLI. + * @param entity The entity to apply the command to e.g. metlake, catalog, schema, table etc etc. + * @param command The type of command to run i.e. list, details, update, delete, or create. + */ + public GravitinoCommandLine(CommandLine line, Options options, String entity, String command) { + this.line = line; + this.options = options; + this.entity = entity; + this.command = command; + } + + /** Handles the parsed command line arguments and executes the corresponding actions. */ + public void handleCommandLine() { + /* Display command usage. */ + if (line.hasOption(GravitinoOptions.HELP)) { + GravitinoCommandLine.displayHelp(options); + } + /* Display Gravitino version. */ + else if (line.hasOption(GravitinoOptions.VERSION)) { + new ClientVersion(getUrl()).handle(); + } else if (line.hasOption(GravitinoOptions.SERVER)) { + new ServerVersion(getUrl()).handle(); + } else { + executeCommand(); + } + } + + /** + * Displays the help message for the command line tool. + * + * @param options The command options. + */ + public static void displayHelp(Options options) { + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp(CMD, options); + } + + /** Executes the appropriate command based on the command type. */ + private void executeCommand() { + if (entity != null) { + if (entity.equals(CommandEntities.TABLE)) { + handleTableCommand(); + } else if (entity.equals(CommandEntities.SCHEMA)) { + handleSchemaCommand(); + } else if (entity.equals(CommandEntities.CATALOG)) { + handleCatalogCommand(); + } else if (entity.equals(CommandEntities.METALAKE)) { + handleMetalakeCommand(); + } + } else { + handleGeneralCommand(); + } + } + + /** + * Handles the command execution for Metalakes based on command type and the command line options. + */ + protected void handleMetalakeCommand() { Review Comment: Why do we make this method and below `protected`? ########## clients/cli/src/main/java/org/apache/gravitino/cli/commands/Command.java: ########## @@ -0,0 +1,60 @@ +/* + * 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. + */ + +package org.apache.gravitino.cli.commands; + +import org.apache.gravitino.client.GravitinoAdminClient; +import org.apache.gravitino.client.GravitinoClient; +import org.apache.gravitino.exceptions.NoSuchMetalakeException; + +/* The base for all commands. */ +public abstract class Command { + private final String url; + + /** + * Command constructor. + * + * @param url The URL of the Gravitino server. + */ + protected Command(String url) { + this.url = url; + } + /** All commands have a handle method to handle and run the required command. */ + public abstract void handle(); + + /** + * Builds a {@link GravitinoClient} instance with the provided server URL and metalake. + * + * @param metalake The name of the metalake. + * @return A configured {@link GravitinoClient} instance. + * @throws NoSuchMetalakeException if the specified metalake does not exist. + */ + protected GravitinoClient buildClient(String metalake) throws NoSuchMetalakeException { + return GravitinoClient.builder(url).withMetalake(metalake).withVersionCheckDisabled().build(); Review Comment: Why do we disable check version? -- 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]
