001/* 002 * Syncany, www.syncany.org 003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package org.syncany.cli; 019 020import static java.util.Arrays.asList; 021import joptsimple.OptionParser; 022import joptsimple.OptionSet; 023import joptsimple.OptionSpec; 024 025import org.syncany.operations.ChangeSet; 026import org.syncany.operations.OperationResult; 027import org.syncany.operations.daemon.messages.LsRemoteStartSyncExternalEvent; 028import org.syncany.operations.daemon.messages.StatusStartSyncExternalEvent; 029import org.syncany.operations.daemon.messages.UpIndexMidSyncExternalEvent; 030import org.syncany.operations.daemon.messages.UpIndexStartSyncExternalEvent; 031import org.syncany.operations.daemon.messages.UpStartSyncExternalEvent; 032import org.syncany.operations.status.StatusOperationOptions; 033import org.syncany.operations.up.UpOperation; 034import org.syncany.operations.up.UpOperationOptions; 035import org.syncany.operations.up.UpOperationResult; 036import org.syncany.operations.up.UpOperationResult.UpResultCode; 037 038import com.google.common.eventbus.Subscribe; 039 040public class UpCommand extends Command { 041 private int totalFileCount = -1; 042 043 @Override 044 public CommandScope getRequiredCommandScope() { 045 return CommandScope.INITIALIZED_LOCALDIR; 046 } 047 048 @Override 049 public boolean canExecuteInDaemonScope() { 050 return false; 051 } 052 053 @Override 054 public int execute(String[] operationArgs) throws Exception { 055 UpOperationOptions operationOptions = parseOptions(operationArgs); 056 UpOperationResult operationResult = new UpOperation(config, operationOptions).execute(); 057 058 printResults(operationResult); 059 060 return 0; 061 } 062 063 @Override 064 public UpOperationOptions parseOptions(String[] operationArgs) throws Exception { 065 UpOperationOptions operationOptions = new UpOperationOptions(); 066 067 OptionParser parser = new OptionParser(); 068 parser.allowsUnrecognizedOptions(); 069 070 OptionSpec<Void> optionForceUpload = parser.acceptsAll(asList("F", "force-upload")); 071 OptionSpec<Void> optionNoResumeUpload = parser.acceptsAll(asList("R", "no-resume")); 072 073 OptionSet options = parser.parse(operationArgs); 074 075 // status [<args>] 076 operationOptions.setStatusOptions(parseStatusOptions(operationArgs)); 077 078 // -F, --force-upload 079 operationOptions.setForceUploadEnabled(options.has(optionForceUpload)); 080 081 // -R, --no-resume 082 operationOptions.setResume(!options.has(optionNoResumeUpload)); 083 084 return operationOptions; 085 } 086 087 private StatusOperationOptions parseStatusOptions(String[] operationArgs) throws Exception { 088 StatusCommand statusCommand = new StatusCommand(); 089 statusCommand.setOut(out); 090 091 return statusCommand.parseOptions(operationArgs); 092 } 093 094 @Override 095 public void printResults(OperationResult operationResult) { 096 UpOperationResult concreteOperationResult = (UpOperationResult) operationResult; 097 098 if (concreteOperationResult.getResultCode() == UpResultCode.NOK_UNKNOWN_DATABASES) { 099 out.println("Sync up skipped, because there are remote changes."); 100 } 101 else if (concreteOperationResult.getResultCode() == UpResultCode.OK_CHANGES_UPLOADED) { 102 ChangeSet changeSet = concreteOperationResult.getChangeSet(); 103 104 for (String newFile : changeSet.getNewFiles()) { 105 out.println("A " + newFile); 106 } 107 108 for (String changedFile : changeSet.getChangedFiles()) { 109 out.println("M " + changedFile); 110 } 111 112 for (String deletedFile : changeSet.getDeletedFiles()) { 113 out.println("D " + deletedFile); 114 } 115 116 out.println("Sync up finished."); 117 } 118 else { 119 out.println("Sync up skipped, no local changes."); 120 } 121 } 122 123 @Subscribe 124 public void onUpStartEventReceived(UpStartSyncExternalEvent syncEvent) { 125 out.printr("Starting indexing and upload ..."); 126 } 127 128 @Subscribe 129 public void onStatusStartEventReceived(StatusStartSyncExternalEvent syncEvent) { 130 out.printr("Checking for new or altered files ..."); 131 } 132 133 @Subscribe 134 public void onLsRemoteStartEventReceived(LsRemoteStartSyncExternalEvent syncEvent) { 135 out.printr("Checking remote changes ..."); 136 } 137 138 @Subscribe 139 public void onIndexStartEventReceived(UpIndexStartSyncExternalEvent syncEvent) { 140 totalFileCount = syncEvent.getFileCount(); 141 out.printr("Indexing " + totalFileCount + " new or altered file(s)..."); 142 } 143 144 @Subscribe 145 public void onIndexMidEventReceived(UpIndexMidSyncExternalEvent syncEvent) { 146 out.printr("Indexed " + syncEvent.getCurrentIndex() + "/" + syncEvent.getFileCount() + " file(s)..."); 147 } 148}