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;
021
022import java.text.DateFormat;
023import java.text.SimpleDateFormat;
024import java.util.Collections;
025import java.util.List;
026import java.util.logging.Logger;
027
028import joptsimple.OptionParser;
029import joptsimple.OptionSet;
030import joptsimple.OptionSpec;
031
032import org.syncany.operations.OperationResult;
033import org.syncany.operations.log.LightweightDatabaseVersion;
034import org.syncany.operations.log.LogOperation;
035import org.syncany.operations.log.LogOperationOptions;
036import org.syncany.operations.log.LogOperationResult;
037
038public class LogCommand extends Command {       
039        protected static final Logger logger = Logger.getLogger(LogCommand.class.getSimpleName());
040        
041        private static final String DATE_FORMAT_PATTERN = "yy-MM-dd HH:mm:ss";
042        private static final DateFormat DATE_FORMAT = new SimpleDateFormat(DATE_FORMAT_PATTERN);
043        
044        private boolean excludeEmpty; 
045        
046        @Override
047        public CommandScope getRequiredCommandScope() { 
048                return CommandScope.INITIALIZED_LOCALDIR;
049        }
050
051        @Override
052        public boolean canExecuteInDaemonScope() {
053                return true;
054        }
055
056        @Override
057        public int execute(String[] operationArgs) throws Exception {
058                LogOperationOptions operationOptions = parseOptions(operationArgs);
059                LogOperationResult operationResult = new LogOperation(config, operationOptions).execute();
060
061                printResults(operationResult);
062
063                return 0;
064        }       
065
066        @Override
067        public LogOperationOptions parseOptions(String[] operationArgs) throws Exception {
068                LogOperationOptions operationOptions = new LogOperationOptions();
069
070                OptionParser parser = new OptionParser();
071                parser.allowsUnrecognizedOptions();
072                
073                OptionSpec<Integer> optionMaxDatabaseVersionCountStr = parser.acceptsAll(asList("n", "database-count")).withRequiredArg().ofType(Integer.class);
074                OptionSpec<Integer> optionStartDatabaseVersionIndexStr = parser.acceptsAll(asList("s", "database-start")).withRequiredArg().ofType(Integer.class);
075                OptionSpec<Integer> optionMaxFileHistoryCountStr = parser.acceptsAll(asList("f", "file-count")).withRequiredArg().ofType(Integer.class);
076                OptionSpec<Void> optionExcludeEmpty = parser.acceptsAll(asList("x", "exclude-empty"));
077
078                OptionSet options = parser.parse(operationArgs);
079                
080                // -x, --exclude-empty
081                excludeEmpty = options.has(optionExcludeEmpty);
082                
083                // -n, --database-count=..
084                if (options.has(optionMaxDatabaseVersionCountStr)) {                    
085                        operationOptions.setMaxDatabaseVersionCount(options.valueOf(optionMaxDatabaseVersionCountStr));
086                }
087                
088                // -s, --database-start=..
089                if (options.has(optionStartDatabaseVersionIndexStr)) {                  
090                        operationOptions.setStartDatabaseVersionIndex(options.valueOf(optionStartDatabaseVersionIndexStr));
091                }
092                
093                // -f, --file-count=..
094                if (options.has(optionMaxFileHistoryCountStr)) {                        
095                        operationOptions.setMaxFileHistoryCount(options.valueOf(optionMaxFileHistoryCountStr));
096                }
097                
098                return operationOptions;
099        }
100
101        @Override
102        public void printResults(OperationResult operationResult) {
103                LogOperationResult concreteOperationResult = (LogOperationResult) operationResult;
104                List<LightweightDatabaseVersion> databaseVersions = concreteOperationResult.getDatabaseVersions();
105                
106                Collections.reverse(databaseVersions);
107                
108                for (LightweightDatabaseVersion databaseVersion : databaseVersions) {
109                        boolean hasChanges = databaseVersion.getChangeSet().hasChanges();
110                        boolean printDatabaseVersion = hasChanges || !excludeEmpty;
111                        
112                        if (printDatabaseVersion) {
113                                printDatabaseVersion(databaseVersion);
114                        }
115                }
116        }
117
118        private void printDatabaseVersion(LightweightDatabaseVersion databaseVersion) {
119                String dateStr = DATE_FORMAT.format(databaseVersion.getDate());
120                String clientStr = databaseVersion.getClient();
121                                
122                out.println(String.format("Database version from %s, by client %s", dateStr, clientStr));
123                
124                if (databaseVersion.getChangeSet().hasChanges()) {      
125                        for (String newFile : databaseVersion.getChangeSet().getNewFiles()) {
126                                out.println("  A "+newFile);
127                        }
128
129                        for (String changedFile : databaseVersion.getChangeSet().getChangedFiles()) {
130                                out.println("  M "+changedFile);
131                        }
132                        
133                        for (String deletedFile : databaseVersion.getChangeSet().getDeletedFiles()) {
134                                out.println("  D "+deletedFile);
135                        }       
136                }
137                else {
138                        out.println("  (empty)");
139                }
140                
141                out.println();          
142        }               
143}