2021-12-01 14:36:07 +01:00
|
|
|
package de.jotoho.waituntil;
|
|
|
|
|
2022-01-28 00:31:59 +01:00
|
|
|
/*
|
|
|
|
waituntil - a tool for delaying command execution until the specified time
|
|
|
|
Copyright (C) 2022 Jonas Tobias Hopusch
|
|
|
|
|
|
|
|
This program 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-03-12 15:24:57 +01:00
|
|
|
import org.apache.commons.cli.CommandLine;
|
2022-03-12 03:16:00 +01:00
|
|
|
import org.apache.commons.cli.DefaultParser;
|
2022-03-12 15:24:57 +01:00
|
|
|
import org.apache.commons.cli.Options;
|
2022-03-12 03:16:00 +01:00
|
|
|
import org.apache.commons.cli.ParseException;
|
2021-12-01 14:36:07 +01:00
|
|
|
|
|
|
|
import static de.jotoho.waituntil.GlobalConf.applicationOutputLanguage;
|
2022-03-12 03:43:03 +01:00
|
|
|
import static java.lang.System.Logger.Level;
|
2021-12-01 14:36:07 +01:00
|
|
|
|
2022-03-12 15:24:57 +01:00
|
|
|
// This file contains the main function and other utility function necessary
|
|
|
|
// for interpreting the terminal arguments.
|
2021-12-01 14:36:07 +01:00
|
|
|
// See README.md and LICENSE.md for license information
|
|
|
|
// Author: Jonas Tobias Hopusch (@jotoho)
|
|
|
|
|
|
|
|
public final class Main {
|
2022-03-12 03:43:03 +01:00
|
|
|
private static final System.Logger logger = System.getLogger("main");
|
2021-12-01 14:36:07 +01:00
|
|
|
|
2022-03-12 03:16:00 +01:00
|
|
|
private static void printVersionInformation() {
|
|
|
|
final var thisPackage = Main.class.getPackage();
|
2022-03-12 15:40:06 +01:00
|
|
|
final var appVersion = thisPackage.getImplementationVersion() != null
|
|
|
|
? thisPackage.getImplementationVersion()
|
|
|
|
: "version unknown";
|
2022-03-12 15:24:57 +01:00
|
|
|
|
2022-03-12 03:16:00 +01:00
|
|
|
System.out.println("waituntil " + appVersion);
|
|
|
|
System.out.println("""
|
2022-03-12 15:24:57 +01:00
|
|
|
Project Repository: https://gitea.jotoho.de/jotoho/waituntil
|
2021-12-01 14:36:07 +01:00
|
|
|
|
2022-03-12 15:24:57 +01:00
|
|
|
This program is free software: you can redistribute it and/or modify it under the terms of the
|
|
|
|
GNU General Public License as published by the Free Software Foundation, either version 3 of the
|
|
|
|
License, or (at your option) any later version.
|
2022-03-12 03:16:00 +01:00
|
|
|
|
2022-03-12 15:24:57 +01:00
|
|
|
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 General Public License for more details.""");
|
2022-03-12 03:16:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void printHelpInformation() {
|
|
|
|
switch (applicationOutputLanguage) {
|
2022-03-12 15:24:57 +01:00
|
|
|
case GlobalConf.langGerman -> logger.log(Level.ERROR,
|
2022-03-12 15:40:06 +01:00
|
|
|
"Hilfe kommt noch. (Nicht " +
|
2022-03-12 15:24:57 +01:00
|
|
|
"implementiert)");
|
2022-03-12 15:40:06 +01:00
|
|
|
default -> logger.log(Level.ERROR, "Help is yet to come. (Not implemented)");
|
2021-12-01 14:36:07 +01:00
|
|
|
}
|
2022-03-12 03:16:00 +01:00
|
|
|
}
|
2021-12-01 14:36:07 +01:00
|
|
|
|
2022-03-12 15:40:06 +01:00
|
|
|
private static CommandLine parseArgs(final Options options, final String[] args)
|
2022-03-12 15:24:57 +01:00
|
|
|
throws ParseException {
|
2022-03-12 15:40:06 +01:00
|
|
|
return DefaultParser.builder()
|
|
|
|
.setStripLeadingAndTrailingQuotes(true)
|
|
|
|
.build()
|
|
|
|
.parse(options, args);
|
2022-03-12 15:24:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized static void main(final String[] args) {
|
|
|
|
// Retrieve defined CLI options
|
2022-03-12 14:20:18 +01:00
|
|
|
final var appOptions = new AppOptions();
|
2022-01-02 18:33:17 +01:00
|
|
|
|
2022-03-12 15:24:57 +01:00
|
|
|
try {
|
|
|
|
// Parse CLI
|
|
|
|
final var parsedArguments = parseArgs(appOptions.options, args);
|
2022-03-12 03:16:00 +01:00
|
|
|
final var userData = parsedArguments.getArgs();
|
2022-01-02 18:33:17 +01:00
|
|
|
|
2022-03-12 15:24:57 +01:00
|
|
|
// Differentiate between usage scenarios
|
2022-03-12 14:20:18 +01:00
|
|
|
if (parsedArguments.hasOption(appOptions.help)) {
|
2022-03-12 03:16:00 +01:00
|
|
|
printHelpInformation();
|
2022-03-12 14:20:18 +01:00
|
|
|
} else if (parsedArguments.hasOption(appOptions.version)) {
|
2022-03-12 03:16:00 +01:00
|
|
|
printVersionInformation();
|
|
|
|
} else if (userData.length == 0) {
|
|
|
|
switch (applicationOutputLanguage) {
|
2022-03-12 15:24:57 +01:00
|
|
|
case GlobalConf.langGerman -> logger.log(Level.ERROR,
|
2022-03-12 15:40:06 +01:00
|
|
|
"Es wurde keine Uhrzeit " +
|
2022-03-12 15:24:57 +01:00
|
|
|
"angegeben.");
|
2022-03-12 15:40:06 +01:00
|
|
|
default -> logger.log(Level.ERROR, "No target time was provided.");
|
2022-03-12 03:16:00 +01:00
|
|
|
}
|
|
|
|
System.exit(1);
|
|
|
|
} else if (userData.length > 1) {
|
|
|
|
switch (applicationOutputLanguage) {
|
2022-03-12 15:24:57 +01:00
|
|
|
case GlobalConf.langGerman -> logger.log(Level.ERROR,
|
2022-03-12 15:40:06 +01:00
|
|
|
"Zu viele Argumente " +
|
|
|
|
"wurden angegeben.");
|
|
|
|
default -> logger.log(Level.ERROR, "Too many arguments provided.");
|
2022-03-12 03:16:00 +01:00
|
|
|
}
|
|
|
|
System.exit(1);
|
|
|
|
} else {
|
2022-03-12 15:40:06 +01:00
|
|
|
final var target = TimeCalculator.calculateAndAnnounceTargetTime(userData[0]);
|
2022-03-12 03:16:00 +01:00
|
|
|
Sleep.waitUntilTimeStamp(target);
|
2021-12-01 14:36:07 +01:00
|
|
|
}
|
2022-03-12 03:16:00 +01:00
|
|
|
} catch (final ParseException e) {
|
2022-03-12 15:40:06 +01:00
|
|
|
System.getLogger("main")
|
|
|
|
.log(Level.ERROR,
|
|
|
|
"Parsing of arguments failed and the program cannot continue.",
|
|
|
|
e);
|
2021-12-01 14:36:07 +01:00
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|