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.init.GenlinkOperation;
027import org.syncany.operations.init.GenlinkOperationOptions;
028import org.syncany.operations.init.GenlinkOperationResult;
029
030public class GenlinkCommand extends AbstractInitCommand {
031        private boolean machineReadableOutput;
032        
033        @Override
034        public CommandScope getRequiredCommandScope() { 
035                return CommandScope.INITIALIZED_LOCALDIR;
036        }
037        
038        @Override
039        public boolean canExecuteInDaemonScope() {
040                return true;
041        }
042        
043        @Override
044        public int execute(String[] operationArgs) throws Exception {
045                GenlinkOperationOptions operationOptions = parseOptions(operationArgs);         
046                GenlinkOperationResult operationResult = new GenlinkOperation(config, operationOptions).execute();              
047                
048                printResults(operationResult);
049                
050                return 0;               
051        }
052        
053        @Override
054        public GenlinkOperationOptions parseOptions(String[] operationArgs) {
055                GenlinkOperationOptions operationOptions = new GenlinkOperationOptions();
056                
057                OptionParser parser = new OptionParser();
058                OptionSpec<Void> optionMachineReadable = parser.acceptsAll(asList("m", "machine-readable"));            
059                OptionSpec<Void> optionShort = parser.acceptsAll(asList("s", "short"));         
060
061                parser.allowsUnrecognizedOptions();             
062                OptionSet options = parser.parse(operationArgs);
063
064                // --machine-readable, -m
065                machineReadableOutput = options.has(optionMachineReadable);
066                
067                // --short, -s
068                operationOptions.setShortUrl(options.has(optionShort));
069
070                return operationOptions;
071        }
072        
073        @Override
074        public void printResults(OperationResult operationResult) {
075                GenlinkOperationResult concreteOperationResult = (GenlinkOperationResult) operationResult;
076                
077                if (!machineReadableOutput) {
078                        out.println();
079                        out.println("To share the same repository with others, you can share this link:");
080                }
081                
082                printLink(concreteOperationResult, machineReadableOutput);                      
083        }       
084}