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

[#6220] improve(CLI): Clean up GravitinoCommandLine class now it been refactored #6227

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Changes from 1 commit
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
Next Next commit
[#6220] improve(CLI): Clean up GravitinoCommandLine class now it been…
… refactored

1. Clean up GravitinoCommandLine class.
2. add SimpleCommandHandler to handle simple commands.
  • Loading branch information
Abyss-lord committed Jan 14, 2025
commit 3c6bdb64f1d41e6c7726928c82aea90f5f5c85bd
Original file line number Diff line number Diff line change
@@ -53,7 +53,7 @@ public ColumnCommandHandler(
this.command = command;
this.ignore = ignore;

this.url = gravitinoCommandLine.getUrl();
this.url = getUrl(line);
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
this.catalog = name.getCatalogName();
@@ -65,7 +65,7 @@ public ColumnCommandHandler(
@Override
protected void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(gravitinoCommandLine.getAuth(), userName);
Command.setAuthenticationMode(getAuth(line), userName);

List<String> missingEntities = Lists.newArrayList();
if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ public FilesetCommandHandler(
this.command = command;
this.ignore = ignore;

this.url = gravitinoCommandLine.getUrl();
this.url = getUrl(line);
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
this.catalog = name.getCatalogName();
@@ -65,7 +65,7 @@ public FilesetCommandHandler(
@Override
protected void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(gravitinoCommandLine.getAuth(), userName);
Command.setAuthenticationMode(getAuth(line), userName);

List<String> missingEntities = Lists.newArrayList();
if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
Original file line number Diff line number Diff line change
@@ -19,13 +19,11 @@

package org.apache.gravitino.cli;

import com.google.common.base.Joiner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
@@ -37,18 +35,13 @@ public class GravitinoCommandLine extends TestableCommandLine {
private final Options options;
private final String entity;
private final String command;
private String urlEnv;
private boolean urlSet = false;
private boolean ignore = false;
private String ignoreEnv;
private boolean ignoreSet = false;
private String authEnv;
private boolean authSet = false;

public static final String CMD = "gcli"; // recommended name
public static final String DEFAULT_URL = "http://localhost:8090";
// This joiner is used to join multiple outputs to be displayed, e.g. roles or groups
private static final Joiner COMMA_JOINER = Joiner.on(", ").skipNulls();

/**
* Gravitino Command line.
@@ -98,14 +91,7 @@ public void handleSimpleLine() {
if (line.hasOption(GravitinoOptions.HELP)) {
displayHelp(options);
}
/* Display Gravitino client version. */
else if (line.hasOption(GravitinoOptions.VERSION)) {
newClientVersion(getUrl(), ignore).handle();
}
/* Display Gravitino server version. */
else if (line.hasOption(GravitinoOptions.SERVER)) {
newServerVersion(getUrl(), ignore).handle();
}
new SimpleCommandHandler(this, line, ignore).handle();
Abyss-lord marked this conversation as resolved.
Show resolved Hide resolved
}

/**
@@ -168,85 +154,4 @@ private void handleHelpCommand() {
Main.exit(-1);
}
}

/**
* Retrieves the Gravitinno URL from the command line options or the GRAVITINO_URL environment
* variable or the Gravitio config file.
*
* @return The Gravitinno URL, or null if not found.
*/
public String getUrl() {
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.
*
* @return The Gravitinno authentication, or null if not found.
*/
public String getAuth() {
// 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;
}

private void checkEntities(List<String> entities) {
if (!entities.isEmpty()) {
System.err.println(ErrorMessages.MISSING_ENTITIES + COMMA_JOINER.join(entities));
Main.exit(-1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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;

/** Handles the command execution for simple command based on the command line options. */
public class SimpleCommandHandler extends CommandHandler {
private final GravitinoCommandLine gravitinoCommandLine;
private final CommandLine line;
private final boolean ignore;

/**
* Constructs a {@link SimpleCommandHandler} instance.
*
* @param gravitinoCommandLine The Gravitino command line instance.
* @param line The command line arguments.
* @param ignore Ignore server version mismatch.
*/
public SimpleCommandHandler(
justinmclean marked this conversation as resolved.
Show resolved Hide resolved
GravitinoCommandLine gravitinoCommandLine, CommandLine line, boolean ignore) {
this.gravitinoCommandLine = gravitinoCommandLine;
this.line = line;
this.ignore = ignore;
}

/** Handles the command execution logic based on the provided command. */
@Override
protected void handle() {
if (line.hasOption(GravitinoOptions.VERSION)) {
gravitinoCommandLine.newClientVersion(getUrl(line), ignore);
} else if (line.hasOption(GravitinoOptions.SERVER)) {
gravitinoCommandLine.newServerVersion(getUrl(line), ignore);
}
}
}