-
Notifications
You must be signed in to change notification settings - Fork 488
Unified --Json output #6419
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
Open
ArbaazKhan1
wants to merge
9
commits into
apache:main
Choose a base branch
from
ArbaazKhan1:accumulo-6391
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+256
−28
Open
Unified --Json output #6419
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6297333
new envelope format for json output
ArbaazKhan1 9e8d477
fixed typo
ArbaazKhan1 bac6e17
fixed failing tests
ArbaazKhan1 6e556f5
added error()
ArbaazKhan1 9d9f301
removed System.exit
ArbaazKhan1 f08ddb9
Merge branch 'apache:main' into accumulo-6391
ArbaazKhan1 92b9944
Merge branch 'apache:main' into accumulo-6391
ArbaazKhan1 0d10159
added exception throw
ArbaazKhan1 0958d8b
Added changes
ArbaazKhan1 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
130 changes: 130 additions & 0 deletions
130
core/src/main/java/org/apache/accumulo/core/cli/CommandOutputEnvelope.java
This file contains hidden or 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,130 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * https://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.accumulo.core.cli; | ||
|
|
||
| import java.time.ZoneId; | ||
| import java.time.ZonedDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| import com.google.gson.Gson; | ||
|
|
||
| /** | ||
| * A stable, versioned outer wrapper for all admin command JSON output. | ||
| * | ||
| * <p> | ||
| * Every command that supports --json output wraps its command-specific data in this envelope. This | ||
| * provides a consistent structure that scripts can rely on regardless of which command produced the | ||
| * output: | ||
| * | ||
| * <pre> | ||
| * { | ||
| * "command": "accumulo admin fate --summary", | ||
| * "version": "1", | ||
| * "reportTime": "2026-06-04T12:00:00Z", | ||
| * "status": "OK" | ||
| * }, | ||
| * "output": { ... command-specific payload... } | ||
| * } | ||
| * </pre> | ||
| * | ||
| * <p> | ||
| * The {@link CommandStatus#version} field is a stability contract. When a breaking change is made | ||
| * to the envelope structure, the version will be incremented. Scripts should check this field and | ||
| * handle the version they were written against. | ||
| * | ||
| */ | ||
| public class CommandOutputEnvelope { | ||
|
|
||
| /** | ||
| * Current envelop schema version. Increment this if a breaking structural change is made to the | ||
| * envelope fields (not to the {@code output} field, data changes command specific). | ||
| */ | ||
| public static final String VERSION = "1.0"; | ||
| private static final DateTimeFormatter ISO_FMT = DateTimeFormatter.ISO_OFFSET_DATE_TIME; | ||
| private static final Gson PRETTY_GSON = | ||
| new Gson().newBuilder().setPrettyPrinting().disableJdkUnsafe().create(); | ||
|
|
||
| public static class CommandStatus { | ||
| private String command; | ||
| private String version; | ||
| private String reportTime; | ||
| private String statusMessage; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| private CommandStatus() {} | ||
|
|
||
| private CommandStatus(String command, String statusMessage) { | ||
| this.command = command; | ||
| this.version = VERSION; | ||
| this.reportTime = ISO_FMT.format(ZonedDateTime.now(ZoneId.systemDefault())); | ||
| this.statusMessage = statusMessage; | ||
| } | ||
|
|
||
| public String getCommand() { | ||
| return command; | ||
| } | ||
|
|
||
| public String getVersion() { | ||
| return version; | ||
| } | ||
|
|
||
| public String getReportTime() { | ||
| return reportTime; | ||
| } | ||
|
|
||
| public String getStatusMessage() { | ||
| return statusMessage; | ||
| } | ||
| } | ||
|
|
||
| private CommandStatus status; | ||
| private Object output; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| private CommandOutputEnvelope() {} | ||
|
|
||
| private CommandOutputEnvelope(String command, String statusMessage, Object output) { | ||
| this.status = new CommandStatus(command, statusMessage); | ||
| this.output = output; | ||
| } | ||
|
|
||
| public static CommandOutputEnvelope of(String command, Object data) { | ||
| return new CommandOutputEnvelope(command, "OK", data); | ||
| } | ||
|
|
||
| public static CommandOutputEnvelope error(String command, String message) { | ||
| return new CommandOutputEnvelope(command, "ERROR" + message, null); | ||
| } | ||
|
|
||
| public String toJson() { | ||
| return PRETTY_GSON.toJson(this); | ||
| } | ||
|
|
||
| public static CommandOutputEnvelope fromJson(String json) { | ||
| return PRETTY_GSON.fromJson(json, CommandOutputEnvelope.class); | ||
| } | ||
|
|
||
| public CommandStatus getStatus() { | ||
| return status; | ||
| } | ||
|
|
||
| public Object getOutput() { | ||
| return output; | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
core/src/main/java/org/apache/accumulo/core/cli/CommandReport.java
This file contains hidden or 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,51 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * https://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.accumulo.core.cli; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Implemented by all command report classes that support both human-readable and computer readable | ||
| * outputs. | ||
| * <p> | ||
| * Json output is always wrapped in a {@link CommandOutputEnvelope} to provide a stable, versioned | ||
| * outer structure that scripts can depend on, regardless of which command is used. | ||
| * | ||
| * <p> | ||
| * Usage pattern is a command's execute() method: | ||
| * | ||
| * <pre> | ||
| * CommandReport report = buildReport(context, options); | ||
| * if (options.json()) { | ||
| * System.out.println(report.toEnvelopedJson("accumulo admin 'my-command'")); | ||
| * } else { | ||
| * report.formatLines().forEach(System.out::println); | ||
| * } | ||
| * </pre> | ||
| */ | ||
| public interface CommandReport { | ||
| List<String> formatLines(); | ||
|
|
||
| Object getData(); | ||
|
|
||
| default String toEnvelopedJson(String commandName) { | ||
| return CommandOutputEnvelope.of(commandName, getData()).toJson(); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
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.
Searched the project and the ListCompactions command has this
-j/--jsonoption already. Should probably remove that and let it read the inheritedoptions.jsoninstead