@Override protected ExitStatus run(OptionSet options) throws Exception { SourceOptions sourceOptions = new SourceOptions(options); List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory .createDefaultRepositoryConfiguration(); GroovyCompilerConfiguration configuration = new OptionSetGroovyCompilerConfiguration( options, this, repositoryConfiguration); if (System.getProperty("grape.root") == null) { System.setProperty("grape.root", "."); } GroovyCompiler groovyCompiler = new GroovyCompiler(configuration); groovyCompiler.compile(sourceOptions.getSourcesArray()); return ExitStatus.OK; }
/** * Parse the arguments and run a suitable command. * @param args the arguments * @return the outcome of the command * @throws Exception if the command fails */ protected ExitStatus run(String... args) throws Exception { if (args.length == 0) { throw new NoArgumentsException(); } String commandName = args[0]; String[] commandArguments = Arrays.copyOfRange(args, 1, args.length); Command command = findCommand(commandName); if (command == null) { throw new NoSuchCommandException(commandName); } beforeRun(command); try { return command.run(commandArguments); } finally { afterRun(command); } }
@Override @SuppressWarnings("unchecked") protected ExitStatus run(OptionSet options) throws Exception { List<String> args = (List<String>) options.nonOptionArguments(); try { if (options.has(this.allOption)) { if (!args.isEmpty()) { throw new IllegalArgumentException( "Please use --all without specifying any dependencies"); } new Installer(options, this).uninstallAll(); } if (args.isEmpty()) { throw new IllegalArgumentException( "Please specify at least one dependency, in the form group:artifact:version, to uninstall"); } new Installer(options, this).uninstall(args); } catch (Exception ex) { String message = ex.getMessage(); Log.error(message != null ? message : ex.getClass().toString()); } return ExitStatus.OK; }
@Override protected synchronized ExitStatus run(OptionSet options) throws Exception { synchronized (this.monitor) { if (this.runner != null) { throw new RuntimeException( "Already running. Please stop the current application before running another (use the 'stop' command)."); } SourceOptions sourceOptions = new SourceOptions(options); List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory .createDefaultRepositoryConfiguration(); repositoryConfiguration.add(0, new RepositoryConfiguration("local", new File("repository").toURI(), true)); SpringApplicationRunnerConfiguration configuration = new SpringApplicationRunnerConfigurationAdapter( options, this, repositoryConfiguration); this.runner = new SpringApplicationRunner(configuration, sourceOptions.getSourcesArray(), sourceOptions.getArgsArray()); this.runner.compileAndRun(); return ExitStatus.OK; } }
@Test public void generateProject() throws Exception { String fileName = UUID.randomUUID().toString() + ".zip"; File file = new File(fileName); assertThat(file.exists()).as("file should not exist").isFalse(); MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest( "application/zip", fileName); mockSuccessfulProjectGeneration(request); try { assertThat(this.command.run()).isEqualTo(ExitStatus.OK); assertThat(file.exists()).as("file should have been created").isTrue(); } finally { assertThat(file.delete()).as("failed to delete test file").isTrue(); } }
@Test public void generateProjectArchiveExtractedByDefault() throws Exception { String fileName = UUID.randomUUID().toString(); assertThat(fileName.contains(".")).as("No dot in filename").isFalse(); byte[] archive = createFakeZipArchive("test.txt", "Fake content"); MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest( "application/zip", "demo.zip", archive); mockSuccessfulProjectGeneration(request); File file = new File(fileName); File archiveFile = new File(file, "test.txt"); try { assertThat(this.command.run(fileName)).isEqualTo(ExitStatus.OK); assertThat(archiveFile).exists(); } finally { archiveFile.delete(); file.delete(); } }
@Test public void generateProjectFileSavedAsFileByDefault() throws Exception { String fileName = UUID.randomUUID().toString(); String content = "Fake Content"; byte[] archive = content.getBytes(); MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest( "application/octet-stream", "pom.xml", archive); mockSuccessfulProjectGeneration(request); File file = new File(fileName); try { assertThat(this.command.run(fileName)).isEqualTo(ExitStatus.OK); assertThat(file.exists()).as("File not saved properly").isTrue(); assertThat(file.isFile()).as("Should not be a directory").isTrue(); } finally { file.delete(); } }
@Test public void generateProjectAndExtractUnsupportedArchive() throws Exception { File folder = this.temporaryFolder.newFolder(); String fileName = UUID.randomUUID().toString() + ".zip"; File file = new File(fileName); assertThat(file.exists()).as("file should not exist").isFalse(); try { byte[] archive = createFakeZipArchive("test.txt", "Fake content"); MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest( "application/foobar", fileName, archive); mockSuccessfulProjectGeneration(request); assertThat(this.command.run("--extract", folder.getAbsolutePath())) .isEqualTo(ExitStatus.OK); assertThat(file.exists()).as("file should have been saved instead").isTrue(); } finally { assertThat(file.delete()).as("failed to delete test file").isTrue(); } }
@Test public void generateProjectAndExtractUnknownContentType() throws Exception { File folder = this.temporaryFolder.newFolder(); String fileName = UUID.randomUUID().toString() + ".zip"; File file = new File(fileName); assertThat(file.exists()).as("file should not exist").isFalse(); try { byte[] archive = createFakeZipArchive("test.txt", "Fake content"); MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest( null, fileName, archive); mockSuccessfulProjectGeneration(request); assertThat(this.command.run("--extract", folder.getAbsolutePath())) .isEqualTo(ExitStatus.OK); assertThat(file.exists()).as("file should have been saved instead").isTrue(); } finally { assertThat(file.delete()).as("failed to delete test file").isTrue(); } }
@Test public void fileInArchiveNotOverwrittenByDefault() throws Exception { File folder = this.temporaryFolder.newFolder(); File conflict = new File(folder, "test.txt"); assertThat(conflict.createNewFile()).as("Should have been able to create file") .isTrue(); long fileLength = conflict.length(); // also contains test.txt byte[] archive = createFakeZipArchive("test.txt", "Fake content"); MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest( "application/zip", "demo.zip", archive); mockSuccessfulProjectGeneration(request); assertThat(this.command.run("--extract", folder.getAbsolutePath())) .isEqualTo(ExitStatus.ERROR); assertThat(conflict.length()).as("File should not have changed") .isEqualTo(fileLength); }
@Test public void overwriteFileInArchive() throws Exception { File folder = this.temporaryFolder.newFolder(); File conflict = new File(folder, "test.txt"); assertThat(conflict.createNewFile()).as("Should have been able to create file") .isTrue(); long fileLength = conflict.length(); // also contains test.txt byte[] archive = createFakeZipArchive("test.txt", "Fake content"); MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest( "application/zip", "demo.zip", archive); mockSuccessfulProjectGeneration(request); assertThat(this.command.run("--force", "--extract", folder.getAbsolutePath())) .isEqualTo(ExitStatus.OK); assertThat(fileLength != conflict.length()).as("File should have changed") .isTrue(); }
@Override protected synchronized ExitStatus run(OptionSet options) throws Exception { if (this.runner != null) { throw new RuntimeException( "Already running. Please stop the current application before running another (use the 'stop' command)."); } SourceOptions sourceOptions = new SourceOptions(options); List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory .createDefaultRepositoryConfiguration(); repositoryConfiguration.add(0, new RepositoryConfiguration("local", new File("repository").toURI(), true)); SpringApplicationRunnerConfiguration configuration = new SpringApplicationRunnerConfigurationAdapter( options, this, repositoryConfiguration); this.runner = new SpringApplicationRunner(configuration, sourceOptions.getSourcesArray(), sourceOptions.getArgsArray()); this.runner.compileAndRun(); return ExitStatus.OK; }
/** * Run the command using the specified parsed {@link OptionSet}. * @param options the parsed option set * @return an ExitStatus * @throws Exception in case of errors */ protected ExitStatus run(OptionSet options) throws Exception { if (this.closure != null) { Object result = this.closure.call(options); if (result instanceof ExitStatus) { return (ExitStatus) result; } if (result instanceof Boolean) { return (Boolean) result ? ExitStatus.OK : ExitStatus.ERROR; } if (result instanceof Integer) { return new ExitStatus((Integer) result, "Finished"); } } return ExitStatus.OK; }
@Test public void generateProject() throws Exception { String fileName = UUID.randomUUID().toString() + ".zip"; File file = new File(fileName); assertFalse("file should not exist", file.exists()); MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest( "application/zip", fileName); mockSuccessfulProjectGeneration(request); try { assertEquals(ExitStatus.OK, this.command.run()); assertTrue("file should have been created", file.exists()); } finally { assertTrue("failed to delete test file", file.delete()); } }
@Test public void generateProjectArchiveExtractedByDefault() throws Exception { String fileName = UUID.randomUUID().toString(); assertFalse("No dot in filename", fileName.contains(".")); byte[] archive = createFakeZipArchive("test.txt", "Fake content"); MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest( "application/zip", "demo.zip", archive); mockSuccessfulProjectGeneration(request); File file = new File(fileName); File archiveFile = new File(file, "test.txt"); try { assertEquals(ExitStatus.OK, this.command.run(fileName)); assertTrue("Archive not extracted properly " + archiveFile.getAbsolutePath() + " not found", archiveFile.exists()); } finally { archiveFile.delete(); file.delete(); } }
@Test public void generateProjectFileSavedAsFileByDefault() throws Exception { String fileName = UUID.randomUUID().toString(); String content = "Fake Content"; byte[] archive = content.getBytes(); MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest( "application/octet-stream", "pom.xml", archive); mockSuccessfulProjectGeneration(request); File file = new File(fileName); try { assertEquals(ExitStatus.OK, this.command.run(fileName)); assertTrue("File not saved properly", file.exists()); assertTrue("Should not be a directory", file.isFile()); } finally { file.delete(); } }
@Test public void generateProjectAndExtractUnsupportedArchive() throws Exception { File folder = this.temporaryFolder.newFolder(); String fileName = UUID.randomUUID().toString() + ".zip"; File file = new File(fileName); assertFalse("file should not exist", file.exists()); try { byte[] archive = createFakeZipArchive("test.txt", "Fake content"); MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest( "application/foobar", fileName, archive); mockSuccessfulProjectGeneration(request); assertEquals(ExitStatus.OK, this.command.run("--extract", folder.getAbsolutePath())); assertTrue("file should have been saved instead", file.exists()); } finally { assertTrue("failed to delete test file", file.delete()); } }
@Test public void generateProjectAndExtractUnknownContentType() throws Exception { File folder = this.temporaryFolder.newFolder(); String fileName = UUID.randomUUID().toString() + ".zip"; File file = new File(fileName); assertFalse("file should not exist", file.exists()); try { byte[] archive = createFakeZipArchive("test.txt", "Fake content"); MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest( null, fileName, archive); mockSuccessfulProjectGeneration(request); assertEquals(ExitStatus.OK, this.command.run("--extract", folder.getAbsolutePath())); assertTrue("file should have been saved instead", file.exists()); } finally { assertTrue("failed to delete test file", file.delete()); } }
@Override protected synchronized ExitStatus run(OptionSet options) throws Exception { String charset = "UTF-8"; if(options.has(charsetOption)){ charset = options.valueOf(charsetOption); } String text = StringUtils .collectionToDelimitedString(options.nonOptionArguments(), " "); try { Charset.forName(charset); String outText = URLDecoder.decode(text, charset); System.out.println(outText); return ExitStatus.OK; } catch (UnsupportedCharsetException e){ System.out.println("Unsupported Character Set"); return ExitStatus.ERROR; } }
@Override protected synchronized ExitStatus run(OptionSet options) throws Exception { String charset = "UTF-8"; if(options.has(charsetOption)){ charset = options.valueOf(charsetOption); } String text = StringUtils .collectionToDelimitedString(options.nonOptionArguments(), " "); try { Charset.forName(charset); String outText = URLEncoder.encode(text, charset); System.out.println(outText); return ExitStatus.OK; } catch (UnsupportedCharsetException e){ System.out.println("Unsupported Character Set"); return ExitStatus.ERROR; } }
@Override protected synchronized ExitStatus run(OptionSet options) throws Exception { if (options.has(this.versionOption)) { System.out.println("Spring Cloud CLI v" + getVersion()); return ExitStatus.OK; } try { URLClassLoader classLoader = populateClassloader(options); // This is the main class in the deployer archive: String name = "org.springframework.boot.loader.wrapper.ThinJarWrapper"; Class<?> threadClass = classLoader.loadClass(name); URL url = classLoader.getURLs()[0]; threadClass.getMethod("main", String[].class).invoke(null, new Object[] { getArgs(options, url) }); } catch (Exception e) { log.error("Error running spring cloud", e); return ExitStatus.ERROR; } return ExitStatus.OK; }
@Override protected ExitStatus run(OptionSet options) throws Exception { List<?> nonOptionArguments = new ArrayList<Object>( options.nonOptionArguments()); Assert.isTrue(nonOptionArguments.size() >= 2, "The name of the resulting " + this.type + " and at least one source file must be specified"); File output = new File((String) nonOptionArguments.remove(0)); Assert.isTrue(output.getName().toLowerCase().endsWith("." + this.type), "The output '" + output + "' is not a " + this.type.toUpperCase() + " file."); deleteIfExists(output); GroovyCompiler compiler = createCompiler(options); List<URL> classpath = getClassPathUrls(compiler); List<MatchedResource> classpathEntries = findMatchingClasspathEntries( classpath, options); String[] sources = new SourceOptions(nonOptionArguments).getSourcesArray(); Class<?>[] compiledClasses = compiler.compile(sources); List<URL> dependencies = getClassPathUrls(compiler); dependencies.removeAll(classpath); writeJar(output, compiledClasses, classpathEntries, dependencies); return ExitStatus.OK; }
public final ExitStatus run(String... args) throws Exception { String[] argsToUse = args.clone(); for (int i = 0; i < argsToUse.length; i++) { if ("-cp".equals(argsToUse[i])) { argsToUse[i] = "--cp"; } } OptionSet options = getParser().parse(argsToUse); return run(options); }
@Override @SuppressWarnings("unchecked") protected ExitStatus run(OptionSet options) throws Exception { List<String> args = (List<String>) options.nonOptionArguments(); Assert.notEmpty(args, "Please specify at least one " + "dependency, in the form group:artifact:version, to install"); try { new Installer(options, this).install(args); } catch (Exception ex) { String message = ex.getMessage(); Log.error(message != null ? message : ex.getClass().toString()); } return ExitStatus.OK; }
protected ExitStatus run(Collection<String> args) throws IOException { this.process = new RunProcess(this.command); int code = this.process.run(true, args.toArray(new String[args.size()])); if (code == 0) { return ExitStatus.OK; } else { return new ExitStatus(code, "EXTERNAL_ERROR"); } }
@Override public ExitStatus run(String... args) throws Exception { List<String> fullArgs = new ArrayList<String>(); fullArgs.add("-cp"); fullArgs.add(System.getProperty("java.class.path")); fullArgs.add(MAIN_CLASS); fullArgs.add(this.command.getName()); fullArgs.addAll(Arrays.asList(args)); run(fullArgs); return ExitStatus.OK; }