Search in sources :

Example 1 with ConfigTO

use of org.syncany.config.to.ConfigTO in project syncany by syncany.

the class ConnectCommand method parseOptions.

@Override
public ConnectOperationOptions parseOptions(String[] operationArgs) throws Exception {
    ConnectOperationOptions operationOptions = new ConnectOperationOptions();
    OptionParser parser = new OptionParser();
    OptionSpec<String> optionPlugin = parser.acceptsAll(asList("P", "plugin")).withRequiredArg();
    OptionSpec<String> optionPluginOpts = parser.acceptsAll(asList("o", "plugin-option")).withRequiredArg();
    OptionSpec<Void> optionAddDaemon = parser.acceptsAll(asList("n", "add-daemon"));
    OptionSpec<String> optionPassword = parser.acceptsAll(asList("password")).withRequiredArg();
    OptionSet options = parser.parse(operationArgs);
    List<?> nonOptionArgs = options.nonOptionArguments();
    // Set interactivity mode  
    isInteractive = !options.has(optionPlugin) && !options.has(optionPassword);
    // Plugin
    TransferSettings transferSettings = null;
    if (nonOptionArgs.size() == 1) {
        String connectLink = (String) nonOptionArgs.get(0);
        operationOptions.setStrategy(ConnectOptionsStrategy.CONNECTION_LINK);
        operationOptions.setConnectLink(connectLink);
        transferSettings = null;
    } else if (nonOptionArgs.size() == 0) {
        operationOptions.setStrategy(ConnectOptionsStrategy.CONNECTION_TO);
        operationOptions.setConnectLink(null);
        transferSettings = createTransferSettingsFromOptions(options, optionPlugin, optionPluginOpts);
    } else {
        throw new Exception("Invalid syntax.");
    }
    ConfigTO configTO = createConfigTO(transferSettings);
    operationOptions.setLocalDir(localDir);
    operationOptions.setConfigTO(configTO);
    operationOptions.setDaemon(options.has(optionAddDaemon));
    operationOptions.setPassword(validateAndGetPassword(options, optionPassword));
    return operationOptions;
}
Also used : TransferSettings(org.syncany.plugins.transfer.TransferSettings) ConfigTO(org.syncany.config.to.ConfigTO) OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser) ConnectOperationOptions(org.syncany.operations.init.ConnectOperationOptions)

Example 2 with ConfigTO

use of org.syncany.config.to.ConfigTO in project syncany by syncany.

the class TransferSettingsTest method testDeserializeCorrectClass.

@Test
public void testDeserializeCorrectClass() throws Exception {
    Serializer serializer = new Persister();
    InitOperationOptions initOperationOptions = TestConfigUtil.createTestInitOperationOptions("syncanytest");
    // Always LocalTransferSettings
    serializer.write(initOperationOptions.getConfigTO(), tmpFile);
    ConfigTO confRestored = ConfigTO.load(tmpFile);
    assertEquals(LocalTransferSettings.class, confRestored.getTransferSettings().getClass());
    // Tear down
    FileUtils.deleteDirectory(initOperationOptions.getLocalDir());
    FileUtils.deleteDirectory(((LocalTransferSettings) initOperationOptions.getConfigTO().getTransferSettings()).getPath());
}
Also used : InitOperationOptions(org.syncany.operations.init.InitOperationOptions) Persister(org.simpleframework.xml.core.Persister) ConfigTO(org.syncany.config.to.ConfigTO) Serializer(org.simpleframework.xml.Serializer) Test(org.junit.Test)

Example 3 with ConfigTO

use of org.syncany.config.to.ConfigTO in project syncany by syncany.

the class ConnectOperationTest method testConnectOperationFailureNoConnection.

@Test
public void testConnectOperationFailureNoConnection() throws Exception {
    // A.init()
    InitOperationOptions initOperationOptionsA = TestConfigUtil.createTestInitOperationOptions("A");
    InitOperation initOperationA = new InitOperation(initOperationOptionsA, null);
    InitOperationResult initOperationResultA = initOperationA.execute();
    String connectLinkA = initOperationResultA.getGenLinkResult().getShareLink();
    assertNotNull(connectLinkA);
    // B.connect()
    File localDirB = TestFileUtil.createTempDirectoryInSystemTemp(TestConfigUtil.createUniqueName("client-B", initOperationOptionsA));
    File localConnectDirB = new File(localDirB, Config.DIR_APPLICATION);
    File repoDir = ((LocalTransferSettings) initOperationOptionsA.getConfigTO().getTransferSettings()).getPath();
    ConfigTO connectionConfigToB = initOperationOptionsA.getConfigTO();
    // <<< Point to non-existing repo
    ((LocalTransferSettings) connectionConfigToB.getTransferSettings()).setPath(new File("/does/not/exist"));
    connectionConfigToB.setMachineName("client-B" + Math.abs(new Random().nextInt()));
    connectionConfigToB.setMasterKey(null);
    ConnectOperationOptions connectOperationOptionsB = new ConnectOperationOptions();
    connectOperationOptionsB.setStrategy(ConnectOptionsStrategy.CONNECTION_TO);
    connectOperationOptionsB.setConfigTO(connectionConfigToB);
    connectOperationOptionsB.setPassword(initOperationOptionsA.getPassword());
    connectOperationOptionsB.setLocalDir(localDirB);
    ConnectOperation connectOperationB = new ConnectOperation(connectOperationOptionsB, null);
    ConnectOperationResult connectOperationResultB = connectOperationB.execute();
    assertEquals(ConnectResultCode.NOK_TEST_FAILED, connectOperationResultB.getResultCode());
    assertFalse(new File(localConnectDirB, Config.DIR_DATABASE).exists());
    assertFalse(new File(localConnectDirB, Config.DIR_CACHE).exists());
    assertFalse(new File(localConnectDirB, Config.FILE_CONFIG).exists());
    assertFalse(new File(localConnectDirB, Config.DIR_LOG).exists());
    assertFalse(new File(localConnectDirB, Config.FILE_REPO).exists());
    // Tear down
    TestFileUtil.deleteDirectory(repoDir);
    TestFileUtil.deleteDirectory(localConnectDirB);
    TestFileUtil.deleteDirectory(initOperationOptionsA.getLocalDir());
    TestFileUtil.deleteDirectory(connectOperationOptionsB.getLocalDir());
}
Also used : ConnectOperationResult(org.syncany.operations.init.ConnectOperationResult) InitOperationResult(org.syncany.operations.init.InitOperationResult) InitOperation(org.syncany.operations.init.InitOperation) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) Random(java.util.Random) InitOperationOptions(org.syncany.operations.init.InitOperationOptions) ConnectOperation(org.syncany.operations.init.ConnectOperation) ConfigTO(org.syncany.config.to.ConfigTO) File(java.io.File) ConnectOperationOptions(org.syncany.operations.init.ConnectOperationOptions) Test(org.junit.Test)

Example 4 with ConfigTO

use of org.syncany.config.to.ConfigTO in project syncany by syncany.

the class ConfigHelper method loadConfig.

/**
	 * Loads a {@link Config} object from the given local directory.
	 *
	 * <p>If the config file (.syncany/config.xml) does not exist, <tt>null</tt>
	 * is returned. If it does, the method tries to do the following:
	 * <ul>
	 *  <li>Load the .syncany/config.xml file and load the plugin given by the config file</li>
	 *  <li>Read .syncany/repo, decrypt it using the master key (if necessary) and load it</li>
	 *  <li>Instantiate a {@link Config} object with the transfer objects</li>
	 * </ul>
	 *
	 * @return Returns an instantiated {@link Config} object, or <tt>null</tt> if
	 *         the config file does not exist
	 * @throws Throws an exception if the config is invalid
	 */
public static Config loadConfig(File localDir) throws ConfigException {
    if (localDir == null) {
        throw new ConfigException("Argument localDir cannot be null.");
    }
    File appDir = new File(localDir, Config.DIR_APPLICATION);
    if (appDir.exists()) {
        logger.log(Level.INFO, "Loading config from {0} ...", localDir);
        ConfigTO configTO = ConfigHelper.loadConfigTO(localDir);
        RepoTO repoTO = ConfigHelper.loadRepoTO(localDir, configTO);
        String pluginId = (configTO.getTransferSettings() != null) ? configTO.getTransferSettings().getType() : null;
        TransferPlugin plugin = Plugins.get(pluginId, TransferPlugin.class);
        if (plugin == null) {
            logger.log(Level.WARNING, "Not loading config! Plugin with id '{0}' does not exist.", pluginId);
            throw new ConfigException("Plugin with id '" + pluginId + "' does not exist. Try 'sy plugin install " + pluginId + "'.");
        }
        logger.log(Level.INFO, "Initializing Config instance ...");
        return new Config(localDir, configTO, repoTO);
    } else {
        logger.log(Level.INFO, "Not loading config, app dir does not exist: {0}", appDir);
        return null;
    }
}
Also used : TransferPlugin(org.syncany.plugins.transfer.TransferPlugin) ConfigTO(org.syncany.config.to.ConfigTO) RepoTO(org.syncany.config.to.RepoTO) File(java.io.File)

Example 5 with ConfigTO

use of org.syncany.config.to.ConfigTO in project syncany by syncany.

the class TestCliUtil method fixMachineName.

private static void fixMachineName(Map<String, String> client) throws Exception {
    File configFile = new File(client.get("localdir") + "/" + Config.DIR_APPLICATION + "/" + Config.FILE_CONFIG);
    Serializer serializer = new Persister();
    ConfigTO configTO = serializer.read(ConfigTO.class, configFile);
    configTO.setMachineName(client.get("machinename"));
    serializer.write(configTO, configFile);
}
Also used : Persister(org.simpleframework.xml.core.Persister) ConfigTO(org.syncany.config.to.ConfigTO) File(java.io.File) Serializer(org.simpleframework.xml.Serializer)

Aggregations

ConfigTO (org.syncany.config.to.ConfigTO)27 File (java.io.File)21 Test (org.junit.Test)18 RepoTO (org.syncany.config.to.RepoTO)17 Config (org.syncany.config.Config)15 InitOperationOptions (org.syncany.operations.init.InitOperationOptions)7 ConfigException (org.syncany.config.ConfigException)6 LocalTransferSettings (org.syncany.plugins.local.LocalTransferSettings)6 Random (java.util.Random)4 Persister (org.simpleframework.xml.core.Persister)4 ConnectOperationOptions (org.syncany.operations.init.ConnectOperationOptions)4 ArrayList (java.util.ArrayList)3 Serializer (org.simpleframework.xml.Serializer)3 TransformerTO (org.syncany.config.to.RepoTO.TransformerTO)3 SaltedSecretKey (org.syncany.crypto.SaltedSecretKey)3 ConnectOperation (org.syncany.operations.init.ConnectOperation)3 ConnectOperationResult (org.syncany.operations.init.ConnectOperationResult)3 InitOperation (org.syncany.operations.init.InitOperation)3 InitOperationResult (org.syncany.operations.init.InitOperationResult)3 TransferPlugin (org.syncany.plugins.transfer.TransferPlugin)3