2 * jSite - CLI.java - Copyright © 2006–2019 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.main;
21 import java.io.PrintWriter;
22 import java.util.List;
24 import net.pterodactylus.util.io.StreamCopier.ProgressListener;
25 import de.todesbaum.jsite.application.Freenet7Interface;
26 import de.todesbaum.jsite.application.InsertListener;
27 import de.todesbaum.jsite.application.Node;
28 import de.todesbaum.jsite.application.Project;
29 import de.todesbaum.jsite.application.ProjectInserter;
30 import de.todesbaum.jsite.main.JarFileLocator.DefaultJarFileLocator;
33 * Command-line interface for jSite.
35 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
37 public class CLI implements InsertListener {
39 /** Object used for synchronization. */
40 private Object lockObject = new Object();
42 /** Writer for the console. */
43 private PrintWriter outputWriter = new PrintWriter(System.out, true);
45 /** The freenet interface. */
46 private Freenet7Interface freenetInterface;
48 /** The project inserter. */
49 private ProjectInserter projectInserter = new ProjectInserter();
51 /** The list of nodes. */
55 private List<Project> projects;
57 /** Whether the insert has finished. */
58 private boolean finished = false;
60 /** Whether the insert finished successfully. */
61 private boolean success;
64 * Creates a new command-line interface.
67 * The command-line arguments
69 private CLI(String[] args) {
71 if ((args.length == 0) || args[0].equals("-h") || args[0].equals("--help")) {
72 outputWriter.println("\nParameters:\n");
73 outputWriter.println(" --config-file=<configuration file>");
74 outputWriter.println(" --node=<node name>");
75 outputWriter.println(" --project=<project name>");
76 outputWriter.println(" --local-directory=<local directory>");
77 outputWriter.println(" --path=<path>");
78 outputWriter.println(" --edition=<edition>");
79 outputWriter.println("\nA project gets inserted when a new project is loaded on the command line,");
80 outputWriter.println("or when the command line is finished. --local-directory, --path, and --edition");
81 outputWriter.println("override the parameters in the project.");
85 String configFile = System.getProperty("user.home") + "/.jSite/config7";
86 for (String argument : args) {
87 String value = argument.substring(argument.indexOf('=') + 1).trim();
88 if (argument.startsWith("--config-file=")) {
93 ConfigurationLocator configurationLocator = new ConfigurationLocator(new DefaultJarFileLocator(getClass().getClassLoader()));
94 if (configFile != null) {
95 configurationLocator.setCustomLocation(configFile);
97 Configuration configuration = new Configuration(configurationLocator, configurationLocator.findPreferredLocation());
99 projectInserter.addInsertListener(this);
100 projects = configuration.getProjects();
101 Node node = configuration.getSelectedNode();
102 nodes = configuration.getNodes();
104 freenetInterface = new Freenet7Interface();
105 freenetInterface.setNode(node);
107 projectInserter.setFreenetInterface(freenetInterface);
108 projectInserter.setPriority(configuration.getPriority());
110 Project currentProject = null;
111 for (String argument : args) {
112 if (argument.startsWith("--config-file=")) {
113 /* we already parsed this one. */
116 String value = argument.substring(argument.indexOf('=') + 1).trim();
117 if (argument.startsWith("--node=")) {
118 Node newNode = getNode(value);
119 if (newNode == null) {
120 outputWriter.println("Node \"" + value + "\" not found.");
124 freenetInterface.setNode(node);
125 } else if (argument.startsWith("--project=")) {
126 if (currentProject != null) {
127 if (insertProject(currentProject)) {
128 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
130 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
132 currentProject = null;
134 currentProject = getProject(value);
135 if (currentProject == null) {
136 outputWriter.println("Project \"" + value + "\" not found.");
138 } else if (argument.startsWith("--local-directory")) {
139 if (currentProject == null) {
140 outputWriter.println("You can't specifiy --local-directory before --project.");
143 currentProject.setLocalPath(value);
144 } else if (argument.startsWith("--path=")) {
145 if (currentProject == null) {
146 outputWriter.println("You can't specify --path before --project.");
149 currentProject.setPath(value);
150 } else if (argument.startsWith("--edition=")) {
151 if (currentProject == null) {
152 outputWriter.println("You can't specify --edition before --project.");
155 currentProject.setEdition(Integer.parseInt(value));
157 outputWriter.println("Unknown parameter: " + argument);
163 if (currentProject != null) {
164 if (insertProject(currentProject)) {
165 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
168 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
172 configuration.setProjects(projects);
173 configuration.save();
175 System.exit(errorCode);
179 * Returns the project with the given name.
182 * The name of the project
183 * @return The project, or <code>null</code> if no project could be found
185 private Project getProject(String name) {
186 for (Project project : projects) {
187 if (project.getName().equals(name)) {
195 * Returns the node with the given name.
198 * The name of the node
199 * @return The node, or <code>null</code> if no node could be found
201 private Node getNode(String name) {
202 for (Node node : nodes) {
203 if (node.getName().equals(name)) {
211 * Inserts the given project.
213 * @param currentProject
214 * The project to insert
215 * @return <code>true</code> if the insert finished successfully,
216 * <code>false</code> otherwise
218 private boolean insertProject(Project currentProject) {
219 if (!freenetInterface.hasNode()) {
220 outputWriter.println("Node is not running!");
223 projectInserter.setProject(currentProject);
224 projectInserter.start(new ProgressListener() {
227 public void onProgress(long copied, long length) {
228 System.out.print("Uploaded: " + copied + " / " + length + " bytes...\r");
231 synchronized (lockObject) {
235 } catch (InterruptedException e) {
236 /* ignore, we're in a loop. */
244 // INTERFACE InsertListener
251 public void projectInsertStarted(Project project) {
252 outputWriter.println("Starting Insert of project \"" + project.getName() + "\".");
259 public void projectUploadFinished(Project project) {
260 outputWriter.println("Project \"" + project.getName() + "\" has been uploaded, starting insert...");
267 public void projectURIGenerated(Project project, String uri) {
268 outputWriter.println("URI: " + uri);
275 public void projectInsertProgress(Project project, int succeeded, int failed, int fatal, int total, boolean finalized) {
279 outputWriter.println("Progress: " + succeeded + " done, " + failed + " failed, " + fatal + " fatal, " + total + " total" + (finalized ? " (finalized)" : "") + ", " + ((succeeded + failed + fatal) * 100 / total) + "%");
286 public void projectInsertFinished(Project project, boolean success, Throwable cause) {
287 outputWriter.println("Request URI: " + project.getFinalRequestURI(0));
289 this.success = success;
290 synchronized (lockObject) {
300 * Creates a new command-line interface with the given arguments.
303 * The command-line arguments
305 public static void main(String[] args) {