|
| 1 | +package org.mule.weave.cli.pico; |
| 2 | + |
| 3 | +import org.mule.weave.dwnative.cli.Console; |
| 4 | +import org.mule.weave.dwnative.cli.DefaultConsole$; |
| 5 | +import org.mule.weave.dwnative.cli.commands.VerifyWeaveCommand; |
| 6 | +import org.mule.weave.dwnative.cli.commands.WeaveModule; |
| 7 | +import org.mule.weave.dwnative.cli.commands.WeaveVerifyConfig; |
| 8 | +import org.mule.weave.v2.io.FileHelper; |
| 9 | +import org.mule.weave.v2.parser.ast.variables.NameIdentifier; |
| 10 | +import org.mule.weave.v2.utils.DataWeaveVersion; |
| 11 | +import picocli.CommandLine; |
| 12 | +import scala.Option; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.util.concurrent.Callable; |
| 16 | + |
| 17 | +import static org.mule.weave.cli.pico.AbstractPicoExecCommand.calculateRuntimeVersion; |
| 18 | +import static org.mule.weave.cli.pico.AbstractPicoExecCommand.fileToString; |
| 19 | + |
| 20 | +@CommandLine.Command( |
| 21 | + name = "validate", |
| 22 | + description = "Validate if a script is valid or not." |
| 23 | +) |
| 24 | +public class PicoValidateScript implements Callable<Integer> { |
| 25 | + |
| 26 | + Console console; |
| 27 | + |
| 28 | + @CommandLine.Option(names = {"--language-level"}, description = {"The version of DW to be supported."}) |
| 29 | + protected String languageLevel = null; |
| 30 | + |
| 31 | + @CommandLine.Parameters( |
| 32 | + index = "0", |
| 33 | + arity = "0..1", |
| 34 | + description = "The DW script to be used", |
| 35 | + paramLabel = "SCRIPT" |
| 36 | + ) |
| 37 | + String script = null; |
| 38 | + |
| 39 | + @CommandLine.Option(names = {"--file", "-f"}, description = "The Path to the dw file to run.") |
| 40 | + File dwFile = null; |
| 41 | + |
| 42 | + @CommandLine.Option(names = {"--input", "-i"}, description = "The name of an in implicit input.") |
| 43 | + String[] inputs = new String[0]; |
| 44 | + |
| 45 | + @CommandLine.Spec |
| 46 | + CommandLine.Model.CommandSpec spec = null; |
| 47 | + |
| 48 | + |
| 49 | + public PicoValidateScript() { |
| 50 | + this.console = DefaultConsole$.MODULE$; |
| 51 | + } |
| 52 | + |
| 53 | + public PicoValidateScript(Console console) { |
| 54 | + this.console = console; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Integer call() { |
| 59 | + if ((script == null && dwFile == null) || (script != null && dwFile != null)) { |
| 60 | + String msg = "The script and file parameters are mutually exclusive, but one is required."; |
| 61 | + throw new CommandLine.ParameterException(spec.commandLine(), msg); |
| 62 | + } |
| 63 | + Option<DataWeaveVersion> dataWeaveVersionOption = calculateRuntimeVersion(languageLevel, spec); |
| 64 | + |
| 65 | + WeaveVerifyConfig config = new WeaveVerifyConfig( |
| 66 | + |
| 67 | + ((nr) -> { |
| 68 | + if (script != null) { |
| 69 | + return new WeaveModule(script, NameIdentifier.ANONYMOUS_NAME().name()); |
| 70 | + } else if (dwFile != null) { |
| 71 | + return new WeaveModule(fileToString(dwFile), FileHelper.baseName(dwFile)); |
| 72 | + } else { |
| 73 | + throw new RuntimeException("Missing dw script or main file"); |
| 74 | + } |
| 75 | + }), |
| 76 | + dataWeaveVersionOption, |
| 77 | + inputs |
| 78 | + |
| 79 | + ); |
| 80 | + final VerifyWeaveCommand command = new VerifyWeaveCommand(config, console); |
| 81 | + return command.exec(); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | +} |
0 commit comments