-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-12519. Generate auto-complete script for Ozone commands. #8030
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c0eea12
HDDS-12519. Autocomplete for Ozone commands.
nandakumar131 85b5be5
Top level Options added to autocomplete.
nandakumar131 ab456f4
Version command added to autocomplete.
nandakumar131 6b8c911
httpfs command added to autocomplete.
nandakumar131 bc2aeec
Merge remote-tracking branch 'upstream/master' into HDDS-12519
nandakumar131 0e854df
Typo fixed.
nandakumar131 430bc14
unessary dependency removed.
nandakumar131 36c93c3
Merge remote-tracking branch 'upstream/master' into HDDS-12519
nandakumar131 216e0f5
Java Reflection based loading of commands.
nandakumar131 303ae91
Renamed commandFilter to isPublicCommand
nandakumar131 3dbd059
Merge remote-tracking branch 'upstream/master' into HDDS-12519
nandakumar131 0455000
Merge remote-tracking branch 'upstream' into HDDS-12519
nandakumar131 db30381
Merge remote-tracking branch 'upstream' into HDDS-12519
nandakumar131 82ea1ba
Subcommands bash & zsh added.
nandakumar131 54639bb
Merge remote-tracking branch 'upstream' into HDDS-12519
nandakumar131 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
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
175 changes: 175 additions & 0 deletions
175
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/utils/AutoCompletion.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,175 @@ | ||
| /* | ||
| * 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.hadoop.ozone.utils; | ||
|
|
||
| import java.util.Objects; | ||
| import org.apache.hadoop.hdds.cli.GenericCli; | ||
| import org.apache.hadoop.hdds.cli.HddsVersionProvider; | ||
| import org.apache.ratis.util.ReflectionUtils; | ||
| import org.reflections.Reflections; | ||
| import picocli.AutoComplete; | ||
| import picocli.CommandLine; | ||
| import picocli.CommandLine.Command; | ||
| import picocli.CommandLine.Option; | ||
|
|
||
| /** Tool to generate auto-completion scripts for Ozone CLI. */ | ||
| @Command(name = "ozone completion", | ||
| header = "Generate autocompletion script for the specified shell.", | ||
| synopsisHeading = "%nUsage: ", | ||
| description = AutoCompletion.DESCRIPTION, | ||
| versionProvider = HddsVersionProvider.class, | ||
| mixinStandardHelpOptions = true) | ||
| public final class AutoCompletion extends GenericCli { | ||
|
|
||
| public static final String DESCRIPTION = "%nThe generated shell code must " + | ||
| "be evaluated to provide interactive completion of ozone commands. " + | ||
| "See each sub-command's help for details on how to use the generated script.%n"; | ||
|
|
||
| private static final String BASH_DESCRIPTION = "%nTo load completions in your current shell session: %n" + | ||
| "%n\t source <(ozone completion bash) %n" + | ||
| "%nTo load completions for every new session automatically, %n" + | ||
| "add this to your `~/.bash_profile`: %n" + | ||
| "%n\t eval \"$(ozone completion bash)\" %n"; | ||
|
|
||
| private static final String ZSH_DESCRIPTION = "%nTo load completions in your current shell session: %n" + | ||
| "%n\t source <(ozone completion zsh)%n" + | ||
| "%nTo load completions for every new session automatically, %n" + | ||
| "generate a `_ozone` completion script and put it in your `$fpath`: %n" + | ||
| "%n\t ozone completion zsh > \"$${fpath[1]}/_ozone\" %n"; | ||
|
|
||
| private static final String OZONE_COMMAND = "ozone "; | ||
| private static final int PREFIX_LENGTH = OZONE_COMMAND.length(); | ||
| private static final String[] PACKAGES_TO_SCAN = { | ||
| "org.apache.hadoop.hdds", | ||
| "org.apache.hadoop.ozone", | ||
| "org.apache.ozone", | ||
| }; | ||
|
|
||
| /** Generates bash auto-completion script for Ozone CLI. */ | ||
| @Command(name = "bash", | ||
| header = "Generate the shell completion code for bash.", | ||
| synopsisHeading = "%nUsage: ", | ||
| description = AutoCompletion.BASH_DESCRIPTION, | ||
| versionProvider = HddsVersionProvider.class, | ||
| mixinStandardHelpOptions = true) | ||
| public void bashCompletion() { | ||
| out().println(getBashCompletion()); | ||
| } | ||
|
|
||
| /** Generates zsh auto-completion script for Ozone CLI. */ | ||
| @Command(name = "zsh", | ||
| header = "Generate the shell completion code for zsh.", | ||
| synopsisHeading = "%nUsage: ", | ||
| description = AutoCompletion.ZSH_DESCRIPTION, | ||
| versionProvider = HddsVersionProvider.class, | ||
| mixinStandardHelpOptions = true) | ||
| public void zshCompletion() { | ||
| bashCompletion(); | ||
| } | ||
|
|
||
| private String getBashCompletion() { | ||
| final CommandLine ozone = new CommandLine(new Ozone()); | ||
| for (String pkg : PACKAGES_TO_SCAN) { | ||
| new Reflections(pkg).getSubTypesOf(GenericCli.class).stream() | ||
| .map(AutoCompletion::getCommand) | ||
| .filter(Objects::nonNull) | ||
| .filter(AutoCompletion::isPublicCommand) | ||
| .forEach(command -> ozone.addSubcommand(getCommandName(command), command)); | ||
| } | ||
| return AutoComplete.bash("ozone", ozone); | ||
| } | ||
|
|
||
| private static CommandLine getCommand(Class<? extends GenericCli> clazz) { | ||
| CommandLine command = null; | ||
| try { | ||
| command = ReflectionUtils.newInstance(clazz).getCmd(); | ||
| } catch (UnsupportedOperationException ignored) { | ||
| // Skip the GenericCli subclasses that do not have no-args constructor. | ||
| } | ||
| return command; | ||
| } | ||
|
|
||
| private static boolean isPublicCommand(CommandLine command) { | ||
| final CommandLine.Model.CommandSpec spec = command.getCommandSpec(); | ||
| return !spec.usageMessage().hidden() && | ||
| !CommandLine.Model.CommandSpec.DEFAULT_COMMAND_NAME.equals(spec.name()); | ||
| } | ||
|
|
||
| private static String getCommandName(CommandLine command) { | ||
| final CommandLine.Model.CommandSpec spec = command.getCommandSpec(); | ||
| final String qualifiedName = spec.qualifiedName(); | ||
| return qualifiedName.startsWith(OZONE_COMMAND) ? | ||
| qualifiedName.substring(PREFIX_LENGTH) : qualifiedName; | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| new AutoCompletion().run(args); | ||
| } | ||
|
|
||
| /** Ozone top level command, used only to generate auto-complete. */ | ||
| @Command(name = "ozone", | ||
| description = "Ozone top level command") | ||
| private static final class Ozone { | ||
|
|
||
| @Option(names = {"--buildpaths"}, | ||
| description = "attempt to add class files from build tree") | ||
| private String buildpaths; | ||
|
|
||
| @Option(names = {"--config"}, | ||
| description = "Ozone config directory") | ||
| private String config; | ||
|
|
||
| @Option(names = {"--debug"}, | ||
| description = "turn on shell script debug mode") | ||
| private String debug; | ||
|
|
||
| @Option(names = {"--daemon"}, | ||
| description = "attempt to add class files from build tree") | ||
| private String daemon; | ||
|
|
||
| @Option(names = {"--help"}, | ||
| description = "usage information") | ||
| private String help; | ||
|
|
||
| @Option(names = {"--hostnames"}, | ||
| description = "hosts to use in worker mode") | ||
| private String hostnames; | ||
|
|
||
| @Option(names = {"--hosts"}, | ||
| description = "list of hosts to use in worker mode") | ||
| private String hosts; | ||
|
|
||
| @Option(names = {"--loglevel"}, | ||
| description = "set the log4j level for this command") | ||
| private String loglevel; | ||
|
|
||
| @Option(names = {"--workers"}, | ||
| description = "turn on worker mode") | ||
| private String workers; | ||
|
|
||
| @Option(names = {"--jvmargs"}, | ||
| description = "append JVM options to any existing options defined in the OZONE_OPTS environment variable. " + | ||
| "Any defined in OZONE_CLIENT_OPTS will be appended after these jvmargs") | ||
| private String jvmargs; | ||
|
|
||
| @Option(names = {"--validate"}, | ||
| description = "validates if all jars as indicated in the corresponding OZONE_RUN_ARTIFACT_NAME classpath " + | ||
| "file are present, command execution shall continue post validation failure if 'continue' is passed") | ||
| private String validate; | ||
| } | ||
| } |
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.
I prefer your original idea about adding this as
ozone util completion(let's wait for more feedback on this before changing anything). Candidates for moving underozone utilin the future:checknativeclasspathenvvars(Or maybe these belong to
ozone debug?)Maybe also the following, but need to examine what they are exactly, and if we still need them.
daemonlogdtutilThere 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.
Thanks for the suggestion.
Will wait for more input from others on this.
cc. @errose28 @dombizita @Tejaskriya @sarvekshayr
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.
daemonloglooks like a wrapper to set the log level on a service over HTTP, similar to doing this through the web UI, which looks useful. I can't figure out the use case fordtutilthough.What would the distinction be between
utilanddebug? I thinkchecknative,classpath, andenvvarsmake sense underdebug, but I'm ok withozone completionbeing a top level command since its result applies to the whole CLI.