Add options for configuration file location.
[jSite.git] / src / de / todesbaum / jsite / main / CLI.java
index c401aaa..0780e9f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * jSite - 
+ * jSite -
  * Copyright (C) 2006 David Roden
  *
  * This program is free software; you can redistribute it and/or modify
@@ -21,32 +21,55 @@ package de.todesbaum.jsite.main;
 
 import java.io.PrintWriter;
 
-import de.todesbaum.jsite.application.EditionProject;
 import de.todesbaum.jsite.application.Freenet7Interface;
 import de.todesbaum.jsite.application.InsertListener;
 import de.todesbaum.jsite.application.Node;
 import de.todesbaum.jsite.application.Project;
 import de.todesbaum.jsite.application.ProjectInserter;
+import de.todesbaum.util.io.StreamCopier.ProgressListener;
 
 /**
- * @author David Roden <droden@gmail.com>
- * @version $Id: CLI.java 418 2006-03-29 17:49:16Z bombe $
+ * Command-line interface for jSite.
+ *
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  */
 public class CLI implements InsertListener {
 
+       /** Object used for synchronization. */
        private Object lockObject = new Object();
+
+       /** Writer for the console. */
        private PrintWriter outputWriter = new PrintWriter(System.out, true);
+
+       /** The freenet interface. */
        private Freenet7Interface freenetInterface;
+
+       /** The project inserter. */
        private ProjectInserter projectInserter = new ProjectInserter();
+
+       /** The list of nodes. */
        private Node[] nodes;
+
+       /** The projects. */
        private Project[] projects;
+
+       /** Whether the insert has finished. */
        private boolean finished = false;
+
+       /** Whether the insert finished successfully. */
        private boolean success;
 
+       /**
+        * Creates a new command-line interface.
+        *
+        * @param args
+        *            The command-line arguments
+        */
        private CLI(String[] args) {
 
                if ((args.length == 0) || args[0].equals("-h") || args[0].equals("--help")) {
                        outputWriter.println("\nParameters:\n");
+                       outputWriter.println("  --config-file=<configuration file>");
                        outputWriter.println("  --node=<node name>");
                        outputWriter.println("  --project=<project name>");
                        outputWriter.println("  --local-directory=<local directory>");
@@ -58,12 +81,20 @@ public class CLI implements InsertListener {
                        return;
                }
 
-               Configuration configuration = new Configuration();
-               if (!configuration.createLockFile()) {
-                       outputWriter.println("Lock file found!");
-                       return;
+               String configFile = System.getProperty("user.home") + "/.jSite/config7";
+               for (String argument : args) {
+                       String value = argument.substring(argument.indexOf('=') + 1).trim();
+                       if (argument.startsWith("--config-file=")) {
+                               configFile = value;
+                       }
                }
 
+               ConfigurationLocator configurationLocator = new ConfigurationLocator();
+               if (configFile != null) {
+                       configurationLocator.setCustomLocation(configFile);
+               }
+               Configuration configuration = new Configuration(configurationLocator, configurationLocator.findPreferredLocation());
+
                projectInserter.addInsertListener(this);
                projects = configuration.getProjects();
                Node node = configuration.getSelectedNode();
@@ -75,8 +106,11 @@ public class CLI implements InsertListener {
                projectInserter.setFreenetInterface(freenetInterface);
 
                Project currentProject = null;
-               for (int argumentIndex = 0, argumentSize = args.length; argumentIndex < argumentSize; argumentIndex++) {
-                       String argument = args[argumentIndex];
+               for (String argument : args) {
+                       if (argument.startsWith("--config-file=")) {
+                               /* we already parsed this one. */
+                               continue;
+                       }
                        String value = argument.substring(argument.indexOf('=') + 1).trim();
                        if (argument.startsWith("--node=")) {
                                Node newNode = getNode(value);
@@ -116,21 +150,18 @@ public class CLI implements InsertListener {
                                        outputWriter.println("You can't specify --edition before --project.");
                                        return;
                                }
-                               if (currentProject instanceof EditionProject) {
-                                       ((EditionProject) currentProject).setEdition(Integer.parseInt(value));
-                               } else {
-                                       outputWriter.println("Project \"" + currentProject.getName() + "\" is not an edition-based project.");
-                                       return;
-                               }
+                               currentProject.setEdition(Integer.parseInt(value));
                        } else {
                                outputWriter.println("Unknown parameter: " + argument);
                                return;
                        }
                }
 
+               int errorCode = 1;
                if (currentProject != null) {
                        if (insertProject(currentProject)) {
                                outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
+                               errorCode = 0;
                        } else {
                                outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
                        }
@@ -138,10 +169,19 @@ public class CLI implements InsertListener {
 
                configuration.setProjects(projects);
                configuration.save();
+
+               System.exit(errorCode);
        }
 
+       /**
+        * Returns the project with the given name.
+        *
+        * @param name
+        *            The name of the project
+        * @return The project, or <code>null</code> if no project could be found
+        */
        private Project getProject(String name) {
-               for (Project project: projects) {
+               for (Project project : projects) {
                        if (project.getName().equals(name)) {
                                return project;
                        }
@@ -149,8 +189,15 @@ public class CLI implements InsertListener {
                return null;
        }
 
+       /**
+        * Returns the node with the given name.
+        *
+        * @param name
+        *            The name of the node
+        * @return The node, or <code>null</code> if no node could be found
+        */
        private Node getNode(String name) {
-               for (Node node: nodes) {
+               for (Node node : nodes) {
                        if (node.getName().equals(name)) {
                                return node;
                        }
@@ -158,18 +205,32 @@ public class CLI implements InsertListener {
                return null;
        }
 
+       /**
+        * Inserts the given project.
+        *
+        * @param currentProject
+        *            The project to insert
+        * @return <code>true</code> if the insert finished successfully,
+        *         <code>false</code> otherwise
+        */
        private boolean insertProject(Project currentProject) {
                if (!freenetInterface.hasNode()) {
                        outputWriter.println("Node is not running!");
                        return false;
                }
                projectInserter.setProject(currentProject);
-               projectInserter.start();
+               projectInserter.start(new ProgressListener() {
+
+                       public void onProgress(long copied, long length) {
+                               System.out.print("Uploaded: " + copied + " / " + length + " bytes...\r");
+                       }
+               });
                synchronized (lockObject) {
                        while (!finished) {
                                try {
                                        lockObject.wait();
                                } catch (InterruptedException e) {
+                                       /* ignore, we're in a loop. */
                                }
                        }
                }
@@ -190,6 +251,20 @@ public class CLI implements InsertListener {
        /**
         * {@inheritDoc}
         */
+       public void projectUploadFinished(Project project) {
+               outputWriter.println("Project \"" + project.getName() + "\" has been uploaded, starting insert...");
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void projectURIGenerated(Project project, String uri) {
+               outputWriter.println("URI: " + uri);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
        public void projectInsertProgress(Project project, int succeeded, int failed, int fatal, int total, boolean finalized) {
                outputWriter.println("Progress: " + succeeded + " done, " + failed + " failed, " + fatal + " fatal, " + total + " total" + (finalized ? " (finalized)" : "") + ", " + ((succeeded + failed + fatal) * 100 / total) + "%");
        }
@@ -198,13 +273,8 @@ public class CLI implements InsertListener {
         * {@inheritDoc}
         */
        public void projectInsertFinished(Project project, boolean success, Throwable cause) {
-               outputWriter.println("Request URI: " + project.getFinalURI(0));
+               outputWriter.println("Request URI: " + project.getFinalRequestURI(0));
                finished = true;
-               if (success) {
-                       if (project instanceof EditionProject) {
-                               ((EditionProject) project).setEdition(((EditionProject) project).getEdition() + 1);
-                       }
-               }
                this.success = success;
                synchronized (lockObject) {
                        lockObject.notify();
@@ -215,6 +285,12 @@ public class CLI implements InsertListener {
        // MAIN
        //
 
+       /**
+        * Creates a new command-line interface with the given arguments.
+        *
+        * @param args
+        *            The command-line arguments
+        */
        public static void main(String[] args) {
                new CLI(args);
        }