001/*
002 * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.shell.automation.cmds;
018
019import java.io.File;
020import java.util.HashMap;
021import java.util.Map;
022
023import jline.ANSIBuffer;
024
025import org.nuxeo.ecm.automation.client.model.FileBlob;
026import org.nuxeo.shell.Argument;
027import org.nuxeo.shell.Command;
028import org.nuxeo.shell.Context;
029import org.nuxeo.shell.Parameter;
030import org.nuxeo.shell.Shell;
031import org.nuxeo.shell.ShellConsole;
032import org.nuxeo.shell.ShellException;
033import org.nuxeo.shell.automation.RemoteContext;
034import org.nuxeo.shell.automation.Scripting;
035import org.nuxeo.shell.utils.ANSICodes;
036import org.nuxeo.shell.utils.StringUtils;
037
038/**
039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
040 */
041@Command(name = "script", help = "Run a script on the server")
042public class Script implements Runnable {
043
044    @Context
045    protected RemoteContext ctx;
046
047    @Parameter(name = "-ctx", hasValue = true, help = "Use this to set execution context variables. Syntax is: \"k1=v1,k1=v2\"")
048    protected String ctxVars;
049
050    @Parameter(name = "-s", hasValue = true, help = "Use this to change the separator used in context variables. THe default is ','")
051    protected String sep = ",";
052
053    @Argument(name = "file", index = 0, required = true, help = "The script file. Must have a .mvel or .groovy extension")
054    protected File file;
055
056    @Argument(name = "timeout", index = 0, required = false, help = "Transaction timeout in seconds.")
057    protected Integer timeout;
058
059    @Override
060    public void run() {
061        ShellConsole console = ctx.getShell().getConsole();
062        FileBlob blob = new FileBlob(file);
063        Map<String, Object> args = new HashMap<>();
064        if (ctxVars != null) {
065            for (String pair : ctxVars.split(sep)) {
066                String[] ar = StringUtils.split(pair, '=', true);
067                args.put(ar[0], ar[1]);
068            }
069        }
070        try {
071            String scriptOutput = Scripting.runScript(ctx, blob, args, timeout);
072            if (scriptOutput != null) {
073                ANSIBuffer buf = Shell.get().newANSIBuffer();
074                ANSICodes.appendTemplate(buf, scriptOutput, false);
075                console.println(buf.toString());
076            }
077        } catch (Exception e) {
078            throw new ShellException("Failed to run script", e);
079        }
080    }
081
082}