changeset 1162:7538716074c6

help should only display appropriate commands Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-July/007306.html
author Omair Majid <omajid@redhat.com>
date Fri, 12 Jul 2013 10:03:42 -0400
parents ca38f9e22bd5
children 23fd98e86b02
files integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/Activator.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandInfo.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/CompoundCommandInfoSource.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/CurrentEnvironment.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/Environment.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/PluginConfiguration.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParser.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/BasicCommandInfoTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/BuiltInCommandInfoTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/CurrentEnvironmentTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/PluginCommandInfoSourceTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParserTest.java
diffstat 17 files changed, 312 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java	Fri Jul 12 09:56:06 2013 -0400
+++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java	Fri Jul 12 10:03:42 2013 -0400
@@ -126,7 +126,7 @@
 
         shell.expect(SHELL_PROMPT);
 
-        assertMatchesHelpCommandList(shell.getCurrentStandardOutContents());
+        assertMatchesShellHelpCommandList(shell.getCurrentStandardOutContents());
 
         shell.send("exit\n");
 
@@ -155,7 +155,7 @@
         String stdOut = shell.getCurrentStandardOutContents();
         String stdErr = shell.getCurrentStandardErrContents();
 
-        assertMatchesHelpCommandList(shell.getCurrentStandardOutContents());
+        assertMatchesShellHelpCommandList(shell.getCurrentStandardOutContents());
         // use the Pattern.DOTALL flag (?s) so that line terminators match with
         // ".*". stdOut contains the SHELL_PROMPT too.
         assertTrue(stdOut.matches("(?s)^.*\nunknown command '--version'\n.*$"));
@@ -231,5 +231,13 @@
         assertTrue(actual.contains("shell"));
     }
 
+    private static void assertMatchesShellHelpCommandList(String actual) {
+        assertTrue(actual.contains("list of commands"));
+        assertTrue(actual.contains("help"));
+        assertTrue(actual.contains("connect"));
+        assertTrue(actual.contains("disconnect"));
+        assertTrue(actual.contains("ping"));
+    }
+
 }
 
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/Activator.java	Fri Jul 12 09:56:06 2013 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/Activator.java	Fri Jul 12 10:03:42 2013 -0400
@@ -51,6 +51,7 @@
 import com.redhat.thermostat.common.cli.CommandRegistry;
 import com.redhat.thermostat.common.cli.CommandRegistryImpl;
 import com.redhat.thermostat.launcher.BundleManager;
+import com.redhat.thermostat.launcher.internal.CurrentEnvironment.CurrentEnvironmentChangeListener;
 import com.redhat.thermostat.shared.config.Configuration;
 import com.redhat.thermostat.utils.keyring.Keyring;
 
@@ -65,10 +66,12 @@
         private ServiceRegistration exitStatusReg;
         private BundleContext context;
         private BundleManager bundleService;
+        private CurrentEnvironment currentEnvironment;
 
-        RegisterLauncherCustomizer(BundleContext context, BundleManager bundleService) {
+        RegisterLauncherCustomizer(BundleContext context, BundleManager bundleService, CurrentEnvironment env) {
             this.context = context;
             this.bundleService = bundleService;
+            this.currentEnvironment = env;
         }
 
         @Override
@@ -88,7 +91,7 @@
 
             // Register Launcher service since FrameworkProvider is waiting for it blockingly.
             LauncherImpl launcher = new LauncherImpl(context,
-                    new CommandContextFactory(context), bundleService, commands);
+                    new CommandContextFactory(context), bundleService, commands, currentEnvironment);
             launcherReg = context.registerService(Launcher.class.getName(), launcher, null);
             bundleManReg = context.registerService(BundleManager.class, bundleService, null);
             ExitStatus exitStatus = new ExitStatusImpl(ExitStatus.EXIT_SUCCESS);
@@ -122,13 +125,22 @@
     @SuppressWarnings({ "rawtypes", "unchecked" })
     @Override
     public void start(final BundleContext context) throws Exception {
+        CurrentEnvironment environment = new CurrentEnvironment(Environment.CLI);
+
         BundleManager bundleService = new BundleManagerImpl(new Configuration());
-        ServiceTrackerCustomizer customizer = new RegisterLauncherCustomizer(context, bundleService);
+        ServiceTrackerCustomizer customizer = new RegisterLauncherCustomizer(context, bundleService, environment);
         serviceTracker = new ServiceTracker(context, Keyring.class, customizer);
         // Track for Keyring service.
         serviceTracker.open();
 
         final HelpCommand helpCommand = new HelpCommand();
+        environment.addListener(new CurrentEnvironmentChangeListener() {
+            @Override
+            public void enviornmentChanged(Environment oldEnv, Environment newEnv) {
+                helpCommand.setEnvironment(newEnv);
+            }
+        });
+        helpCommand.setEnvironment(environment.getCurrent());
 
         commandInfoSourceTracker = new ServiceTracker(context, CommandInfoSource.class, null) {
             @Override
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandInfo.java	Fri Jul 12 09:56:06 2013 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandInfo.java	Fri Jul 12 10:03:42 2013 -0400
@@ -43,11 +43,6 @@
 
 public interface CommandInfo {
 
-    public enum Environment {
-        SHELL,
-        CLI,
-    }
-
     /**
      * Returns a name for this command. This will be used by the user to select
      * this command.
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CompoundCommandInfoSource.java	Fri Jul 12 09:56:06 2013 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CompoundCommandInfoSource.java	Fri Jul 12 10:03:42 2013 -0400
@@ -46,7 +46,6 @@
 
 import org.apache.commons.cli.Options;
 
-import com.redhat.thermostat.launcher.internal.CommandInfo.Environment;
 
 
 /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CurrentEnvironment.java	Fri Jul 12 10:03:42 2013 -0400
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2012, 2013 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.launcher.internal;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+public class CurrentEnvironment {
+
+    public interface CurrentEnvironmentChangeListener {
+        public void enviornmentChanged(Environment oldEnv, Environment newEnv);
+    }
+
+    private Environment current;
+    private List<CurrentEnvironmentChangeListener> listeners = new CopyOnWriteArrayList<>();
+
+    public CurrentEnvironment(Environment initial) {
+        current = initial;
+    }
+
+    public Environment getCurrent() {
+        return current;
+    }
+
+    public void setCurrent(Environment environment) {
+        if (environment == current) {
+            return;
+        }
+
+        Environment old = current;
+        current = environment;
+        fireEnvironmentChanged(old, current);
+    }
+
+    private void fireEnvironmentChanged(Environment oldEnv, Environment newEnv) {
+        for (CurrentEnvironmentChangeListener listener : listeners) {
+            listener.enviornmentChanged(oldEnv, newEnv);
+        }
+    }
+
+    public void addListener(CurrentEnvironmentChangeListener listener) {
+        listeners.add(listener);
+    }
+
+    public void removeListener(CurrentEnvironmentChangeListener listener) {
+        listeners.remove(listener);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/Environment.java	Fri Jul 12 10:03:42 2013 -0400
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2012, 2013 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.launcher.internal;
+
+
+public enum Environment {
+    SHELL,
+    CLI,
+
+}
\ No newline at end of file
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java	Fri Jul 12 09:56:06 2013 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java	Fri Jul 12 10:03:42 2013 -0400
@@ -47,14 +47,13 @@
 import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Options;
 
+import com.redhat.thermostat.common.cli.AbstractCommand;
 import com.redhat.thermostat.common.cli.Arguments;
 import com.redhat.thermostat.common.cli.CommandContext;
-import com.redhat.thermostat.common.cli.AbstractCommand;
 import com.redhat.thermostat.common.cli.TableRenderer;
-import com.redhat.thermostat.launcher.internal.CommandInfo.Environment;
 import com.redhat.thermostat.shared.locale.Translate;
 
-public class HelpCommand extends AbstractCommand {
+public class HelpCommand extends AbstractCommand  {
 
     private static final Translate<LocaleResources> translator = LocaleResources.createLocalizer();
 
@@ -65,10 +64,16 @@
 
     private CommandInfoSource commandInfoSource;
 
+    private Environment currentEnvironment;
+
     public void setCommandInfoSource(CommandInfoSource source) {
         this.commandInfoSource = source;
     }
 
+    public void setEnvironment(Environment env) {
+        currentEnvironment = env;
+    }
+
     @Override
     public void run(CommandContext ctx) {
         Arguments args = ctx.getArguments();
@@ -91,7 +96,13 @@
 
         TableRenderer renderer = new TableRenderer(2, COMMANDS_COLUMNS_WIDTH);
 
-        Collection<CommandInfo> commandInfos = commandInfoSource.getCommandInfos();
+        Collection<CommandInfo> commandInfos = new ArrayList<>();
+        for (CommandInfo info: commandInfoSource.getCommandInfos()) {
+            if (info.getEnvironments().contains(currentEnvironment)) {
+                commandInfos.add(info);
+            }
+        }
+
         List<CommandInfo> sortedCommandInfos = new ArrayList<>(commandInfos);
 
         Collections.sort(sortedCommandInfos, comparator);
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Fri Jul 12 09:56:06 2013 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Fri Jul 12 10:03:42 2013 -0400
@@ -68,7 +68,6 @@
 import com.redhat.thermostat.common.tools.StorageAuthInfoGetter;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 import com.redhat.thermostat.launcher.BundleManager;
-import com.redhat.thermostat.launcher.internal.CommandInfo.Environment;
 import com.redhat.thermostat.shared.config.InvalidConfigurationException;
 import com.redhat.thermostat.shared.locale.LocalizedString;
 import com.redhat.thermostat.shared.locale.Translate;
@@ -95,17 +94,21 @@
     private final Version coreVersion;
     private final CommandSource commandSource;
     private final CommandInfoSource commandInfoSource;
+    private final CurrentEnvironment currentEnvironment;
 
     /** MUST be mutated in a 'synchronized (this)' block */
     private ClientPreferences prefs;
 
 
-    public LauncherImpl(BundleContext context, CommandContextFactory cmdCtxFactory, BundleManager registry, CommandInfoSource infoSource) {
-        this(context, cmdCtxFactory, registry, infoSource, new CommandSource(context), new LoggingInitializer(), new DbServiceFactory(), new Version());
+
+    public LauncherImpl(BundleContext context, CommandContextFactory cmdCtxFactory, BundleManager registry, CommandInfoSource infoSource, CurrentEnvironment env) {
+        this(context, cmdCtxFactory, registry, infoSource,
+                new CommandSource(context), env, new LoggingInitializer(), new DbServiceFactory(), new Version());
     }
 
     LauncherImpl(BundleContext context, CommandContextFactory cmdCtxFactory, BundleManager registry,
             CommandInfoSource commandInfoSource, CommandSource commandSource,
+            CurrentEnvironment currentEnvironment,
             LoggingInitializer loggingInitializer, DbServiceFactory dbServiceFactory, Version version) {
         this.context = context;
         this.cmdCtxFactory = cmdCtxFactory;
@@ -114,6 +117,7 @@
         this.coreVersion = version;
         this.commandSource = commandSource;
         this.commandInfoSource = commandInfoSource;
+        this.currentEnvironment = currentEnvironment;
 
         loggingInitializer.initialize();
         logger = LoggingUtils.getLogger(LauncherImpl.class);
@@ -127,6 +131,10 @@
     @Override
     public void run(String[] args, Collection<ActionListener<ApplicationState>> listeners, boolean inShell) {
         usageCount.incrementAndGet();
+
+        Environment oldEnvironment = currentEnvironment.getCurrent();
+        currentEnvironment.setCurrent(inShell ? Environment.SHELL : Environment.CLI);
+
         try {
             if (hasNoArguments(args)) {
                 runHelpCommand();
@@ -149,9 +157,9 @@
             // they really aren't, but the finally block make it seem so.
             e.printStackTrace(System.err);
             throw e;
-        }
-        finally {
+        } finally {
             args = null;
+            currentEnvironment.setCurrent(oldEnvironment);
             boolean isLastLaunch = (usageCount.decrementAndGet() == 0);
             if (isLastLaunch) {
                 shutdown();
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/PluginConfiguration.java	Fri Jul 12 09:56:06 2013 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/PluginConfiguration.java	Fri Jul 12 10:03:42 2013 -0400
@@ -42,7 +42,6 @@
 
 import org.apache.commons.cli.Options;
 
-import com.redhat.thermostat.launcher.internal.CommandInfo.Environment;
 
 public class PluginConfiguration {
 
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParser.java	Fri Jul 12 09:56:06 2013 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParser.java	Fri Jul 12 10:03:42 2013 -0400
@@ -65,7 +65,6 @@
 import org.xml.sax.SAXParseException;
 
 import com.redhat.thermostat.common.utils.LoggingUtils;
-import com.redhat.thermostat.launcher.internal.CommandInfo.Environment;
 import com.redhat.thermostat.launcher.internal.PluginConfiguration.CommandExtensions;
 import com.redhat.thermostat.launcher.internal.PluginConfiguration.NewCommand;
 import com.redhat.thermostat.plugin.validator.PluginConfigurationValidatorException;
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/BasicCommandInfoTest.java	Fri Jul 12 09:56:06 2013 -0400
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/BasicCommandInfoTest.java	Fri Jul 12 10:03:42 2013 -0400
@@ -46,7 +46,6 @@
 import org.apache.commons.cli.Options;
 import org.junit.Test;
 
-import com.redhat.thermostat.launcher.internal.CommandInfo.Environment;
 
 public class BasicCommandInfoTest {
 
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/BuiltInCommandInfoTest.java	Fri Jul 12 09:56:06 2013 -0400
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/BuiltInCommandInfoTest.java	Fri Jul 12 10:03:42 2013 -0400
@@ -57,7 +57,6 @@
 import org.junit.Before;
 import org.junit.Test;
 
-import com.redhat.thermostat.launcher.internal.CommandInfo.Environment;
 import com.redhat.thermostat.shared.locale.Translate;
 
 public class BuiltInCommandInfoTest {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/CurrentEnvironmentTest.java	Fri Jul 12 10:03:42 2013 -0400
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2012, 2013 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.launcher.internal;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import org.junit.Test;
+
+import com.redhat.thermostat.launcher.internal.CurrentEnvironment.CurrentEnvironmentChangeListener;
+
+public class CurrentEnvironmentTest {
+
+    @Test
+    public void verifyInitialization() {
+        CurrentEnvironment environment = new CurrentEnvironment(Environment.CLI);
+
+        assertEquals(Environment.CLI, environment.getCurrent());
+    }
+
+    @Test
+    public void verifySetAndGet() {
+        CurrentEnvironment environment = new CurrentEnvironment(Environment.CLI);
+        environment.setCurrent(Environment.SHELL);
+
+        assertEquals(Environment.SHELL, environment.getCurrent());
+    }
+
+    @Test
+    public void verifyListenersAreInvoked() {
+        CurrentEnvironment environment = new CurrentEnvironment(Environment.CLI);
+        CurrentEnvironmentChangeListener listener = mock(CurrentEnvironmentChangeListener.class);
+
+        environment.addListener(listener);
+
+        environment.setCurrent(Environment.SHELL);
+
+        verify(listener).enviornmentChanged(Environment.CLI, Environment.SHELL);
+    }
+}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java	Fri Jul 12 09:56:06 2013 -0400
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java	Fri Jul 12 10:03:42 2013 -0400
@@ -52,7 +52,6 @@
 
 import com.redhat.thermostat.common.cli.Arguments;
 import com.redhat.thermostat.common.cli.SimpleArguments;
-import com.redhat.thermostat.launcher.internal.CommandInfo.Environment;
 import com.redhat.thermostat.test.TestCommandContextFactory;
 
 public class HelpCommandTest {
@@ -103,16 +102,20 @@
         CommandInfo info1 = mock(CommandInfo.class);
         when(info1.getName()).thenReturn("test1");
         when(info1.getDescription()).thenReturn("test command 1");
+        when(info1.getEnvironments()).thenReturn(EnumSet.of(Environment.CLI, Environment.SHELL));
+
         infoList.add(info1);
 
         CommandInfo info2 = mock(CommandInfo.class);
         when(info2.getName()).thenReturn("test2longname");
         when(info2.getDescription()).thenReturn("test command 2");
+        when(info2.getEnvironments()).thenReturn(EnumSet.of(Environment.CLI, Environment.SHELL));
         infoList.add(info2);
 
         when(infos.getCommandInfos()).thenReturn(infoList);
 
         HelpCommand cmd = new HelpCommand();
+        cmd.setEnvironment(Environment.CLI);
         cmd.setCommandInfoSource(infos);
 
         Arguments args = mock(Arguments.class);
@@ -153,26 +156,32 @@
         CommandInfo helpInfo = mock(CommandInfo.class);
         when(helpInfo.getName()).thenReturn("help");
         when(helpInfo.getDescription()).thenReturn("show help");
-        
+        when(helpInfo.getEnvironments()).thenReturn(EnumSet.of(Environment.CLI, Environment.SHELL));
+
         CommandInfo info1 = mock(CommandInfo.class);
         when(info1.getName()).thenReturn("test1");
         when(info1.getDescription()).thenReturn("test command 1");
+        when(info1.getEnvironments()).thenReturn(EnumSet.of(Environment.CLI, Environment.SHELL));
 
         CommandInfo info2 = mock(CommandInfo.class);
         when(info2.getName()).thenReturn("test2");
         when(info2.getDescription()).thenReturn("test command 2");
+        when(info2.getEnvironments()).thenReturn(EnumSet.of(Environment.CLI, Environment.SHELL));
 
         CommandInfo info3 = mock(CommandInfo.class);
         when(info3.getName()).thenReturn("test3");
         when(info3.getDescription()).thenReturn("test command 3");
+        when(info3.getEnvironments()).thenReturn(EnumSet.of(Environment.CLI, Environment.SHELL));
 
         CommandInfo info4 = mock(CommandInfo.class);
         when(info4.getName()).thenReturn("test4");
         when(info4.getDescription()).thenReturn("test command 4");
+        when(info4.getEnvironments()).thenReturn(EnumSet.of(Environment.CLI, Environment.SHELL));
 
         when(infos.getCommandInfos()).thenReturn(Arrays.asList(info2, helpInfo, info4, info3, info1));
 
         HelpCommand cmd = new HelpCommand();
+        cmd.setEnvironment(Environment.CLI);
         cmd.setCommandInfoSource(infos);
         Arguments args = mock(Arguments.class);
         when(args.getNonOptionArguments()).thenReturn(new ArrayList<String>());
@@ -189,6 +198,50 @@
     }
 
     @Test
+    public void verifyHelpFiltersCommands() {
+        CommandInfo helpInfo = mock(CommandInfo.class);
+        when(helpInfo.getName()).thenReturn("help");
+        when(helpInfo.getDescription()).thenReturn("show help");
+        when(helpInfo.getEnvironments()).thenReturn(EnumSet.of(Environment.CLI));
+
+        CommandInfo info1 = mock(CommandInfo.class);
+        when(info1.getName()).thenReturn("test1");
+        when(info1.getDescription()).thenReturn("test command 1");
+        when(info1.getEnvironments()).thenReturn(EnumSet.of(Environment.CLI, Environment.SHELL));
+
+        CommandInfo info2 = mock(CommandInfo.class);
+        when(info2.getName()).thenReturn("test2");
+        when(info2.getDescription()).thenReturn("test command 2");
+        when(info2.getEnvironments()).thenReturn(EnumSet.of(Environment.CLI));
+
+        CommandInfo info3 = mock(CommandInfo.class);
+        when(info3.getName()).thenReturn("test3");
+        when(info3.getDescription()).thenReturn("test command 3");
+        when(info3.getEnvironments()).thenReturn(EnumSet.of(Environment.SHELL));
+
+        CommandInfo info4 = mock(CommandInfo.class);
+        when(info4.getName()).thenReturn("test4");
+        when(info4.getDescription()).thenReturn("test command 4");
+        when(info4.getEnvironments()).thenReturn(EnumSet.of(Environment.CLI));
+
+        when(infos.getCommandInfos()).thenReturn(Arrays.asList(info2, helpInfo, info4, info3, info1));
+
+        HelpCommand cmd = new HelpCommand();
+        cmd.setEnvironment(Environment.CLI);
+        cmd.setCommandInfoSource(infos);
+        Arguments args = mock(Arguments.class);
+        when(args.getNonOptionArguments()).thenReturn(new ArrayList<String>());
+        cmd.run(ctxFactory.createContext(args));
+
+        String actual = ctxFactory.getOutput();
+        String expected = "list of commands:\n\n"
+                + " help          show help\n"
+                + " test1         test command 1\n"
+                + " test2         test command 2\n"
+                + " test4         test command 4\n";
+        assertEquals(expected, actual);
+    }
+    @Test
     public void verifyHelpUnknownCmdPrintsSummaries() {
         when(infos.getCommandInfo("test1")).thenThrow(new CommandInfoNotFoundException("test1"));
 
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java	Fri Jul 12 09:56:06 2013 -0400
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java	Fri Jul 12 10:03:42 2013 -0400
@@ -67,7 +67,6 @@
 import com.redhat.thermostat.common.ActionNotifier;
 import com.redhat.thermostat.common.ApplicationService;
 import com.redhat.thermostat.common.ExitStatus;
-import com.redhat.thermostat.common.Pair;
 import com.redhat.thermostat.common.Version;
 import com.redhat.thermostat.common.cli.AbstractStateNotifyingCommand;
 import com.redhat.thermostat.common.cli.Arguments;
@@ -78,7 +77,6 @@
 import com.redhat.thermostat.common.config.ClientPreferences;
 import com.redhat.thermostat.common.tools.ApplicationState;
 import com.redhat.thermostat.launcher.BundleManager;
-import com.redhat.thermostat.launcher.internal.CommandInfo.Environment;
 import com.redhat.thermostat.launcher.internal.DisallowSystemExitSecurityManager.ExitException;
 import com.redhat.thermostat.launcher.internal.LauncherImpl.LoggingInitializer;
 import com.redhat.thermostat.shared.locale.LocalizedString;
@@ -220,6 +218,7 @@
         when(helpCommandInfo.getEnvironments()).thenReturn(EnumSet.of(Environment.SHELL, Environment.CLI));
 
         HelpCommand helpCommand = new HelpCommand();
+        helpCommand.setEnvironment(Environment.CLI);
 
         CommandRegistry reg = ctxFactory.getCommandRegistry();
         reg.registerCommand("help", helpCommand);
@@ -259,11 +258,12 @@
         when(appSvc.getApplicationExecutor()).thenReturn(exec);
         bundleContext.registerService(ApplicationService.class, appSvc, null);
 
+        CurrentEnvironment environment = mock(CurrentEnvironment.class);
         loggingInitializer = mock(LoggingInitializer.class);
         dbServiceFactory = mock(DbServiceFactory.class);
         version = mock(Version.class);
 
-        launcher = new LauncherImpl(bundleContext, ctxFactory, registry, infos, new CommandSource(bundleContext), loggingInitializer, dbServiceFactory, version);
+        launcher = new LauncherImpl(bundleContext, ctxFactory, registry, infos, new CommandSource(bundleContext), environment, loggingInitializer, dbServiceFactory, version);
 
         Keyring keyring = mock(Keyring.class);
         launcher.setPreferences(new ClientPreferences(keyring));
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/PluginCommandInfoSourceTest.java	Fri Jul 12 09:56:06 2013 -0400
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/PluginCommandInfoSourceTest.java	Fri Jul 12 10:03:42 2013 -0400
@@ -63,7 +63,6 @@
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 
-import com.redhat.thermostat.launcher.internal.CommandInfo.Environment;
 import com.redhat.thermostat.launcher.internal.PluginConfiguration.CommandExtensions;
 import com.redhat.thermostat.launcher.internal.PluginConfiguration.NewCommand;
 import com.redhat.thermostat.plugin.validator.PluginConfigurationValidatorException;
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParserTest.java	Fri Jul 12 09:56:06 2013 -0400
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParserTest.java	Fri Jul 12 10:03:42 2013 -0400
@@ -54,7 +54,6 @@
 import org.apache.commons.cli.Options;
 import org.junit.Test;
 
-import com.redhat.thermostat.launcher.internal.CommandInfo.Environment;
 import com.redhat.thermostat.launcher.internal.PluginConfiguration.CommandExtensions;
 import com.redhat.thermostat.launcher.internal.PluginConfiguration.NewCommand;
 import com.redhat.thermostat.shared.locale.Translate;