-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
[cli] Completions command for suggestions #213
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
wing328
merged 2 commits into
OpenAPITools:master
from
jimschubert:cli-commands-command
Jun 10, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
100 changes: 100 additions & 0 deletions
100
...s/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/CompletionCommand.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,100 @@ | ||
/* | ||
* Copyright (C) 2010 the original author or authors. | ||
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech) | ||
* | ||
* Licensed 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. | ||
*/ | ||
/* | ||
* NOTICE: File originally taken from: | ||
* https://github.com/airlift/airline/blob/fc7a55e34b6361cb97235de5a1b21cba9b508f4b/src/main/java/io/airlift/airline/SuggestCommand.java#L1 | ||
* Modifications have been made to fit the needs of OpenAPI Tools CLI. | ||
*/ | ||
package org.openapitools.codegen.cmd; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Joiner; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import io.airlift.airline.*; | ||
import io.airlift.airline.model.*; | ||
|
||
import javax.inject.Inject; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.Callable; | ||
|
||
import static com.google.common.collect.Lists.newArrayList; | ||
import static io.airlift.airline.ParserUtil.createInstance; | ||
|
||
@Command(name = "completion", description = "Complete commands (for using in tooling such as Bash Completions).", hidden = true) | ||
public class CompletionCommand | ||
implements Runnable, Callable<Void> { | ||
private static final Map<Context, Class<? extends Suggester>> BUILTIN_SUGGESTERS = ImmutableMap.<Context, Class<? extends Suggester>>builder() | ||
.put(Context.GLOBAL, GlobalSuggester.class) | ||
.put(Context.GROUP, GroupSuggester.class) | ||
.put(Context.COMMAND, CommandSuggester.class) | ||
.build(); | ||
|
||
@Inject | ||
public GlobalMetadata metadata; | ||
|
||
@Arguments | ||
public List<String> arguments = newArrayList(); | ||
|
||
@Override | ||
public Void call() { | ||
run(); | ||
return null; | ||
} | ||
|
||
@VisibleForTesting | ||
public Iterable<String> generateSuggestions() { | ||
Parser parser = new Parser(); | ||
ParseState state = parser.parse(metadata, arguments); | ||
|
||
Class<? extends Suggester> suggesterClass = BUILTIN_SUGGESTERS.get(state.getLocation()); | ||
if (suggesterClass != null) { | ||
SuggesterMetadata suggesterMetadata = MetadataLoader.loadSuggester(suggesterClass); | ||
|
||
if (suggesterMetadata != null) { | ||
ImmutableMap.Builder<Class<?>, Object> bindings = ImmutableMap.<Class<?>, Object>builder() | ||
.put(GlobalMetadata.class, metadata); | ||
|
||
if (state.getGroup() != null) { | ||
bindings.put(CommandGroupMetadata.class, state.getGroup()); | ||
} | ||
|
||
if (state.getCommand() != null) { | ||
bindings.put(CommandMetadata.class, state.getCommand()); | ||
} | ||
|
||
Suggester suggester = createInstance(suggesterMetadata.getSuggesterClass(), | ||
ImmutableList.<OptionMetadata>of(), | ||
null, | ||
null, | ||
null, | ||
suggesterMetadata.getMetadataInjections(), | ||
bindings.build()); | ||
|
||
return suggester.suggest(); | ||
} | ||
} | ||
|
||
return ImmutableList.of(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
System.out.println(Joiner.on("\n").join(generateSuggestions())); | ||
} | ||
} |
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.
Shall we update the above with the following instead?
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.
@wing328 we can't. Per comment below the header, I took this from the framework's suggest command and renamed it with a description. It's unfortunate that they have such a generic license text, because it makes it look like something that needs to be updated.
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.
Oh, or do you think we should add our line as a second line in the header?
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.
@wing328 I've added
Below the line you've commented on, as this licensed file is originally from airlift/airline, not SmartBear.
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.
Looks good to me