You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2022/10/12 16:51:23 UTC

[karaf] branch main updated: [KARAF-4542] - add '--repository' option to feature:list

This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/main by this push:
     new 1b18c1b371 [KARAF-4542] - add '--repository' option to feature:list
     new 0697da7295 Merge pull request #1642 from awrb/KARAF-4542
1b18c1b371 is described below

commit 1b18c1b371223616bbe11e4018f5e9e35a19ea2c
Author: Aleksy Wróblewski <al...@bbbit.io>
AuthorDate: Mon Oct 10 23:49:02 2022 +0200

    [KARAF-4542] - add '--repository' option to feature:list
---
 .../features/command/ListFeaturesCommand.java      | 97 +++++++++++++---------
 .../features/command/ListFeaturesCommandTest.java  | 40 +++++++++
 .../java/org/apache/karaf/itests/FeatureTest.java  | 27 ++++++
 3 files changed, 123 insertions(+), 41 deletions(-)

diff --git a/features/command/src/main/java/org/apache/karaf/features/command/ListFeaturesCommand.java b/features/command/src/main/java/org/apache/karaf/features/command/ListFeaturesCommand.java
index cc26e52f1f..6e8df0f2ed 100644
--- a/features/command/src/main/java/org/apache/karaf/features/command/ListFeaturesCommand.java
+++ b/features/command/src/main/java/org/apache/karaf/features/command/ListFeaturesCommand.java
@@ -17,14 +17,15 @@
 package org.apache.karaf.features.command;
 
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
 
 import org.apache.karaf.features.Feature;
 import org.apache.karaf.features.FeaturesService;
 import org.apache.karaf.features.Repository;
+import org.apache.karaf.features.command.completers.InstalledRepoNameCompleter;
 import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
 import org.apache.karaf.shell.api.action.Option;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.apache.karaf.shell.support.table.Row;
@@ -49,12 +50,16 @@ public class ListFeaturesCommand extends FeaturesCommandSupport {
     @Option(name = "-o", aliases = {"--ordered"}, description = "Display a list using alphabetical order ", required = false, multiValued = false)
     boolean ordered;
 
+    @Option(name = "--repository", description = "Only list features from that repository", required = false, multiValued = false)
+    @Completion(InstalledRepoNameCompleter.class)
+    String repository;
+
     @Option(name = "--no-format", description = "Disable table rendered output", required = false, multiValued = false)
     boolean noFormat;
 
     protected void doExecute(FeaturesService featuresService) throws Exception {
         boolean needsLegend = false;
-        
+
         ShellTable table = new ShellTable();
         table.column("Name");
         table.column("Version");
@@ -67,43 +72,16 @@ public class ListFeaturesCommand extends FeaturesCommandSupport {
         }
         table.emptyTableText(onlyInstalled ? "No features installed" : "No features available");
 
-        List<Repository> repos = Arrays.asList(featuresService.listRepositories());
-        for (Repository r : repos) {
-            List<Feature> features = Arrays.asList(r.getFeatures());
-            if (ordered) {
-                features.sort(new FeatureComparator());
-            }
-            for (Feature f : features) {
-                if (onlyInstalled && !featuresService.isInstalled(f)) {
-                    // Filter out not installed features if we only want to see the installed ones
-                    continue;
-                }
-                if (onlyRequired && !featuresService.isRequired(f)) {
-                    // Filter out not installed features if we only want to see the installed ones
-                    continue;
-                }
-                if (!showBlacklisted && f.isBlacklisted()) {
-                    // Filter out blacklisted
-                    continue;
-                }
-                if (!showHidden && f.isHidden()) {
-                    // Filter out hidden feature if not asked to display those
-                    continue;
-                }
-                Row row = table.addRow();
-                row.addContent(
-                        f.getName(),
-                        f.getVersion(),
-                        featuresService.isRequired(f) ? "x" : "",
-                        featuresService.getState(f.getId()),
-                        r.getName(),
-                        f.getDescription());
-                if (showBlacklisted) {
-                    row.addContent(f.isBlacklisted() ? "yes" : "no");
-                }
-                if (isInstalledViaDeployDir(r.getName())) {
-                    needsLegend = true;
-                }
+        if (repository != null) {
+            List<Feature> features = Arrays.asList(featuresService.getRepository(repository).getFeatures());
+            populateTable(featuresService, repository, features, table);
+            needsLegend = isInstalledViaDeployDir(repository);
+        } else {
+            Repository[] repos = featuresService.listRepositories();
+            for (Repository r : repos) {
+                String repoName = r.getName();
+                populateTable(featuresService, repoName, Arrays.asList(r.getFeatures()), table);
+                needsLegend = isInstalledViaDeployDir(repoName);
             }
         }
 
@@ -119,10 +97,47 @@ public class ListFeaturesCommand extends FeaturesCommandSupport {
         return st != null && st.length() > 1 && st.charAt(st.length() - 1) == '*';
     }
 
+    private void populateTable(FeaturesService featuresService,
+                               String repositoryName,
+                               List<Feature> features,
+                               ShellTable table) {
+        if (ordered) {
+            features.sort(new FeatureComparator());
+        }
+        for (Feature f : features) {
+            if (onlyInstalled && !featuresService.isInstalled(f)) {
+                // Filter out not installed features if we only want to see the installed ones
+                continue;
+            }
+            if (onlyRequired && !featuresService.isRequired(f)) {
+                // Filter out not installed features if we only want to see the installed ones
+                continue;
+            }
+            if (!showBlacklisted && f.isBlacklisted()) {
+                // Filter out blacklisted
+                continue;
+            }
+            if (!showHidden && f.isHidden()) {
+                // Filter out hidden feature if not asked to display those
+                continue;
+            }
+            Row row = table.addRow();
+            row.addContent(
+                    f.getName(),
+                    f.getVersion(),
+                    featuresService.isRequired(f) ? "x" : "",
+                    featuresService.getState(f.getId()),
+                    repositoryName,
+                    f.getDescription());
+            if (showBlacklisted) {
+                row.addContent(f.isBlacklisted() ? "yes" : "no");
+            }
+        }
+    }
+
     class FeatureComparator implements Comparator<Feature> {
         public int compare(Feature o1, Feature o2) {
-            return o1.getName().toLowerCase().compareTo( o2.getName().toLowerCase() );
+            return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase());
         }
     }
-
 }
diff --git a/features/command/src/test/java/org/apache/karaf/features/command/ListFeaturesCommandTest.java b/features/command/src/test/java/org/apache/karaf/features/command/ListFeaturesCommandTest.java
index a56347c68f..eb8d38b966 100644
--- a/features/command/src/test/java/org/apache/karaf/features/command/ListFeaturesCommandTest.java
+++ b/features/command/src/test/java/org/apache/karaf/features/command/ListFeaturesCommandTest.java
@@ -41,6 +41,7 @@ public class ListFeaturesCommandTest {
         EasyMock.expect(service.listRepositories()).andReturn(new Repository[] { repo });
         EasyMock.expect(repo.getFeatures()).andReturn(new Feature[] { feature });
         EasyMock.expect(feature.isHidden()).andReturn(true);
+        EasyMock.expect(repo.getName()).andReturn("repository");
         EasyMock.expect(feature.isBlacklisted()).andReturn(false);
 
         EasyMock.replay(service, repo, feature);
@@ -97,4 +98,43 @@ public class ListFeaturesCommandTest {
         EasyMock.verify(service, repo, feature);
     }
 
+    @Test
+    public void testListFeaturesFromRepository() throws Exception {
+
+        FeaturesService service = EasyMock.niceMock(FeaturesService.class);
+        Repository repo = EasyMock.createMock(Repository.class);
+        Feature feature = EasyMock.createMock(Feature.class);
+        String repositoryName = "repository";
+
+        EasyMock.expect(service.getRepository(repositoryName)).andReturn(repo);
+
+        EasyMock.expect(repo.getFeatures()).andReturn(new Feature[] { feature });
+        EasyMock.expect(feature.isHidden()).andReturn(false);
+        EasyMock.expect(feature.isBlacklisted()).andReturn(false);
+        EasyMock.expect(feature.getName()).andReturn("feature");
+        EasyMock.expect(feature.getId()).andReturn("feature/1.0.0");
+        EasyMock.expect(service.getState(EasyMock.eq("feature/1.0.0"))).andReturn(FeatureState.Started);
+        EasyMock.expect(feature.getDescription()).andReturn("description");
+        EasyMock.expect(feature.getVersion()).andReturn("1.0.0");
+        EasyMock.expect(service.isRequired(feature)).andReturn(true);
+        EasyMock.expect(repo.getName()).andReturn(repositoryName).anyTimes();
+
+        EasyMock.replay(service, repo, feature);
+
+        ListFeaturesCommand command = new ListFeaturesCommand();
+        command.setFeaturesService(service);
+        command.noFormat = true;
+        command.repository = repositoryName;
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        PrintStream out = new PrintStream(baos);
+        System.setOut(out);
+
+        command.execute();
+
+        out.flush();
+        assertTrue(baos.toString().contains("feature"));
+        EasyMock.verify(service, repo, feature);
+    }
+
 }
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/FeatureTest.java b/itests/test/src/test/java/org/apache/karaf/itests/FeatureTest.java
index 467f9636c1..3d27125b17 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/FeatureTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/FeatureTest.java
@@ -13,6 +13,7 @@
  */
 package org.apache.karaf.itests;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
@@ -74,6 +75,32 @@ public class FeatureTest extends BaseTest {
         assertFalse(featureListOutput.isEmpty());
     }
 
+    @Test
+    public void listCommandFromRepository() {
+        executeCommand("feature:repo-add mvn:org.apache.karaf.cellar/apache-karaf-cellar/3.0.0/xml/features");
+        String repositoryName = "karaf-cellar-3.0.0";
+        String featureListOutput = executeCommand("feature:list --repository " + repositoryName);
+        assertFalse(featureListOutput.isEmpty());
+
+        String[] lines = featureListOutput.split("\\R");
+        String headers = lines[0];
+        assertContains("Name", headers);
+        assertContains("Version", headers);
+        assertContains("Required", headers);
+        assertContains("State", headers);
+        assertContains("Repository", headers);
+        assertContains("Description", headers);
+
+        // lines[1] is a separator line, start from 2
+        int repositoryColumnIndex = 4;
+        for (int i = 2; i < lines.length; i++) {
+            String row = lines[i];
+            assertTrue(row.matches("(.*|){4}"));
+            String[] columns = row.split("\\|");
+            assertEquals(repositoryName.trim(), columns[repositoryColumnIndex].trim());
+        }
+    }
+
     @Test
     public void listViaMBean() throws Exception {
         MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();