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.OperationResult;
026import org.syncany.operations.daemon.messages.StatusStartSyncExternalEvent;
027import org.syncany.operations.status.StatusOperation;
028import org.syncany.operations.status.StatusOperationOptions;
029import org.syncany.operations.status.StatusOperationResult;
030
031import com.google.common.eventbus.Subscribe;
032
033public class StatusCommand extends Command {
034        @Override
035        public CommandScope getRequiredCommandScope() { 
036                return CommandScope.INITIALIZED_LOCALDIR;
037        }
038
039        @Override
040        public boolean canExecuteInDaemonScope() {
041                return true;
042        }
043        
044        @Override
045        public int execute(String[] operationArgs) throws Exception {
046                StatusOperationOptions operationOptions = parseOptions(operationArgs);
047                StatusOperationResult operationResult = new StatusOperation(config, operationOptions).execute();
048                
049                printResults(operationResult);
050                
051                return 0;
052        }
053
054        @Override
055        public StatusOperationOptions parseOptions(String[] operationArgs) throws Exception {
056                StatusOperationOptions operationOptions = new StatusOperationOptions();
057
058                OptionParser parser = new OptionParser();       
059                parser.allowsUnrecognizedOptions();
060                
061                OptionSpec<Void> optionForceChecksum = parser.acceptsAll(asList("f", "force-checksum"));
062                OptionSpec<Void> optionNoDeleteUpload = parser.acceptsAll(asList("D", "no-delete"));
063                
064                OptionSet options = parser.parse(operationArgs);        
065                
066                // --force-checksum
067                operationOptions.setForceChecksum(options.has(optionForceChecksum));
068                
069                // -D, --no-delete
070                operationOptions.setDelete(!options.has(optionNoDeleteUpload));
071                
072                return operationOptions;
073        }       
074
075        @Override
076        public void printResults(OperationResult operationResult) {
077                StatusOperationResult concreteOperationResult = (StatusOperationResult) operationResult;
078                
079                if (concreteOperationResult.getChangeSet().hasChanges()) {
080                        for (String newFile : concreteOperationResult.getChangeSet().getNewFiles()) {
081                                out.println("? "+newFile);
082                        }
083
084                        for (String changedFile : concreteOperationResult.getChangeSet().getChangedFiles()) {
085                                out.println("M "+changedFile);
086                        }
087                        
088                        for (String deletedFile : concreteOperationResult.getChangeSet().getDeletedFiles()) {
089                                out.println("D "+deletedFile);
090                        }                                               
091                }
092                else {
093                        out.println("No local changes.");
094                }
095        }
096        
097        @Subscribe
098        public void onStatusStartEventReceived(StatusStartSyncExternalEvent syncEvent) {
099                out.printr("Checking for new or altered files ...");
100        }
101}