Skip to content

Commit

Permalink
FORGE-2574: Introduced CommandLine in ShellContext
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jan 12, 2016
1 parent 7451e8f commit 143f930
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.shell.line;

import java.util.List;

/**
* The command line
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*/
public interface CommandLine
{
/**
* @return the arguments for a given command line
*/
CommandOption getArgument();

/**
* @return the parsed options from the command line
*/
List<CommandOption> getOptions();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.shell.line;

import java.util.List;

/**
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*/
public interface CommandOption
{
String getName();

String getValue();

List<String> getValues();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jboss.forge.addon.shell.ui;

import org.jboss.forge.addon.shell.Shell;
import org.jboss.forge.addon.shell.line.CommandLine;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.wizard.UIWizard;
Expand All @@ -27,4 +28,9 @@ public interface ShellContext extends UIContext
* @return <code>true</code> if should display errors
*/
boolean isVerbose();

/**
* @return the {@link CommandLine} used in this command execution
*/
CommandLine getCommandLine();
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ private ProcessedCommand<?> generateCommand(final CommandAdapter commandAdapter,
public Map<String, InputComponent<?, ?>> populateUIInputs(CommandLine<?> commandLine,
Map<String, InputComponent<?, ?>> inputs, UIContext context)
{
// Added command line to UIContext.getAttributeMap
context.getAttributeMap().put(CommandLine.class, commandLine);
Map<String, InputComponent<?, ?>> populatedInputs = new LinkedHashMap<>();
for (Entry<String, InputComponent<?, ?>> entry : inputs.entrySet())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.shell.aesh.line;

import java.util.List;
import java.util.stream.Collectors;

import org.jboss.aesh.cl.CommandLine;
import org.jboss.aesh.cl.internal.ProcessedOption;
import org.jboss.forge.addon.shell.line.CommandOption;

/**
* Default {@link org.jboss.forge.addon.shell.line.CommandLine} implementation
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*/
public class CommandLineImpl implements org.jboss.forge.addon.shell.line.CommandLine
{
private final CommandLine<?> cmdLine;

public CommandLineImpl(CommandLine<?> cmdLine)
{
super();
this.cmdLine = cmdLine;
}

@Override
public CommandOption getArgument()
{
ProcessedOption argument = cmdLine.getArgument();
return argument == null || argument.getValue() == null ? null : new CommandOptionImpl(argument);
}

@Override
public List<CommandOption> getOptions()
{
return cmdLine
.getOptions()
.stream()
.map((option) -> new CommandOptionImpl(option))
.collect(Collectors.toList());
}

@Override
public String toString()
{
return "CommandLineImpl [cmdLine=" + cmdLine + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.shell.aesh.line;

import java.util.List;

import org.jboss.aesh.cl.internal.ProcessedOption;
import org.jboss.forge.addon.shell.line.CommandOption;

/**
* Default {@link CommandOption} implementation
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*/
class CommandOptionImpl implements CommandOption
{
private final ProcessedOption option;

public CommandOptionImpl(ProcessedOption option)
{
this.option = option;
}

@Override
public String getName()
{
return option.getName();
}

@Override
public String getValue()
{
return option.getValue();
}

@Override
public List<String> getValues()
{
return option.getValues();
}

@Override
public String toString()
{
return "CommandOptionImpl [option=" + option + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import org.jboss.forge.addon.resource.Resource;
import org.jboss.forge.addon.shell.Shell;
import org.jboss.forge.addon.shell.aesh.line.CommandLineImpl;
import org.jboss.forge.addon.shell.line.CommandLine;
import org.jboss.forge.addon.ui.context.AbstractUIContext;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIContextListener;
Expand Down Expand Up @@ -79,4 +81,12 @@ public boolean isVerbose()
Object verboseFlag = getAttributeMap().get("VERBOSE");
return (verboseFlag != null && "true".equalsIgnoreCase(verboseFlag.toString()));
}

@Override
public CommandLine getCommandLine()
{
org.jboss.aesh.cl.CommandLine<?> cmdLine = (org.jboss.aesh.cl.CommandLine<?>) getAttributeMap()
.get(org.jboss.aesh.cl.CommandLine.class);
return new CommandLineImpl(cmdLine);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package org.jboss.forge.addon.shell.mock.command;

import java.util.ArrayList;
import java.util.Arrays;

import javax.inject.Inject;
Expand All @@ -17,9 +16,6 @@
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.context.UIValidationContext;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.UICompleter;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.input.UIInputMany;
import org.jboss.forge.addon.ui.input.UISelectMany;
Expand Down Expand Up @@ -94,31 +90,13 @@ public boolean isEnabled(UIContext context)
public void initializeUI(UIBuilder builder) throws Exception
{
valueWithSpaces.setValueChoices(Arrays.asList("Value 1", "Value 2", "Value 10", "Value 100"));
help.setCompleter(new UICompleter<String>()
{
@Override
public Iterable<String> getCompletionProposals(UIContext context, InputComponent<?, String> input, String value)
{
if (value == null || value.length() == 0)
{
ArrayList<String> list = new ArrayList<>();
list.add("HELP");
list.add("HALP");
return list;
}
return null;
}
});
help.setCompleter(
(context, input, value) -> (value == null || value.isEmpty()) ? Arrays.asList("HELP", "HALP") : null);

builder.add(name).add(help).add(bool).add(bar).add(bar2).add(targetLocation).add(valueWithSpaces).add(career)
.add(manyCareer).add(disabledOption).add(arguments);
}

@Override
public void validate(UIValidationContext context)
{
}

@Override
public Result execute(UIExecutionContext context) throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.shell;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.addon.shell.line.CommandLine;
import org.jboss.forge.addon.shell.line.CommandOption;
import org.jboss.forge.addon.shell.mock.command.Career;
import org.jboss.forge.addon.shell.mock.command.FooCommand;
import org.jboss.forge.addon.shell.test.ShellTest;
import org.jboss.forge.addon.shell.ui.ShellContext;
import org.jboss.forge.addon.ui.command.AbstractCommandExecutionListener;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.arquillian.AddonDependencies;
import org.jboss.forge.arquillian.AddonDependency;
import org.jboss.forge.arquillian.archive.AddonArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
@RunWith(Arquillian.class)
public class ShellCommandLineTest
{
@Deployment
@AddonDependencies({
@AddonDependency(name = "org.jboss.forge.addon:shell-test-harness"),
@AddonDependency(name = "org.jboss.forge.furnace.container:cdi")

})
public static AddonArchive getDeployment()
{
AddonArchive archive = ShrinkWrap.create(AddonArchive.class)
.addBeansXML()
.addClasses(FooCommand.class, Career.class);

return archive;
}

@Inject
private ShellTest shellTest;

@Before
public void setUp() throws Exception
{
shellTest.clearScreen();
}

@After
public void tearDown() throws Exception
{
shellTest.close();
}

@Test(timeout = 10000)
public void testCommandLineShouldContainExecutionData() throws Exception
{
final AtomicReference<CommandLine> ref = new AtomicReference<>();
shellTest.getShell().addCommandExecutionListener(new AbstractCommandExecutionListener()
{
@Override
public void preCommandExecuted(UICommand command, UIExecutionContext context)
{
ShellContext uiContext = (ShellContext) context.getUIContext();
ref.set(uiContext.getCommandLine());
}
});
shellTest.execute("foocommand --name George --help HALP", 10, TimeUnit.SECONDS);
CommandLine commandLine = ref.get();
Assert.assertThat(commandLine, notNullValue());
Assert.assertThat(commandLine.getArgument(), nullValue());
List<CommandOption> options = commandLine.getOptions();
Assert.assertThat(options.size(), equalTo(2));
Assert.assertThat(options.get(0).getName(), equalTo("name"));
Assert.assertThat(options.get(0).getValue(), equalTo("George"));
Assert.assertThat(options.get(1).getName(), equalTo("help"));
Assert.assertThat(options.get(1).getValue(), equalTo("HALP"));
}
}

0 comments on commit 143f930

Please sign in to comment.