-
Notifications
You must be signed in to change notification settings - Fork 384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#6148] improve(CLI): Refactor model commands in Gavitino CLI #6162
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* 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 com.google.common.base.Joiner; | ||
import java.util.List; | ||
import org.apache.commons.cli.CommandLine; | ||
|
||
public abstract class CommandHandler { | ||
public static final Joiner COMMA_JOINER = Joiner.on(", ").skipNulls(); | ||
|
||
public static final String DEFAULT_URL = "http://localhost:8090"; | ||
|
||
private String urlEnv; | ||
private boolean urlSet = false; | ||
private String authEnv; | ||
private boolean authSet = false; | ||
|
||
/** | ||
* Retrieves the Gravitinno URL from the command line options or the GRAVITINO_URL environment | ||
* variable or the Gravitio config file. | ||
* | ||
* @param line The command line instance. | ||
* @return The Gravitinno URL, or null if not found. | ||
*/ | ||
public String getUrl(CommandLine line) { | ||
GravitinoConfig config = new GravitinoConfig(null); | ||
|
||
// If specified on the command line use that | ||
if (line.hasOption(GravitinoOptions.URL)) { | ||
return line.getOptionValue(GravitinoOptions.URL); | ||
} | ||
|
||
// Cache the Gravitino URL environment variable | ||
if (urlEnv == null && !urlSet) { | ||
urlEnv = System.getenv("GRAVITINO_URL"); | ||
urlSet = true; | ||
} | ||
|
||
// If set return the Gravitino URL environment variable | ||
if (urlEnv != null) { | ||
return urlEnv; | ||
} | ||
|
||
// Check if the Gravitino URL is specified in the configuration file | ||
if (config.fileExists()) { | ||
config.read(); | ||
String configURL = config.getGravitinoURL(); | ||
if (configURL != null) { | ||
return configURL; | ||
} | ||
} | ||
|
||
// Return the default localhost URL | ||
return DEFAULT_URL; | ||
} | ||
|
||
/** | ||
* Retrieves the Gravitinno authentication from the command line options or the GRAVITINO_AUTH | ||
* environment variable or the Gravitio config file. | ||
* | ||
* @param line The command line instance. | ||
* @return The Gravitinno authentication, or null if not found. | ||
*/ | ||
public String getAuth(CommandLine line) { | ||
// If specified on the command line use that | ||
if (line.hasOption(GravitinoOptions.SIMPLE)) { | ||
return GravitinoOptions.SIMPLE; | ||
} | ||
|
||
// Cache the Gravitino authentication type environment variable | ||
if (authEnv == null && !authSet) { | ||
authEnv = System.getenv("GRAVITINO_AUTH"); | ||
authSet = true; | ||
} | ||
|
||
// If set return the Gravitino authentication type environment variable | ||
if (authEnv != null) { | ||
return authEnv; | ||
} | ||
|
||
// Check if the authentication type is specified in the configuration file | ||
GravitinoConfig config = new GravitinoConfig(null); | ||
if (config.fileExists()) { | ||
config.read(); | ||
String configAuthType = config.getGravitinoAuthType(); | ||
if (configAuthType != null) { | ||
return configAuthType; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected abstract void handle(); | ||
|
||
protected void checkEntities(List<String> entities) { | ||
if (!entities.isEmpty()) { | ||
System.err.println(ErrorMessages.MISSING_ENTITIES + COMMA_JOINER.join(entities)); | ||
Main.exit(-1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
clients/cli/src/main/java/org/apache/gravitino/cli/ModelCommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* | ||
* 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 com.google.common.collect.Lists; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.gravitino.cli.commands.Command; | ||
|
||
/** Handles the command execution for Models based on command type and the command line options. */ | ||
public class ModelCommandHandler extends CommandHandler { | ||
private final GravitinoCommandLine gravitinoCommandLine; | ||
private final CommandLine line; | ||
private final String command; | ||
private final boolean ignore; | ||
private final String url; | ||
private final FullName name; | ||
private final String metalake; | ||
private final String catalog; | ||
private final String schema; | ||
private final String model; | ||
|
||
/** | ||
* Constructs a {@link ModelCommandHandler} instance. | ||
* | ||
* @param gravitinoCommandLine The Gravitino command line instance. | ||
* @param line The command line arguments. | ||
* @param command The command to execute. | ||
* @param ignore Ignore server version mismatch. | ||
*/ | ||
public ModelCommandHandler( | ||
GravitinoCommandLine gravitinoCommandLine, CommandLine line, String command, boolean ignore) { | ||
this.gravitinoCommandLine = gravitinoCommandLine; | ||
this.line = line; | ||
this.command = command; | ||
this.ignore = ignore; | ||
|
||
this.url = getUrl(line); | ||
this.name = new FullName(line); | ||
this.metalake = name.getMetalakeName(); | ||
this.catalog = name.getCatalogName(); | ||
this.schema = name.getSchemaName(); | ||
this.model = name.getModelName(); | ||
} | ||
|
||
/** Handles the command execution logic based on the provided command. */ | ||
@Override | ||
protected void handle() { | ||
String userName = line.getOptionValue(GravitinoOptions.LOGIN); | ||
Command.setAuthenticationMode(getAuth(line), userName); | ||
|
||
List<String> missingEntities = Lists.newArrayList(); | ||
if (catalog == null) missingEntities.add(CommandEntities.CATALOG); | ||
if (schema == null) missingEntities.add(CommandEntities.SCHEMA); | ||
|
||
// Handle CommandActions.LIST action separately as it doesn't require the `model` | ||
if (CommandActions.LIST.equals(command)) { | ||
checkEntities(missingEntities); | ||
handleListCommand(); | ||
return; | ||
} | ||
|
||
String model = name.getModelName(); | ||
Abyss-lord marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (model == null) missingEntities.add(CommandEntities.MODEL); | ||
checkEntities(missingEntities); | ||
|
||
if (!executeCommand()) { | ||
System.err.println(ErrorMessages.UNSUPPORTED_COMMAND); | ||
Main.exit(-1); | ||
} | ||
} | ||
|
||
/** | ||
* Executes the specific command based on the command type. | ||
* | ||
* @return true if the command is supported, false otherwise | ||
*/ | ||
private boolean executeCommand() { | ||
switch (command) { | ||
case CommandActions.DETAILS: | ||
handleDetailsCommand(); | ||
return true; | ||
|
||
case CommandActions.CREATE: | ||
handleCreateCommand(); | ||
return true; | ||
|
||
case CommandActions.DELETE: | ||
handleDeleteCommand(); | ||
return true; | ||
|
||
case CommandActions.UPDATE: | ||
handleUpdateCommand(); | ||
return true; | ||
|
||
default: | ||
return false; | ||
} | ||
} | ||
|
||
/** Handles the "DETAILS" command. */ | ||
private void handleDetailsCommand() { | ||
if (line.hasOption(GravitinoOptions.AUDIT)) { | ||
gravitinoCommandLine | ||
.newModelAudit(url, ignore, metalake, catalog, schema, model) | ||
.validate() | ||
.handle(); | ||
} else { | ||
gravitinoCommandLine | ||
.newModelDetails(url, ignore, metalake, catalog, schema, model) | ||
.validate() | ||
.handle(); | ||
} | ||
} | ||
|
||
/** Handles the "CREATE" command. */ | ||
private void handleCreateCommand() { | ||
String createComment = line.getOptionValue(GravitinoOptions.COMMENT); | ||
String[] createProperties = line.getOptionValues(GravitinoOptions.PROPERTIES); | ||
Map<String, String> createPropertyMap = new Properties().parse(createProperties); | ||
gravitinoCommandLine | ||
.newCreateModel( | ||
url, ignore, metalake, catalog, schema, model, createComment, createPropertyMap) | ||
.validate() | ||
.handle(); | ||
} | ||
|
||
/** Handles the "DELETE" command. */ | ||
private void handleDeleteCommand() { | ||
boolean force = line.hasOption(GravitinoOptions.FORCE); | ||
gravitinoCommandLine | ||
.newDeleteModel(url, ignore, force, metalake, catalog, schema, model) | ||
.validate() | ||
.handle(); | ||
} | ||
|
||
/** Handles the "UPDATE" command. */ | ||
private void handleUpdateCommand() { | ||
String[] alias = line.getOptionValues(GravitinoOptions.ALIAS); | ||
String uri = line.getOptionValue(GravitinoOptions.URI); | ||
String linkComment = line.getOptionValue(GravitinoOptions.COMMENT); | ||
String[] linkProperties = line.getOptionValues(CommandActions.PROPERTIES); | ||
Map<String, String> linkPropertityMap = new Properties().parse(linkProperties); | ||
gravitinoCommandLine | ||
.newLinkModel( | ||
url, | ||
ignore, | ||
metalake, | ||
catalog, | ||
schema, | ||
model, | ||
uri, | ||
alias, | ||
linkComment, | ||
linkPropertityMap) | ||
.validate() | ||
.handle(); | ||
} | ||
|
||
/** Handles the "LIST" command. */ | ||
private void handleListCommand() { | ||
gravitinoCommandLine.newListModel(url, ignore, metalake, catalog, schema).validate().handle(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is likely to cause an issue with the list command.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getModelName
will return null ifnames.length <= position
, list command is executed first, IMHO, It should be retrieved in the constructor.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even so, you should not call it if it is not needed. It will put out an error message saying the name is malformed - we don't want this as it will confuse the user. I'd remove the final and set it after the list command call.