From 0908e9afb29e76ccdef3cffdbd6e1575e31915d9 Mon Sep 17 00:00:00 2001 From: vince Date: Wed, 21 Feb 2018 16:38:00 +0100 Subject: [PATCH] feature: make "pull", "backward" and "forward" works with id and pid --- lib/API/Version.js | 36 ++++++++++++++++++++---------------- lib/Client.js | 22 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/lib/API/Version.js b/lib/API/Version.js index 8f5bb93ba..9a20bb209 100644 --- a/lib/API/Version.js +++ b/lib/API/Version.js @@ -21,11 +21,11 @@ module.exports = function(CLI) { printOut(cst.PREFIX_MSG + 'Updating repository for process name %s', process_name); - that.Client.getProcessByName(process_name, function(err, processes) { + that.Client.getProcessByNameOrId(process_name, function (err, processes) { - if (processes.length === 0) { - printError('No processes with this name: %s', process_name); - return cb ? cb({msg:'Process not found: '+process_name}) : that.exitCli(cst.ERROR_EXIT); + if (err || processes.length === 0) { + printError('No processes with this name or id : %s', process_name); + return cb ? cb({msg: 'Process not found: ' + process_name}) : that.exitCli(cst.ERROR_EXIT); } var proc = processes[0]; @@ -82,11 +82,11 @@ module.exports = function(CLI) { printOut(cst.PREFIX_MSG + 'Updating repository for process name %s', process_name); - that.Client.getProcessByName(process_name, function(err, processes) { + that.Client.getProcessByNameOrId(process_name, function (err, processes) { - if (processes.length === 0) { - printError('No processes with this name: %s', process_name); - return cb ? cb({msg:'Process not found: ' + process_name}) : that.exitCli(cst.ERROR_EXIT); + if (err || processes.length === 0) { + printError('No processes with this name or id : %s', process_name); + return cb ? cb({msg: 'Process not found: ' + process_name}) : that.exitCli(cst.ERROR_EXIT); } var proc = processes[0]; @@ -138,14 +138,16 @@ module.exports = function(CLI) { var that = this; printOut(cst.PREFIX_MSG + 'Downgrading to previous commit repository for process name %s', process_name); - that.Client.getProcessByName(process_name, function(err, processes) { + that.Client.getProcessByNameOrId(process_name, function (err, processes) { - if (processes.length === 0) { - printError('No processes with this name: %s', process_name); - return cb ? cb({msg:'Process not found: '+process_name}) : that.exitCli(cst.ERROR_EXIT); + if (err || processes.length === 0) { + printError('No processes with this name or id : %s', process_name); + return cb ? cb({msg: 'Process not found: ' + process_name}) : that.exitCli(cst.ERROR_EXIT); } var proc = processes[0]; + // in case user searched by id/pid + process_name = proc.name; if (proc.pm2_env.versioning === undefined || proc.pm2_env.versioning === null) @@ -194,14 +196,16 @@ module.exports = function(CLI) { var that = this; printOut(cst.PREFIX_MSG + 'Updating to next commit repository for process name %s', process_name); - that.Client.getProcessByName(process_name, function(err, processes) { + that.Client.getProcessByNameOrId(process_name, function (err, processes) { - if (processes.length === 0) { - printError('No processes with this name: %s', process_name); - return cb ? cb({msg:'Process not found: '+process_name}) : that.exitCli(cst.ERROR_EXIT); + if (err || processes.length === 0) { + printError('No processes with this name or id: %s', process_name); + return cb ? cb({msg: 'Process not found: ' + process_name}) : that.exitCli(cst.ERROR_EXIT); } var proc = processes[0]; + // in case user searched by id/pid + process_name = proc.name; if (proc.pm2_env.versioning) { vizion.next({folder: proc.pm2_env.versioning.repo_path}, function(err, meta) { if (err !== null) diff --git a/lib/Client.js b/lib/Client.js index 74103fd3b..0b7855157 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -717,3 +717,25 @@ Client.prototype.getProcessByName = function(name, cb) { return cb(null, found_proc); }); }; + +Client.prototype.getProcessByNameOrId = function (nameOrId, cb) { + var foundProc = []; + + this.executeRemote('getMonitorData', {}, function (err, list) { + if (err) { + Common.printError('Error retrieving process list: ' + err); + return cb(err); + } + + list.forEach(function (proc) { + if (proc.pm2_env.name === nameOrId || + proc.pm2_env.pm_exec_path === path.resolve(nameOrId) || + proc.pid === parseInt(nameOrId) || + proc.pm2_env.pm_id === parseInt(nameOrId)) { + foundProc.push(proc); + } + }); + + return cb(null, foundProc); + }); +};