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 java.io.File; 021import java.io.FileInputStream; 022import java.io.InputStream; 023import java.util.List; 024import java.util.logging.Level; 025import java.util.logging.Logger; 026 027import joptsimple.OptionParser; 028import joptsimple.OptionSet; 029 030import org.apache.commons.io.IOUtils; 031import org.syncany.operations.OperationOptions; 032import org.syncany.operations.OperationResult; 033 034/** 035 * Intentionally undocumented command to help debugging the application. Implements various 036 * helpers for the repository and the local directory. 037 * 038 * @author Philipp C. Heckel (philipp.heckel@gmail.com) 039 */ 040public class DebugCommand extends Command { 041 private static final Logger logger = Logger.getLogger(DebugCommand.class.getSimpleName()); 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 OptionParser parser = new OptionParser(); 056 OptionSet options = parser.parse(operationArgs); 057 058 // Files 059 List<?> nonOptionArgs = options.nonOptionArguments(); 060 061 if (nonOptionArgs.size() > 0) { 062 String debugCommand = nonOptionArgs.get(0).toString(); 063 List<?> newNonOptionArgs = nonOptionArgs.subList(1, nonOptionArgs.size()); 064 065 if ("decrypt".equals(debugCommand)) { 066 runDebugCommand(newNonOptionArgs); 067 } 068 } 069 070 throw new Exception("Invalid syntax. No command given or command unknown."); 071 } 072 073 private void runDebugCommand(List<?> nonOptionArgs) throws Exception { 074 logger.log(Level.INFO, "Running 'decrypt' command with arguments: "+nonOptionArgs); 075 076 if (nonOptionArgs.size() != 1) { 077 throw new Exception("Invalid syntax for 'debug' command. Argument expected."); 078 } 079 080 if (!isInitializedScope()) { 081 throw new Exception("Command 'debug' can only be run in initialized local dir."); 082 } 083 084 File decryptFile = new File(nonOptionArgs.get(0).toString()); 085 086 if (!decryptFile.exists()) { 087 throw new Exception("Given file does not exist: "+decryptFile); 088 } 089 090 InputStream fileInputStream = config.getTransformer().createInputStream(new FileInputStream(decryptFile)); 091 092 IOUtils.copy(fileInputStream, System.out); 093 System.exit(0); 094 } 095 096 private boolean isInitializedScope() { 097 return config != null; 098 } 099 100 @Override 101 public OperationOptions parseOptions(String[] operationArgs) throws Exception { 102 return null; 103 } 104 105 @Override 106 public void printResults(OperationResult result) { 107 // Nothing. 108 } 109}