@Override public int execute(String[] operationArgs) throws Exception { DaemonOperationOptions operationOptions = parseOptions(operationArgs); DaemonOperationResult operationResult = new DaemonOperation(operationOptions).execute(); printResults(operationResult); return 0; }
private DaemonOperationResult executeRun() throws Exception { if (PidFileUtil.isProcessRunning(pidFile)) { throw new ServiceAlreadyStartedException("Syncany daemon already running."); } PidFileUtil.createPidFile(pidFile); initEventBus(); loadOrCreateConfig(); startWebServer(); startWatchServer(); enterControlLoop(); // This blocks until SHUTDOWN is received! return new DaemonOperationResult(DaemonResultCode.OK); }
@Override public DaemonOperationResult execute() throws Exception { logger.log(Level.INFO, "Starting daemon operation with action " + options.getAction() + " ..."); switch (options.getAction()) { case LIST: return executeList(); case ADD: return executeAdd(); case REMOVE: return executeRemove(); case RUN: return executeRun(); default: throw new Exception("Unknown action: " + options.getAction()); } }
@Test public void testExecuteRemove() throws Exception { when(options.getAction()).thenReturn(DaemonAction.REMOVE); List<String> watchRoots = new ArrayList<String>(); watchRoots.add(tempWatchRootAppFolder.getParent()); when(options.getWatchRoots()).thenReturn(watchRoots); DaemonOperationResult res = deamonOp.execute(); assertNotNull(res); assertEquals(DaemonResultCode.OK, res.getResultCode()); assertNotNull(res.getWatchList()); assertTrue(res.getWatchList().isEmpty()); }
private DaemonOperationResult executeAdd() throws Exception { // Check all folders for (String watchRoot : options.getWatchRoots()) { File watchRootFolder = new File(watchRoot); File watchRootAppFolder = new File(watchRootFolder, Config.DIR_APPLICATION); if (!watchRootFolder.isDirectory() || !watchRootAppFolder.isDirectory()) { throw new Exception("Given argument is not an existing folder, or a valid Syncany folder: " + watchRoot); } } // Add them for (String watchRoot : options.getWatchRoots()) { DaemonConfigHelper.addFolder(new File(watchRoot)); } // Determine return code loadOrCreateConfig(); int watchedMatchingFoldersCount = countWatchedMatchingFolders(); if (watchedMatchingFoldersCount == options.getWatchRoots().size()) { return new DaemonOperationResult(DaemonResultCode.OK, daemonConfig.getFolders()); } else if (watchedMatchingFoldersCount > 0) { return new DaemonOperationResult(DaemonResultCode.OK_PARTIAL, daemonConfig.getFolders()); } else { return new DaemonOperationResult(DaemonResultCode.NOK, daemonConfig.getFolders()); } }
@Before public void setUp() throws Exception { options = mock(DaemonOperationOptions.class); deamonOp = new DaemonOperation(options); }
private void reloadOperation() { loadOrCreateConfig(); watchServer.reload(daemonConfig); }
@Test public void testExecuteAdd() throws Exception { when(options.getAction()).thenReturn(DaemonAction.ADD); List<String> watchRoots = new ArrayList<String>(); watchRoots.add(tempWatchRootAppFolder.getParent()); when(options.getWatchRoots()).thenReturn(watchRoots); DaemonOperationResult res = deamonOp.execute(); assertNotNull(res); assertEquals(DaemonResultCode.OK, res.getResultCode()); assertNotNull(res.getWatchList()); assertEquals(1, res.getWatchList().size()); assertEquals(tempWatchRootAppFolder.getParentFile().getAbsolutePath(), res.getWatchList().get(0).getPath()); }
private DaemonOperationResult executeRemove() throws ConfigException { // Sort Collections.sort(options.getWatchRoots(), Ordering.natural().reverse()); // Remove all folders for (String watchRoot : options.getWatchRoots()) { logger.log(Level.INFO, "- Removing folder from daemon config: " + watchRoot + " ..."); DaemonConfigHelper.removeFolder(watchRoot); } // Check if folders were removed loadOrCreateConfig(); int watchedMatchingFoldersCount = countWatchedMatchingFolders(); if (watchedMatchingFoldersCount == options.getWatchRoots().size()) { return new DaemonOperationResult(DaemonResultCode.NOK, daemonConfig.getFolders()); } else if (watchedMatchingFoldersCount > 0) { return new DaemonOperationResult(DaemonResultCode.NOK_PARTIAL, daemonConfig.getFolders()); } else { return new DaemonOperationResult(DaemonResultCode.OK, daemonConfig.getFolders()); } }
private DaemonOperationResult executeList() { logger.log(Level.INFO, "Listing daemon-managed folders ..."); loadOrCreateConfig(); return new DaemonOperationResult(DaemonResultCode.OK, daemonConfig.getFolders()); }
@Test public void testExecuteList() throws Exception { when(options.getAction()).thenReturn(DaemonAction.LIST); DaemonOperationResult res = deamonOp.execute(); assertNotNull(res); assertEquals(DaemonResultCode.OK, res.getResultCode()); assertNotNull(res.getWatchList()); }