Permalink
Browse files

Extract command parsing into own class

This way, it can be easily tested and the code does not clutter the main
application class.
  • Loading branch information...
tkrille committed May 28, 2017
1 parent 58d96e9 commit 413038ea929c74238c260fbb00fbf0a89c2b9020
@@ -24,6 +24,7 @@
package org.osiam;
import com.google.common.collect.ImmutableMap;
import org.osiam.cli.CliCommand;
import org.osiam.cli.InitHome;
import org.osiam.cli.MigrateDb;
import org.springframework.boot.SpringApplication;
@@ -38,7 +39,6 @@
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
@@ -56,7 +56,7 @@
public static void main(String[] args) {
SpringApplication application = new SpringApplication();
String command = extractCommand(args);
String command = new CliCommand(args).get();
OsiamHome osiamHome = new OsiamHome();
if ("initHome".equals(command)) {
application.setSources(Collections.<Object>singleton(InitHome.class));
@@ -73,15 +73,6 @@ public static void main(String[] args) {
application.run(args);
}
private static String extractCommand(String[] arguments) {
return Arrays.stream(arguments)
.map(String::trim)
.filter(argument -> !argument.startsWith("-"))
.filter(argument -> !argument.startsWith("--"))
.findFirst()
.orElse("server");
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) {
applicationBuilder.application().setDefaultProperties(DEFAULT_PROPERTIES);
@@ -0,0 +1,46 @@
/**
* The MIT License (MIT)
*
* Copyright (C) 2013-2016 tarent solutions GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.osiam.cli;
import java.util.Arrays;
public class CliCommand {
private static final String DEFAULT_COMMAND = "server";
private final String[] args;
public CliCommand(String[] args) {
this.args = args;
}
public String get() {
return Arrays.stream(args)
.map(String::trim)
.filter(argument -> !argument.startsWith("-"))
.filter(argument -> !argument.startsWith("--"))
.findFirst()
.orElse(DEFAULT_COMMAND);
}
}
@@ -0,0 +1,85 @@
/*
* The MIT License (MIT)
*
* Copyright (C) 2013-2016 tarent solutions GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.osiam.cli
import spock.lang.Specification
class CliCommandSpec extends Specification {
def 'Get the command from the application arguments'() {
given: 'Simple application arguments containing 1 command'
def args = ['command'] as String[]
when: 'Getting the command from the application arguments'
def command = new CliCommand(args).get()
then: 'The contained command is found'
command == 'command'
}
def 'The first command that is found wins'() {
given: 'Application arguments containing 3 commands'
def args = ['command1', 'command2', 'command3'] as String[]
when: 'Getting the command from the application arguments'
def command = new CliCommand(args).get()
then: 'The first command is found'
command == 'command1'
}
def 'Ignores option arguments'() {
given: 'Application arguments containing options'
def args = ['--option=value', 'command', '-o=v'] as String[]
when: 'Getting the command from the application arguments'
def command = new CliCommand(args).get()
then: 'The option arguments are ignored and the command is found'
command == 'command'
}
def 'The default command is "server"'() {
given: 'Application arguments without a command'
def args = [] as String[]
when: 'Getting the command from the application arguments'
def command = new CliCommand(args).get()
then: 'No command is found and the default command is returned'
command == 'server'
}
def 'Whitespace around arguments is ignored'() {
given: 'Application arguments containing leading and trailing whitespace'
def args = [' --option=value ', ' command ', ' -o=v '] as String[]
when: 'Getting the command from the application arguments'
def command = new CliCommand(args).get()
then: 'Whitespace is ignored and the correct command is found'
command == 'command'
}
}

0 comments on commit 413038e

Please sign in to comment.