Skip to content

Commit 7c3aaed

Browse files
committed
[#6424] improve(CLI): Refactor getURL in CLI and add context to simple commands. (#6440)
### What changes were proposed in this pull request? Refactor getURL in CLI and add context to simple commands. ### Why are the changes needed? Fix: #6424 ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? local test.
1 parent 02d1b39 commit 7c3aaed

23 files changed

+150
-158
lines changed

clients/cli/src/main/java/org/apache/gravitino/cli/CatalogCommandHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public CatalogCommandHandler(
5656
this.command = command;
5757
this.context = context;
5858

59-
this.context.setUrl(getUrl(line));
6059
this.name = new FullName(line);
6160
this.metalake = name.getMetalakeName();
6261
}

clients/cli/src/main/java/org/apache/gravitino/cli/ColumnCommandHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public ColumnCommandHandler(
5555
this.command = command;
5656
this.context = context;
5757

58-
this.context.setUrl(getUrl(line));
5958
this.name = new FullName(line);
6059
this.metalake = name.getMetalakeName();
6160
this.catalog = name.getCatalogName();

clients/cli/src/main/java/org/apache/gravitino/cli/CommandContext.java

+88-34
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,40 @@
1919

2020
package org.apache.gravitino.cli;
2121

22+
import com.google.common.base.Preconditions;
23+
import org.apache.commons.cli.CommandLine;
2224
import org.apache.gravitino.cli.commands.Command;
2325

2426
/* Context for a command */
2527
public class CommandContext {
26-
private String url;
27-
private boolean ignoreVersions;
28-
private boolean force;
29-
private String outputFormat;
28+
private final boolean force;
29+
private final boolean ignoreVersions;
30+
private final String outputFormat;
31+
private final String url;
32+
private final CommandLine line;
33+
34+
private String ignoreEnv;
35+
private boolean ignoreSet = false;
36+
private String urlEnv;
37+
private boolean urlSet = false;
3038
// Can add more "global" command flags here without any major changes e.g. a guiet flag
3139

3240
/**
3341
* Command constructor.
3442
*
35-
* @param url The URL of the Gravitino server.
36-
* @param ignoreVersions If true don't check the client/server versions match.
43+
* @param line The command line.
3744
*/
38-
public CommandContext(String url, boolean ignoreVersions) {
39-
this.url = url;
40-
this.ignoreVersions = ignoreVersions;
41-
this.force = false;
42-
this.outputFormat = Command.OUTPUT_FORMAT_PLAIN;
43-
}
45+
public CommandContext(CommandLine line) {
46+
Preconditions.checkNotNull(line);
47+
this.line = line;
48+
this.force = line.hasOption(GravitinoOptions.FORCE);
49+
this.outputFormat =
50+
line.hasOption(GravitinoOptions.OUTPUT)
51+
? line.getOptionValue(GravitinoOptions.OUTPUT)
52+
: Command.OUTPUT_FORMAT_PLAIN;
4453

45-
/**
46-
* Command constructor.
47-
*
48-
* @param url The URL of the Gravitino server.
49-
* @param ignoreVersions If true don't check the client/server versions match.
50-
* @param force Force operation.
51-
* @param outputFormat Display output format.
52-
*/
53-
public CommandContext(String url, boolean ignoreVersions, boolean force, String outputFormat) {
54-
this.url = url;
55-
this.ignoreVersions = ignoreVersions;
56-
this.force = force;
57-
this.outputFormat = outputFormat;
54+
this.url = getUrl();
55+
this.ignoreVersions = getIgnore();
5856
}
5957

6058
/**
@@ -66,15 +64,6 @@ public String url() {
6664
return url;
6765
}
6866

69-
/**
70-
* Sets the URL.
71-
*
72-
* @param url The URL to be set.
73-
*/
74-
public void setUrl(String url) {
75-
this.url = url;
76-
}
77-
7867
/**
7968
* Indicates whether versions should be ignored.
8069
*
@@ -101,4 +90,69 @@ public boolean force() {
10190
public String outputFormat() {
10291
return outputFormat;
10392
}
93+
94+
/**
95+
* Retrieves the Gravitino URL from the command line options or the GRAVITINO_URL environment
96+
* variable or the Gravitino config file.
97+
*
98+
* @return The Gravitino URL, or null if not found.
99+
*/
100+
private String getUrl() {
101+
GravitinoConfig config = new GravitinoConfig(null);
102+
103+
// If specified on the command line use that
104+
if (line.hasOption(GravitinoOptions.URL)) {
105+
return line.getOptionValue(GravitinoOptions.URL);
106+
}
107+
108+
// Cache the Gravitino URL environment variable
109+
if (urlEnv == null && !urlSet) {
110+
urlEnv = System.getenv("GRAVITINO_URL");
111+
urlSet = true;
112+
}
113+
114+
// If set return the Gravitino URL environment variable
115+
if (urlEnv != null) {
116+
return urlEnv;
117+
}
118+
119+
// Check if the Gravitino URL is specified in the configuration file
120+
if (config.fileExists()) {
121+
config.read();
122+
String configURL = config.getGravitinoURL();
123+
if (configURL != null) {
124+
return configURL;
125+
}
126+
}
127+
128+
// Return the default localhost URL
129+
return GravitinoCommandLine.DEFAULT_URL;
130+
}
131+
132+
private boolean getIgnore() {
133+
GravitinoConfig config = new GravitinoConfig(null);
134+
boolean ignore = false;
135+
136+
/* Check if you should ignore client/version versions */
137+
if (line.hasOption(GravitinoOptions.IGNORE)) {
138+
ignore = true;
139+
} else {
140+
// Cache the ignore environment variable
141+
if (ignoreEnv == null && !ignoreSet) {
142+
ignoreEnv = System.getenv("GRAVITINO_IGNORE");
143+
ignore = ignoreEnv != null && ignoreEnv.equals("true");
144+
ignoreSet = true;
145+
}
146+
147+
// Check if the ignore name is specified in the configuration file
148+
if (ignoreEnv == null) {
149+
if (config.fileExists()) {
150+
config.read();
151+
ignore = config.getIgnore();
152+
}
153+
}
154+
}
155+
156+
return ignore;
157+
}
104158
}

clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java

-44
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,9 @@
2525

2626
public abstract class CommandHandler {
2727
public static final Joiner COMMA_JOINER = Joiner.on(", ").skipNulls();
28-
29-
public static final String DEFAULT_URL = "http://localhost:8090";
30-
31-
private String urlEnv;
32-
private boolean urlSet = false;
3328
private String authEnv;
3429
private boolean authSet = false;
3530

36-
/**
37-
* Retrieves the Gravitino URL from the command line options or the GRAVITINO_URL environment
38-
* variable or the Gravitino config file.
39-
*
40-
* @param line The command line instance.
41-
* @return The Gravitino URL, or null if not found.
42-
*/
43-
public String getUrl(CommandLine line) {
44-
GravitinoConfig config = new GravitinoConfig(null);
45-
46-
// If specified on the command line use that
47-
if (line.hasOption(GravitinoOptions.URL)) {
48-
return line.getOptionValue(GravitinoOptions.URL);
49-
}
50-
51-
// Cache the Gravitino URL environment variable
52-
if (urlEnv == null && !urlSet) {
53-
urlEnv = System.getenv("GRAVITINO_URL");
54-
urlSet = true;
55-
}
56-
57-
// If set return the Gravitino URL environment variable
58-
if (urlEnv != null) {
59-
return urlEnv;
60-
}
61-
62-
// Check if the Gravitino URL is specified in the configuration file
63-
if (config.fileExists()) {
64-
config.read();
65-
String configURL = config.getGravitinoURL();
66-
if (configURL != null) {
67-
return configURL;
68-
}
69-
}
70-
71-
// Return the default localhost URL
72-
return DEFAULT_URL;
73-
}
74-
7531
/**
7632
* Retrieves the Gravitino authentication from the command line options or the GRAVITINO_AUTH
7733
* environment variable or the Gravitino config file.

clients/cli/src/main/java/org/apache/gravitino/cli/FilesetCommandHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public FilesetCommandHandler(
5656
this.command = command;
5757
this.context = context;
5858

59-
this.context.setUrl(getUrl(line));
6059
this.name = new FullName(line);
6160
this.metalake = name.getMetalakeName();
6261
this.catalog = name.getCatalogName();

clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java

+5-32
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ public class GravitinoCommandLine extends TestableCommandLine {
3535
private final Options options;
3636
private final String entity;
3737
private final String command;
38-
private boolean ignore = false;
39-
private String ignoreEnv;
40-
private boolean ignoreSet = false;
4138

4239
public static final String CMD = "gcli"; // recommended name
4340
public static final String DEFAULT_URL = "http://localhost:8090";
@@ -60,29 +57,8 @@ public GravitinoCommandLine(CommandLine line, Options options, String entity, St
6057

6158
/** Handles the parsed command line arguments and executes the corresponding actions. */
6259
public void handleCommandLine() {
63-
GravitinoConfig config = new GravitinoConfig(null);
64-
65-
/* Check if you should ignore client/version versions */
66-
if (line.hasOption(GravitinoOptions.IGNORE)) {
67-
ignore = true;
68-
} else {
69-
// Cache the ignore environment variable
70-
if (ignoreEnv == null && !ignoreSet) {
71-
ignoreEnv = System.getenv("GRAVITINO_IGNORE");
72-
ignore = ignoreEnv != null && ignoreEnv.equals("true");
73-
ignoreSet = true;
74-
}
75-
76-
// Check if the ignore name is specified in the configuration file
77-
if (ignoreEnv == null) {
78-
if (config.fileExists()) {
79-
config.read();
80-
ignore = config.getIgnore();
81-
}
82-
}
83-
}
84-
85-
executeCommand();
60+
CommandContext context = new CommandContext(line);
61+
executeCommand(context);
8662
}
8763

8864
/** Handles the parsed command line arguments and executes the corresponding actions. */
@@ -91,7 +67,8 @@ public void handleSimpleLine() {
9167
if (line.hasOption(GravitinoOptions.HELP)) {
9268
displayHelp(options);
9369
} else {
94-
new SimpleCommandHandler(this, line, ignore).handle();
70+
CommandContext context = new CommandContext(line);
71+
new SimpleCommandHandler(this, line, context).handle();
9572
}
9673
}
9774

@@ -106,11 +83,7 @@ public static void displayHelp(Options options) {
10683
}
10784

10885
/** Executes the appropriate command based on the command type. */
109-
private void executeCommand() {
110-
boolean force = line.hasOption(GravitinoOptions.FORCE);
111-
String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT);
112-
CommandContext context = new CommandContext(null, ignore, force, outputFormat);
113-
86+
private void executeCommand(CommandContext context) {
11487
if (CommandActions.HELP.equals(command)) {
11588
handleHelpCommand();
11689
} else if (line.hasOption(GravitinoOptions.OWNER)) {

clients/cli/src/main/java/org/apache/gravitino/cli/GroupCommandHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public GroupCommandHandler(
5050
this.command = command;
5151
this.context = context;
5252

53-
this.context.setUrl(getUrl(line));
5453
this.name = new FullName(line);
5554
this.metalake = name.getMetalakeName();
5655
}

clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public MetalakeCommandHandler(
5050
this.line = line;
5151
this.command = command;
5252
this.context = context;
53-
this.context.setUrl(getUrl(line));
5453
}
5554

5655
/** Handles the command execution logic based on the provided command. */

clients/cli/src/main/java/org/apache/gravitino/cli/ModelCommandHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public ModelCommandHandler(
5555
this.command = command;
5656

5757
this.context = context;
58-
this.context.setUrl(getUrl(line));
5958
this.name = new FullName(line);
6059
this.metalake = name.getMetalakeName();
6160
this.catalog = name.getCatalogName();

clients/cli/src/main/java/org/apache/gravitino/cli/OwnerCommandHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public OwnerCommandHandler(
5555
this.command = command;
5656
this.context = context;
5757

58-
this.context.setUrl(getUrl(line));
5958
this.owner = line.getOptionValue(GravitinoOptions.USER);
6059
this.group = line.getOptionValue(GravitinoOptions.GROUP);
6160
this.name = new FullName(line);

clients/cli/src/main/java/org/apache/gravitino/cli/RoleCommandHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public RoleCommandHandler(
4242
this.line = line;
4343
this.command = command;
4444
this.context = context;
45-
this.context.setUrl(getUrl(line));
4645
}
4746

4847
/** Handles the command execution logic based on the provided command. */

clients/cli/src/main/java/org/apache/gravitino/cli/SchemaCommandHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public SchemaCommandHandler(
5353
this.command = command;
5454
this.context = context;
5555

56-
this.context.setUrl(getUrl(line));
5756
this.name = new FullName(line);
5857
this.metalake = name.getMetalakeName();
5958
this.catalog = name.getCatalogName();

clients/cli/src/main/java/org/apache/gravitino/cli/SimpleCommandHandler.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,29 @@
2525
public class SimpleCommandHandler extends CommandHandler {
2626
private final GravitinoCommandLine gravitinoCommandLine;
2727
private final CommandLine line;
28-
private final boolean ignore;
28+
private final CommandContext context;
2929

3030
/**
3131
* Constructs a {@link SimpleCommandHandler} instance.
3232
*
3333
* @param gravitinoCommandLine The Gravitino command line instance.
3434
* @param line The command line arguments.
35-
* @param ignore Ignore server version mismatch.
35+
* @param context The command context.
3636
*/
3737
public SimpleCommandHandler(
38-
GravitinoCommandLine gravitinoCommandLine, CommandLine line, boolean ignore) {
38+
GravitinoCommandLine gravitinoCommandLine, CommandLine line, CommandContext context) {
3939
this.gravitinoCommandLine = gravitinoCommandLine;
4040
this.line = line;
41-
this.ignore = ignore;
41+
this.context = context;
4242
}
4343

4444
/** Handles the command execution logic based on the provided command. */
4545
@Override
4646
protected void handle() {
4747
if (line.hasOption(GravitinoOptions.VERSION)) {
48-
gravitinoCommandLine.newClientVersion(getUrl(line), ignore).validate().handle();
48+
gravitinoCommandLine.newClientVersion(context).validate().handle();
4949
} else if (line.hasOption(GravitinoOptions.SERVER)) {
50-
gravitinoCommandLine.newServerVersion(getUrl(line), ignore).validate().handle();
50+
gravitinoCommandLine.newServerVersion(context).validate().handle();
5151
}
5252
}
5353
}

clients/cli/src/main/java/org/apache/gravitino/cli/TableCommandHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public TableCommandHandler(
5454
this.command = command;
5555
this.context = context;
5656

57-
this.context.setUrl(getUrl(line));
5857
this.name = new FullName(line);
5958
this.metalake = name.getMetalakeName();
6059
this.catalog = name.getCatalogName();

clients/cli/src/main/java/org/apache/gravitino/cli/TagCommandHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public TagCommandHandler(
3939
this.line = line;
4040
this.command = command;
4141
this.context = context;
42-
this.context.setUrl(getUrl(line));
4342
this.tags = line.getOptionValues(GravitinoOptions.TAG);
4443

4544
if (tags != null) {

0 commit comments

Comments
 (0)