You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by GitBox <gi...@apache.org> on 2022/09/19 19:57:09 UTC

[GitHub] [karaf] awrb commented on a diff in pull request #1624: KARAF-7554 - add feature:status command

awrb commented on code in PR #1624:
URL: https://github.com/apache/karaf/pull/1624#discussion_r974620384


##########
features/command/src/main/java/org/apache/karaf/features/command/completers/FeatureVersionCompleter.java:
##########
@@ -0,0 +1,41 @@
+package org.apache.karaf.features.command.completers;
+
+import org.apache.karaf.features.Feature;
+import org.apache.karaf.features.FeaturesService;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.CommandLine;
+import org.apache.karaf.shell.api.console.Completer;
+import org.apache.karaf.shell.api.console.Session;
+import org.apache.karaf.shell.support.completers.StringsCompleter;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+@Service
+public class FeatureVersionCompleter implements Completer {
+
+    @Reference
+    private FeaturesService featuresService;
+
+    @Override
+    public int complete(Session session, CommandLine commandLine, List<String> candidates) {
+        StringsCompleter delegate = new StringsCompleter();
+        String[] args = commandLine.getArguments();
+        // args look like this at this point: [feature:status, wrapper, '']
+        if (args.length >= 3) {
+            String featureArg = args[1];
+            try {
+                Feature[] features = featuresService.getFeatures(featureArg);
+                List<String> versions = Arrays.stream(features).map(Feature::getVersion).collect(Collectors.toList());
+                delegate.getStrings().addAll(versions);

Review Comment:
   I wanted the completer to work if there are multiple versions of a single feature available, but sadly `versions` is always a singleton here. In `FeatureReq` the latest version is returned:
   ```
       public Stream<Feature> getMatchingFeatures(Map<String, Map<String, Feature>> allFeatures) {
           Pattern pattern = Pattern.compile(name);
           Function<String, Optional<Feature>> func = featureName -> {
               Feature matchingFeature = null;
               if (pattern.matcher(featureName).matches()) {
                   Map<String, Feature> versions = allFeatures.get(featureName);
                   matchingFeature = getLatestFeature(versions, versionRange);
               }
               return Optional.ofNullable(matchingFeature);
           };
           return allFeatures.keySet().stream().map(func).filter(Optional::isPresent).map(Optional::get);
       }
   ```
   It does not seem possible to get all versions via `FeaturesService`
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@karaf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org