示例#1
0
 /**
  * @inheritdoc
  */
 public function run(&$cmdParams, &$params)
 {
     $controller = $this->controller;
     $res = true;
     $connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : '';
     $file = !empty($cmdParams[1]) ? $cmdParams[1] : '';
     if (empty($connectionId) || empty($file)) {
         Log::throwException('sftpRm: Please specify a valid connection id and file');
     }
     /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */
     $connParams = $controller->getConnectionParams($connectionId);
     $controller->stdout(" " . $connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK);
     $controller->stdout(' Removing file ');
     $controller->stdout($file, Console::FG_CYAN);
     if (!$controller->dryRun) {
         // the getConnection method is provided by the SftpConnectReqs Behavior
         /** @noinspection PhpUndefinedMethodInspection */
         /** @var $connection Net_SFTP|resource */
         $connection = $controller->getConnection($connectionId);
         $sftpHelper = new SftpHelper($connectionId, $connection, $connParams);
         if ($sftpHelper->isFile($file)) {
             $res = $sftpHelper->delete($file, false);
             if (!$res) {
                 Log::logger()->addError('sftpRm: error removing file {file}', ['file' => $file]);
             }
         } else {
             $controller->stdout("\n");
             $controller->warn('sftpRm: ' . $file . ' is not a file');
         }
         $sftpHelper->flushCache();
     } else {
         $controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
     $controller->stdout("\n");
     return $res;
 }
示例#2
0
 /**
  * @inheritdoc
  */
 public function run(&$cmdParams, &$params)
 {
     $res = true;
     $taskRunner = $this->taskRunner;
     $connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : '';
     $pathFrom = !empty($cmdParams[1]) ? $taskRunner->parsePath($cmdParams[1]) : '';
     $pathTo = !empty($cmdParams[2]) ? $taskRunner->parsePath($cmdParams[2]) : '';
     $overwrite = !empty($cmdParams[3]) ? $cmdParams[3] : false;
     // TODO: check if overwrite is needed after implementing the FTP support
     if (empty($pathFrom) || empty($pathTo)) {
         throw new Exception('sftpMv: Origin and destination cannot be empty');
     }
     /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */
     $connParams = $this->controller->getConnectionParams($connectionId);
     $this->controller->stdout(" " . $connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK);
     $this->controller->stdout(" Move (overwrite: " . ($overwrite ? 'yes' : 'no') . ") \n " . $pathFrom . " to \t " . $pathTo);
     $insidePathTo = $pathTo . '/' . basename($pathFrom);
     if (!$this->controller->dryRun) {
         /** @noinspection PhpUndefinedMethodInspection */
         /** @var $connection Net_SFTP|resource */
         $connection = $this->controller->getConnection($connectionId);
         $sftpHelper = new SftpHelper($connectionId, $connection, $connParams);
         if (!$sftpHelper->fileExists($pathFrom)) {
             $this->controller->stdout("\n");
             $this->controller->stderr("Not found: {$pathFrom}", Console::FG_RED);
         } else {
             if ($sftpHelper->isFile($pathFrom) && $sftpHelper->isDir($pathTo)) {
                 $pathTo = $insidePathTo;
             }
             if (!$overwrite && $sftpHelper->isDir($pathFrom) && $sftpHelper->isDir($pathTo)) {
                 if (!$sftpHelper->fileExists($insidePathTo)) {
                     // not overwriting; copy the source directory into the destination folder:
                     $res = $sftpHelper->rename($pathFrom, $insidePathTo);
                 } else {
                     $this->controller->stdout("\n");
                     $this->controller->warn("Destination directory {$insidePathTo} already exists; not overwriting");
                 }
             } elseif (!$overwrite && $sftpHelper->isFile($pathFrom) && $sftpHelper->isFile($pathTo)) {
                 $this->controller->stdout("\n");
                 $this->controller->warn("Destination file {$pathTo} already exists; not overwriting");
             } elseif ($sftpHelper->isDir($pathFrom) && $sftpHelper->isFile($pathTo)) {
                 $this->controller->stdout("\n");
                 $this->controller->stderr("Trying to move a directory to a file: {$pathTo}", Console::FG_RED);
             } elseif (!$sftpHelper->fileExists($pathTo) || $overwrite && $sftpHelper->isDir($pathFrom) && $sftpHelper->isDir($pathTo) || $overwrite && $sftpHelper->isFile($pathFrom) && $sftpHelper->isFile($pathTo)) {
                 // if destination exists, overwrite it with the source file/dir
                 // note: if pathTo is a directory, it has to be empty in order to be overwritten
                 try {
                     $res = $sftpHelper->rename($pathFrom, $pathTo);
                 } catch (ErrorException $e) {
                     $this->controller->stdout("\n");
                     $this->controller->warn($e->getMessage());
                 }
                 if (!$res) {
                     // Note: using sftp, renaming a directory to another does not work even if destination is empty
                     $this->controller->stdout("\n");
                     $this->controller->stderr("Failed moving {$pathFrom} to {$pathTo}", Console::FG_RED);
                 }
             }
         }
         $sftpHelper->flushCache();
     } else {
         $this->controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
     $this->controller->stdout("\n");
     return $res;
 }