001/*
002 * (C) Copyright 2006-2010 Nuxeo SAS (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.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.cmds;
018
019import java.io.File;
020
021import org.nuxeo.shell.Argument;
022import org.nuxeo.shell.Command;
023import org.nuxeo.shell.Context;
024import org.nuxeo.shell.Parameter;
025import org.nuxeo.shell.Shell;
026import org.nuxeo.shell.ShellException;
027import org.nuxeo.shell.fs.cmds.Cat;
028
029/**
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
031 */
032@Command(name = "settings", help = "Print or modify the shell settings.")
033public class Settings implements Runnable {
034
035    @Context
036    protected Shell shell;
037
038    @Argument(name = "name", index = 0, required = false, help = "The variable to print or set.")
039    protected String name;
040
041    @Argument(name = "value", index = 1, required = false, help = "The variable value to set.")
042    protected String value;
043
044    @Parameter(name = "-reset", hasValue = false, help = "Reset settings to their defaults. Need to restart shell.")
045    protected boolean reset;
046
047    public void run() {
048        File file = shell.getSettingsFile();
049        if (reset) {
050            file.delete();
051            return;
052        }
053        try {
054            if (name == null) {
055                Cat.cat(shell.getConsole(), file);
056            } else if (value == null) {
057                shell.getConsole().println((String) shell.getSetting(name, "NULL"));
058            } else {
059                shell.setSetting(name, value);
060            }
061        } catch (Exception e) {
062            throw new ShellException(e);
063        }
064    }
065}