-
Notifications
You must be signed in to change notification settings - Fork 406
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
[#5746] improve(CLI): Support table format output for Audit command #6461
Changes from all commits
176eeff
8d8ed0a
128cafe
663c338
fa90f4f
6aa2b39
3efd4b5
484b6b8
64fda51
256e4df
80eaca6
2985aff
950cf34
98c3f43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,8 @@ public class CommandContext { | |
private final boolean quiet; | ||
private final CommandLine line; | ||
private final String auth; | ||
// TODO make it final | ||
private int outputLimit; | ||
|
||
private String ignoreEnv; | ||
private boolean ignoreSet = false; | ||
|
@@ -55,6 +57,7 @@ public CommandContext(CommandLine line) { | |
? line.getOptionValue(GravitinoOptions.OUTPUT) | ||
: Command.OUTPUT_FORMAT_PLAIN; | ||
this.quiet = line.hasOption(GravitinoOptions.QUIET); | ||
this.outputLimit = -1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's unclear to me what this is for - perhaps it needs a better name or better still in another PR |
||
|
||
this.url = getUrl(); | ||
this.ignoreVersions = getIgnore(); | ||
|
@@ -106,6 +109,15 @@ public boolean quiet() { | |
return quiet; | ||
} | ||
|
||
/** | ||
* Returns the output limit. | ||
* | ||
* @return The output limit. | ||
*/ | ||
public int outputLimit() { | ||
return outputLimit; | ||
} | ||
|
||
/** | ||
* Returns the authentication type. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* 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.outputs; | ||
|
||
import com.google.common.base.Preconditions; | ||
import java.io.BufferedOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.PrintStream; | ||
import java.io.UncheckedIOException; | ||
import java.nio.charset.StandardCharsets; | ||
import org.apache.gravitino.cli.CommandContext; | ||
|
||
/** | ||
* Abstract base implementation of {@link OutputFormat} interface providing common functionality for | ||
* various output format implementations. This class handles basic output operations and provides | ||
* configurable behavior for quiet mode, output limiting. | ||
*/ | ||
public abstract class BaseOutputFormat<T> implements OutputFormat<T> { | ||
protected int limit; | ||
protected CommandContext context; | ||
|
||
/** | ||
* Creates a new {@link BaseOutputFormat} with specified configuration. | ||
* | ||
* @param context the command context, must not be null; | ||
*/ | ||
public BaseOutputFormat(CommandContext context) { | ||
Preconditions.checkNotNull(context, "CommandContext cannot be null"); | ||
Abyss-lord marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.context = context; | ||
this.limit = context.outputLimit(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am worried that too many schemas or tables will affect the display |
||
} | ||
|
||
/** | ||
* Outputs a message to the specified OutputStream. This method handles both system streams | ||
* ({@code System.out}, {@code System.err}) and regular output streams differently: - For system | ||
* streams: Preserves the stream open after writing - For other streams: Automatically closes the | ||
* stream after writing | ||
* | ||
* @param message the message to output, must not be null | ||
* @param os the output stream to write to, must not be null If this is {@code System.out} or | ||
* {@code System.err}, the stream will not be closed | ||
* @throws IllegalArgumentException if either message or os is null | ||
* @throws UncheckedIOException if an I/O error occurs during writing | ||
*/ | ||
public static void output(String message, OutputStream os) { | ||
if (message == null || os == null) { | ||
throw new IllegalArgumentException( | ||
"Message and OutputStream cannot be null, message: " + message + ", os: " + os); | ||
} | ||
boolean isSystemStream = (os == System.out || os == System.err); | ||
|
||
try { | ||
PrintStream printStream = | ||
new PrintStream( | ||
isSystemStream ? os : new BufferedOutputStream(os), | ||
true, | ||
StandardCharsets.UTF_8.name()); | ||
|
||
try { | ||
printStream.println(message); | ||
printStream.flush(); | ||
} finally { | ||
if (!isSystemStream) { | ||
printStream.close(); | ||
} | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Failed to write message to output stream", e); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} This implementation checks the quiet flag and handles null output gracefully. If | ||
* quiet mode is enabled, no output is produced. | ||
*/ | ||
@Override | ||
public void output(T entity) { | ||
String outputMessage = getOutput(entity); | ||
String output = outputMessage == null ? "" : outputMessage; | ||
output(output, System.out); | ||
} | ||
} |
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.
can it be made final?