001/*
002 * Copyright (c) 2012-2018 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others
003 *
004 * This program and the accompanying materials are made available under the
005 * terms of the Eclipse Public License 2.0 which is available at
006 * http://www.eclipse.org/legal/epl-2.0.
007 *
008 * SPDX-License-Identifier: EPL-2.0
009 */
010
011package org.eclipse.golo.cli.command;
012
013import com.beust.jcommander.Parameter;
014import com.beust.jcommander.Parameters;
015import com.beust.jcommander.ParametersDelegate;
016import org.eclipse.golo.cli.command.spi.CliCommand;
017
018import java.util.LinkedList;
019import java.util.List;
020
021import static gololang.Messages.error;
022import static gololang.Messages.message;
023
024@Parameters(commandNames = {"run"}, resourceBundle = "commands", commandDescriptionKey = "run")
025public class RunCommand implements CliCommand {
026
027  @Parameter(names = "--module", descriptionKey = "main_module", required = true)
028  String module;
029
030  @Parameter(descriptionKey = "arguments")
031  List<String> arguments = new LinkedList<>();
032
033  @ParametersDelegate
034  ClasspathOption classpath = new ClasspathOption();
035
036  @Override
037  public void execute() throws Throwable {
038    try {
039      Class<?> module = Class.forName(this.module, true, classpath.initGoloClassLoader());
040      callRun(module, this.arguments.toArray(new String[this.arguments.size()]));
041    } catch (ClassNotFoundException e) {
042      error(message("module_not_found", this.module));
043    } catch (NoMainMethodException e) {
044      error(message("module_no_main", this.module));
045    }
046  }
047}