Siba/src/org/dclermonte/siba/cli/SibaCLI.java

189 lines
4.8 KiB
Java

/*
* Copyright (C) 2016 Didier Clermonté <dclermonte@april.org>
* 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/>.
*/
package org.dclermonte.siba.cli;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ResourceBundle;
import org.apache.commons.compress.archivers.ArchiveException;
import org.dclermonte.siba.gui.SibaGUI;
import org.dclermonte.siba.model.SibaManager;
public class SibaCLI
{
private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.cli.messages"); //$NON-NLS-1$
public static void backup(final File directoryToSave, final File targetDirectory)
throws ArchiveException, IOException
{
File result;
result = SibaManager.backup(directoryToSave, targetDirectory);
System.out.println(BUNDLE.getString("confirmDialog.text") + result.getName()); //$NON-NLS-1$
return;
}
public static void check(final File fileToCheck) throws NoSuchAlgorithmException, IOException
{
boolean result;
result = SibaManager.check(fileToCheck);
String yourFile = BUNDLE.getString("confirmDialogYourString.text"); //$NON-NLS-1$
String good = BUNDLE.getString("confirmDialogGood.text"); //$NON-NLS-1$
String bad = BUNDLE.getString("confirmDialogBad.text"); //$NON-NLS-1$
if (result)
{
System.out.println(yourFile + fileToCheck.getName() + good);
}
else
{
System.out.println(yourFile + fileToCheck.getName() + bad);
}
return;
}
public static void help()
{
System.out.println("usage :");
System.out.println("Siba :");
System.out.println("Siba [-h |-help| --help]");
System.out.println("siba backup directoryToSave [target]");
System.out.println("siba check [filename | filename.tgz | filename.tgz.md5]");
}
public static void run(final String[] args) throws ArchiveException, IOException, NoSuchAlgorithmException
{
int numberParameter = args.length;
if (numberParameter == 0)
{
SibaGUI.run(args);
}
else
{
if ((args[0].equals("-h")) || (args[0].equals("-help")) || (args[0].equals("--help")))
{
help();
}
else if (args[0].equals("backup"))
{
if (numberParameter == 1)
{
System.out.println(BUNDLE.getString("missingDirectoryToSave.text"));
help();
}
else if (numberParameter == 2)
{
File directoryToSave = new File(args[1]);
if (directoryToSave.exists())
{
backup(directoryToSave, new File(System.getProperty("user.dir")));
}
else
{
System.out.println(BUNDLE.getString("directoryToSaveNotExist.text"));
help();
}
}
else if (numberParameter == 3)
{
File directoryToSave = new File(args[1]);
File targetDirectory = new File(args[2]);
if (directoryToSave.exists() && targetDirectory.exists())
{
backup(directoryToSave, targetDirectory);
}
else
{
System.out.println(BUNDLE.getString("directoryNotExist.text"));
help();
}
}
else
{
System.out.println(BUNDLE.getString("badUsage.text"));
help();
}
}
else if (args[0].equals("check"))
{
if (numberParameter == 1)
{
System.out.println(BUNDLE.getString("missingFileToCheck.text"));
help();
}
else
{
String fileToCheckName = args[1];
if (!args[1].endsWith("tgz") && !args[1].endsWith("md5"))
{
fileToCheckName = fileToCheckName.concat(".tgz.md5");
}
else if (args[1].endsWith("tgz"))
{
fileToCheckName = fileToCheckName.concat(".md5");
}
else
{
fileToCheckName = args[1];
}
File fileToCheck = new File(fileToCheckName);
File fileToCheckFinal;
if (!fileToCheck.isAbsolute())
{
fileToCheckFinal = new File(System.getProperty("user.dir") + "/" + fileToCheckName);
}
else
{
fileToCheckFinal = new File(fileToCheckName);
}
if (fileToCheckFinal.exists())
{
check(fileToCheckFinal);
}
else
{
System.out.println(BUNDLE.getString("missingFileToCheck.text"));
help();
}
}
}
else
{
System.out.println(BUNDLE.getString("badUsage.text"));
help();
}
}
}
}