001/*
002 * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.shell.automation.cmds;
020
021import java.io.File;
022import java.util.HashMap;
023import java.util.Map;
024
025import jline.ANSIBuffer;
026
027import org.nuxeo.ecm.automation.client.model.FileBlob;
028import org.nuxeo.shell.Argument;
029import org.nuxeo.shell.Command;
030import org.nuxeo.shell.Context;
031import org.nuxeo.shell.Parameter;
032import org.nuxeo.shell.Shell;
033import org.nuxeo.shell.ShellConsole;
034import org.nuxeo.shell.ShellException;
035import org.nuxeo.shell.automation.RemoteContext;
036import org.nuxeo.shell.automation.Scripting;
037import org.nuxeo.shell.utils.ANSICodes;
038import org.nuxeo.shell.utils.StringUtils;
039
040/**
041 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
042 */
043@Command(name = "script", help = "Run a script on the server")
044public class Script implements Runnable {
045
046    @Context
047    protected RemoteContext ctx;
048
049    @Parameter(name = "-ctx", hasValue = true, help = "Use this to set execution context variables. Syntax is: \"k1=v1,k1=v2\"")
050    protected String ctxVars;
051
052    @Parameter(name = "-s", hasValue = true, help = "Use this to change the separator used in context variables. THe default is ','")
053    protected String sep = ",";
054
055    @Argument(name = "file", index = 0, required = true, help = "The script file. Must have a .mvel or .groovy extension")
056    protected File file;
057
058    @Argument(name = "timeout", index = 0, required = false, help = "Transaction timeout in seconds.")
059    protected Integer timeout;
060
061    @Override
062    public void run() {
063        ShellConsole console = ctx.getShell().getConsole();
064        FileBlob blob = new FileBlob(file);
065        Map<String, Object> args = new HashMap<>();
066        if (ctxVars != null) {
067            for (String pair : ctxVars.split(sep)) {
068                String[] ar = StringUtils.split(pair, '=', true);
069                args.put(ar[0], ar[1]);
070            }
071        }
072        try {
073            String scriptOutput = Scripting.runScript(ctx, blob, args, timeout);
074            if (scriptOutput != null) {
075                ANSIBuffer buf = Shell.get().newANSIBuffer();
076                ANSICodes.appendTemplate(buf, scriptOutput, false);
077                console.println(buf.toString());
078            }
079        } catch (Exception e) {
080            throw new ShellException("Failed to run script", e);
081        }
082    }
083
084}