2016-04-19 22:34:25 +02:00
|
|
|
/*
|
2016-04-26 23:02:26 +02:00
|
|
|
* Copyright (C) 2016 Didier Clermonté <dclermonte@april.org>
|
2016-04-19 22:34:25 +02:00
|
|
|
* Copyright (C) 2016 Christian Pierre Momon <christian.momon@devinsy.fr>
|
|
|
|
*
|
|
|
|
* This file is part of Siba.
|
|
|
|
*
|
|
|
|
* Siba is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-03-29 23:25:18 +02:00
|
|
|
package org.dclermonte.siba.cli;
|
|
|
|
|
2016-05-03 17:36:45 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.util.ResourceBundle;
|
|
|
|
|
2016-05-16 23:40:07 +02:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2016-05-19 18:40:00 +02:00
|
|
|
import org.dclermonte.siba.SibaException;
|
2016-05-03 17:36:45 +02:00
|
|
|
import org.dclermonte.siba.model.SibaManager;
|
2016-06-01 11:56:21 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2016-05-02 22:28:35 +02:00
|
|
|
|
2016-05-16 23:40:07 +02:00
|
|
|
/**
|
2016-05-19 18:40:00 +02:00
|
|
|
* This class CLI is for use from Command Line Interface.
|
2016-05-16 23:40:07 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2016-03-29 23:25:18 +02:00
|
|
|
public class SibaCLI
|
|
|
|
{
|
2016-07-26 17:01:58 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(SibaCLI.class);
|
2016-05-08 22:43:19 +02:00
|
|
|
private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.cli.messages"); //$NON-NLS-1$
|
2016-05-03 17:36:45 +02:00
|
|
|
|
2016-07-27 22:41:03 +02:00
|
|
|
public SibaCLI()
|
|
|
|
{
|
|
|
|
// Set default CLI catch.
|
|
|
|
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void uncaughtException(final Thread thread, final Throwable exception)
|
|
|
|
{
|
|
|
|
String message;
|
|
|
|
if (exception instanceof OutOfMemoryError)
|
|
|
|
{
|
|
|
|
message = "Java ran out of memory!\n\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
message = "An error occured: " + exception.getClass() + "(" + exception.getMessage() + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.error("uncaughtException ", exception);
|
|
|
|
logger.error(message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-31 16:25:35 +02:00
|
|
|
public static void backup(final File directoryToSave) throws SibaException
|
|
|
|
{
|
|
|
|
File result;
|
|
|
|
result = SibaManager.backup(directoryToSave);
|
|
|
|
System.out.println(BUNDLE.getString("confirmDialog.text") + result.getName()); //$NON-NLS-1$
|
|
|
|
}
|
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
|
|
|
*
|
2016-05-19 18:40:00 +02:00
|
|
|
* This method is called for Backup from CLI.
|
2016-05-18 23:20:29 +02:00
|
|
|
*
|
|
|
|
* @param directoryToSave
|
|
|
|
* @param targetDirectory
|
2016-05-19 18:40:00 +02:00
|
|
|
* @throws SibaException
|
2016-05-18 23:20:29 +02:00
|
|
|
*/
|
2016-05-22 21:47:41 +02:00
|
|
|
public static void backup(final File directoryToSave, final File targetDirectory) throws SibaException
|
2016-05-03 17:36:45 +02:00
|
|
|
{
|
|
|
|
File result;
|
|
|
|
result = SibaManager.backup(directoryToSave, targetDirectory);
|
2016-05-08 22:43:19 +02:00
|
|
|
System.out.println(BUNDLE.getString("confirmDialog.text") + result.getName()); //$NON-NLS-1$
|
2016-05-03 17:36:45 +02:00
|
|
|
}
|
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
|
|
|
*
|
2016-05-22 21:47:41 +02:00
|
|
|
* This method is called for Check from CLI. anObject
|
2016-05-18 23:20:29 +02:00
|
|
|
*
|
|
|
|
* @param fileToCheck
|
2016-05-22 21:47:41 +02:00
|
|
|
* @throws SibaException
|
2016-05-18 23:20:29 +02:00
|
|
|
*/
|
2016-05-22 21:47:41 +02:00
|
|
|
public static void check(final File fileToCheck) throws SibaException
|
2016-05-03 17:36:45 +02:00
|
|
|
{
|
|
|
|
boolean result;
|
|
|
|
result = SibaManager.check(fileToCheck);
|
|
|
|
if (result)
|
|
|
|
{
|
2016-06-07 00:21:31 +02:00
|
|
|
String message = String.format(BUNDLE.getString("confirmDialogGood.text"), fileToCheck.getName());
|
|
|
|
System.out.println(message);
|
2016-05-03 17:36:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-07 00:21:31 +02:00
|
|
|
String message = String.format(BUNDLE.getString("confirmDialogBad.text"), fileToCheck.getName());
|
|
|
|
System.out.println(message);
|
2016-05-03 17:36:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
|
|
|
*
|
2016-05-19 18:40:00 +02:00
|
|
|
* This method send help response.
|
2016-05-18 23:20:29 +02:00
|
|
|
*
|
|
|
|
*/
|
2016-05-02 22:28:35 +02:00
|
|
|
public static void help()
|
|
|
|
{
|
2016-05-08 22:43:19 +02:00
|
|
|
System.out.println("usage :");
|
|
|
|
System.out.println("Siba :");
|
2016-05-02 22:28:35 +02:00
|
|
|
System.out.println("Siba [-h |-help| --help]");
|
2016-05-08 22:43:19 +02:00
|
|
|
System.out.println("siba backup directoryToSave [target]");
|
|
|
|
System.out.println("siba check [filename | filename.tgz | filename.tgz.md5]");
|
2016-05-02 22:28:35 +02:00
|
|
|
}
|
|
|
|
|
2016-05-15 16:56:10 +02:00
|
|
|
/**
|
2016-05-18 23:20:29 +02:00
|
|
|
*
|
2016-05-19 18:40:00 +02:00
|
|
|
* This method launch CLI.
|
2016-05-15 16:56:10 +02:00
|
|
|
*
|
|
|
|
* @param args
|
|
|
|
*/
|
2016-05-19 18:40:00 +02:00
|
|
|
public static void run(final String[] args)
|
2016-05-02 22:28:35 +02:00
|
|
|
{
|
2016-05-18 23:20:29 +02:00
|
|
|
try
|
2016-05-08 22:43:19 +02:00
|
|
|
{
|
2016-05-18 23:20:29 +02:00
|
|
|
// This part implements an automate.
|
|
|
|
int parameterCount = args.length;
|
|
|
|
if (parameterCount == 0)
|
2016-05-08 22:43:19 +02:00
|
|
|
{
|
2016-05-02 22:28:35 +02:00
|
|
|
help();
|
2016-05-08 22:43:19 +02:00
|
|
|
}
|
2016-05-22 21:47:41 +02:00
|
|
|
else if (StringUtils.equals(args[0], "-h") || StringUtils.equals(args[0], "-help")
|
|
|
|
|| StringUtils.equals(args[0], "--help"))
|
|
|
|
{
|
|
|
|
help();
|
|
|
|
}
|
|
|
|
else if (StringUtils.equals(args[0], "backup"))
|
2016-05-08 22:43:19 +02:00
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
if (parameterCount == 1)
|
2016-05-08 22:43:19 +02:00
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
backup(null, null);
|
|
|
|
}
|
|
|
|
else if (parameterCount == 2)
|
|
|
|
{
|
2016-05-31 16:25:35 +02:00
|
|
|
backup(new File(args[1]));
|
2016-05-22 21:47:41 +02:00
|
|
|
}
|
|
|
|
else if (parameterCount == 3)
|
|
|
|
{
|
|
|
|
backup(new File(args[1]), new File(args[2]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-25 18:10:34 +02:00
|
|
|
throw new SibaException(BUNDLE.getString("ToManyParameter.text")); //$NON-NLS-1$
|
2016-05-08 22:43:19 +02:00
|
|
|
}
|
2016-05-22 21:47:41 +02:00
|
|
|
}
|
|
|
|
else if (StringUtils.equals(args[0], "check"))
|
|
|
|
{
|
|
|
|
if (parameterCount == 1)
|
2016-05-03 17:36:45 +02:00
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
check(null);
|
2016-05-03 17:36:45 +02:00
|
|
|
}
|
2016-05-22 21:47:41 +02:00
|
|
|
else if (parameterCount == 2)
|
2016-05-03 17:36:45 +02:00
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
String fileToCheckName;
|
|
|
|
if (!args[1].endsWith("tgz") && !args[1].endsWith("md5"))
|
|
|
|
{
|
|
|
|
fileToCheckName = args[1].concat(".tgz.md5");
|
|
|
|
}
|
|
|
|
else if (args[1].endsWith("tgz"))
|
2016-05-08 22:43:19 +02:00
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
fileToCheckName = args[1].concat(".md5");
|
2016-05-08 22:43:19 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
fileToCheckName = args[1];
|
2016-05-08 22:43:19 +02:00
|
|
|
}
|
2016-05-22 21:47:41 +02:00
|
|
|
File fileToCheck = new File(fileToCheckName);
|
|
|
|
check(fileToCheck);
|
2016-05-18 23:20:29 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-25 18:10:34 +02:00
|
|
|
throw new SibaException(BUNDLE.getString("ToManyParameter.text")); //$NON-NLS-1$
|
2016-05-03 17:36:45 +02:00
|
|
|
}
|
2016-05-08 22:43:19 +02:00
|
|
|
}
|
2016-05-22 21:47:41 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
System.out.println(BUNDLE.getString("badUsage.text"));
|
|
|
|
help();
|
|
|
|
}
|
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
}
|
2016-06-21 15:05:12 +02:00
|
|
|
catch (SibaException exception)
|
2016-05-18 23:20:29 +02:00
|
|
|
{
|
2016-06-21 15:05:12 +02:00
|
|
|
System.err.println("SibaException = " + exception.getMessage());
|
|
|
|
logger.error(exception.getMessage(), exception);
|
2016-05-25 18:10:34 +02:00
|
|
|
help();
|
2016-05-02 22:28:35 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-29 23:25:18 +02:00
|
|
|
}
|