diff --git a/src/main/java/airfield/App.java b/src/main/java/airfield/App.java index f94620e..b737ac6 100644 --- a/src/main/java/airfield/App.java +++ b/src/main/java/airfield/App.java @@ -9,17 +9,25 @@ public class App { public final static void main(String args[]) { - if (args.length != 2) { + if (args.length < 2) { usage(); return; } String local = args[0]; String remote = args[1]; - TakeDown installer = new TakeDown(local, remote); + String user = null; + String password = null; + if (args.length == 4) { + user = args[2]; + password = args[3]; + } + + TakeDown installer; + installer = new TakeDown(local, remote, user, password); installer.installOrUpdate(); } static void usage() { - System.out.println("Use: java -jar airfield.App [PATH_TO_LOCAL_APP] [PATH_TO_REMOTE_GIT_REPO]"); + System.out.println("Use: java -jar airfield.App PATH_TO_LOCAL_APP PATH_TO_REMOTE_GIT_REPO [userName] [ password]"); } } diff --git a/src/main/java/com/airhacks/airfield/TakeDown.java b/src/main/java/com/airhacks/airfield/TakeDown.java index 4393408..4bbf170 100644 --- a/src/main/java/com/airhacks/airfield/TakeDown.java +++ b/src/main/java/com/airhacks/airfield/TakeDown.java @@ -6,11 +6,14 @@ import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; +import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.PullCommand; import org.eclipse.jgit.api.PullResult; import org.eclipse.jgit.api.ResetCommand; import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.transport.CredentialsProvider; +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; /** * @@ -21,18 +24,27 @@ public class TakeDown { private final String remotePath; private final String localPath; private Git git; + CredentialsProvider cp = null; - public TakeDown(String localPath, String remotePath) { + public TakeDown(String localPath, String remotePath, String userName, String password) { this.remotePath = remotePath; this.localPath = localPath; + if (userName != null && password != null) { + cp = new UsernamePasswordCredentialsProvider(userName, password); + } } void initialDownload() { + try { - this.git = Git.cloneRepository() + CloneCommand clone = Git.cloneRepository() .setURI(remotePath) - .setDirectory(new File(localPath)) - .call(); + .setDirectory(new File(localPath)); + if (cp != null) { + clone = clone.setCredentialsProvider(cp); + } + this.git = clone.call(); + System.out.println("+App installed into: " + this.localPath); } catch (GitAPIException ex) { System.err.println("--Cannot download files: " + ex.getMessage()); @@ -49,6 +61,9 @@ void update() { throw new IllegalStateException("Cannot reset local repository", ex); } PullCommand command = this.git.pull(); + if (cp != null) { + command.setCredentialsProvider(cp); + } try { PullResult pullResult = command.call(); if (pullResult.isSuccessful()) { diff --git a/src/test/java/com/airhacks/airfield/TakeDownIT.java b/src/test/java/com/airhacks/airfield/TakeDownIT.java index 1150c17..609bb8e 100644 --- a/src/test/java/com/airhacks/airfield/TakeDownIT.java +++ b/src/test/java/com/airhacks/airfield/TakeDownIT.java @@ -40,7 +40,7 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx }); } - this.cut = new TakeDown(LOCAL_REPO, "git://localhost:4242/"); + this.cut = new TakeDown(LOCAL_REPO, "git://localhost:4242/", null, null); } @Test