Skip to content
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

[#6147] improve(CLI): Refactor fileset commands in Gavitino CLI  #6191

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 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 Filesets based on command type and the command line options.
*/
public class FilesetCommandHandler 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 String fileset;

/**
* Constructs a {@link FilesetCommandHandler} 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 FilesetCommandHandler(
GravitinoCommandLine gravitinoCommandLine, CommandLine line, String command, boolean ignore) {
this.gravitinoCommandLine = gravitinoCommandLine;
this.line = line;
this.command = command;
this.ignore = ignore;

this.url = gravitinoCommandLine.getUrl();
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
this.catalog = name.getCatalogName();
this.schema = name.getSchemaName();
}

/** Handles the command execution logic based on the provided command. */
@Override
protected void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(gravitinoCommandLine.getAuth(), userName);

List<String> missingEntities = Lists.newArrayList();
if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
if (schema == null) missingEntities.add(CommandEntities.SCHEMA);

if (CommandActions.LIST.equals(command)) {
checkEntities(missingEntities);
handleListCommand();
return;
}

this.fileset = name.getFilesetName();
if (fileset == null) missingEntities.add(CommandEntities.FILESET);
checkEntities(missingEntities);

if (!executeCommand()) {
System.err.println(ErrorMessages.UNSUPPORTED_ACTION);
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.SET:
handleSetCommand();
return true;

case CommandActions.REMOVE:
handleRemoveCommand();
return true;

case CommandActions.PROPERTIES:
handlePropertiesCommand();
return true;

case CommandActions.UPDATE:
handleUpdateCommand();
return true;

default:
return false;
}
}

/** Handles the "DETAILS" command. */
private void handleDetailsCommand() {
gravitinoCommandLine
.newFilesetDetails(url, ignore, metalake, catalog, schema, fileset)
.validate()
.handle();
}

/** Handles the "CREATE" command. */
private void handleCreateCommand() {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
String[] properties = line.getOptionValues(CommandActions.PROPERTIES);
Map<String, String> propertyMap = new Properties().parse(properties);
gravitinoCommandLine
.newCreateFileset(url, ignore, metalake, catalog, schema, fileset, comment, propertyMap)
.validate()
.handle();
}

/** Handles the "DELETE" command. */
private void handleDeleteCommand() {
boolean force = line.hasOption(GravitinoOptions.FORCE);
gravitinoCommandLine
.newDeleteFileset(url, ignore, force, metalake, catalog, schema, fileset)
.validate()
.handle();
}

/** Handles the "SET" command. */
private void handleSetCommand() {
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
String value = line.getOptionValue(GravitinoOptions.VALUE);
gravitinoCommandLine
.newSetFilesetProperty(url, ignore, metalake, catalog, schema, fileset, property, value)
.validate()
.handle();
}

/** Handles the "REMOVE" command. */
private void handleRemoveCommand() {
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
gravitinoCommandLine
.newRemoveFilesetProperty(url, ignore, metalake, catalog, schema, fileset, property)
.validate()
.handle();
}

/** Handles the "PROPERTIES" command. */
private void handlePropertiesCommand() {
gravitinoCommandLine
.newListFilesetProperties(url, ignore, metalake, catalog, schema, fileset)
.validate()
.handle();
}

/** Handles the "LIST" command. */
private void handleListCommand() {
gravitinoCommandLine
.newListFilesets(url, ignore, metalake, catalog, schema)
.validate()
.handle();
}

/** Handles the "UPDATE" command. */
private void handleUpdateCommand() {
if (line.hasOption(GravitinoOptions.COMMENT)) {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
gravitinoCommandLine
.newUpdateFilesetComment(url, ignore, metalake, catalog, schema, fileset, comment)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.RENAME)) {
String newName = line.getOptionValue(GravitinoOptions.RENAME);
gravitinoCommandLine
.newUpdateFilesetName(url, ignore, metalake, catalog, schema, fileset, newName)
.validate()
.handle();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
package org.apache.gravitino.cli;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
Expand Down Expand Up @@ -141,7 +139,7 @@ private void executeCommand() {
} else if (entity.equals(CommandEntities.TOPIC)) {
new TopicCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.FILESET)) {
handleFilesetCommand();
new FilesetCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.USER)) {
new UserCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.GROUP)) {
Expand Down Expand Up @@ -358,108 +356,6 @@ private void handleHelpCommand() {
}
}

/**
* Handles the command execution for filesets based on command type and the command line options.
*/
private void handleFilesetCommand() {
String url = getUrl();
String auth = getAuth();
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
FullName name = new FullName(line);
String metalake = name.getMetalakeName();
String catalog = name.getCatalogName();
String schema = name.getSchemaName();

Command.setAuthenticationMode(auth, 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 `fileset`
if (CommandActions.LIST.equals(command)) {
checkEntities(missingEntities);
newListFilesets(url, ignore, metalake, catalog, schema).validate().handle();
return;
}

String fileset = name.getFilesetName();
if (fileset == null) missingEntities.add(CommandEntities.FILESET);
checkEntities(missingEntities);

switch (command) {
case CommandActions.DETAILS:
newFilesetDetails(url, ignore, metalake, catalog, schema, fileset).validate().handle();
break;

case CommandActions.CREATE:
{
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
String[] properties = line.getOptionValues(CommandActions.PROPERTIES);
Map<String, String> propertyMap = new Properties().parse(properties);
newCreateFileset(url, ignore, metalake, catalog, schema, fileset, comment, propertyMap)
.validate()
.handle();
break;
}

case CommandActions.DELETE:
{
boolean force = line.hasOption(GravitinoOptions.FORCE);
newDeleteFileset(url, ignore, force, metalake, catalog, schema, fileset)
.validate()
.handle();
break;
}

case CommandActions.SET:
{
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
String value = line.getOptionValue(GravitinoOptions.VALUE);
newSetFilesetProperty(url, ignore, metalake, catalog, schema, fileset, property, value)
.validate()
.handle();
break;
}

case CommandActions.REMOVE:
{
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
newRemoveFilesetProperty(url, ignore, metalake, catalog, schema, fileset, property)
.validate()
.handle();
break;
}

case CommandActions.PROPERTIES:
newListFilesetProperties(url, ignore, metalake, catalog, schema, fileset)
.validate()
.handle();
break;

case CommandActions.UPDATE:
{
if (line.hasOption(GravitinoOptions.COMMENT)) {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
newUpdateFilesetComment(url, ignore, metalake, catalog, schema, fileset, comment)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.RENAME)) {
String newName = line.getOptionValue(GravitinoOptions.RENAME);
newUpdateFilesetName(url, ignore, metalake, catalog, schema, fileset, newName)
.validate()
.handle();
}
break;
}

default:
System.err.println(ErrorMessages.UNSUPPORTED_ACTION);
break;
}
}

/**
* Retrieves the Gravitinno URL from the command line options or the GRAVITINO_URL environment
* variable or the Gravitio config file.
Expand Down
Loading