mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-20 01:47:35 +00:00
feature(util) isContainStrAtBegin: pStr2 could be array
This commit is contained in:
parent
c5e11681f1
commit
baf5629ddd
2 changed files with 82 additions and 44 deletions
|
|
@ -10,10 +10,12 @@
|
|||
update = main.srvrequire('update'),
|
||||
exec = main.child_process.exec,
|
||||
Util = main.util,
|
||||
path = main.path,
|
||||
mainpackage = main.mainpackage,
|
||||
equalPart = Util.isContainStrAtBegin,
|
||||
CLOUDCMD = mainpackage.name,
|
||||
ClientFuncs = [],
|
||||
ClientDirs = [],
|
||||
OnMessageFuncs = [],
|
||||
INFO_LOG_LEVEL = 2,
|
||||
HOME = process.env.HOME,
|
||||
|
|
@ -127,19 +129,37 @@
|
|||
return function(pCommand) {
|
||||
var lMsg, lWinCommand, lExec_func, firstChar,
|
||||
connName,
|
||||
lError, lRet, lExecSymbols, isContain;
|
||||
lError, lRet, lExecSymbols, isContain,
|
||||
dir, options = {};
|
||||
|
||||
dir = ClientDirs[pConnNum];
|
||||
|
||||
if (!dir)
|
||||
dir = ClientDirs[pConnNum] = HOME;
|
||||
|
||||
connName = '#' + pConnNum + ': ';
|
||||
Util.log(connName + pCommand);
|
||||
|
||||
if (equalPart(pCommand, CLOUDCMD))
|
||||
lRet = onCloudCmd(pCommand);
|
||||
else if (equalPart(pCommand, 'cd '))
|
||||
lRet = onCD(pCommand);
|
||||
lRet = onCloudCmd(pCommand, function(json) {
|
||||
jsonSend(pSocket, json);
|
||||
});
|
||||
else if (equalPart(pCommand, 'cd ')) {
|
||||
lRet = true;
|
||||
|
||||
if (lRet)
|
||||
jsonSend(pSocket, lRet);
|
||||
else {
|
||||
onCD(pCommand, dir, function(json) {
|
||||
var error = json.stderr,
|
||||
stdout = json.stdout;
|
||||
|
||||
console.log(stdout);
|
||||
if (error)
|
||||
jsonSend(pSocket, json);
|
||||
else
|
||||
ClientDirs[pConnNum] = stdout;
|
||||
});
|
||||
}
|
||||
|
||||
if (!lRet) {
|
||||
/* if we on windows and command is build in
|
||||
* change code page to unicode becouse
|
||||
* windows use unicode on non English versions
|
||||
|
|
@ -163,6 +183,8 @@
|
|||
lExecSymbols = ['*', '&', '{', '}', '|', '\'', '"'];
|
||||
isContain = Util.isContainStr(pCommand, lExecSymbols);
|
||||
firstChar = pCommand[0];
|
||||
options.cwd = dir;
|
||||
console.log(options);
|
||||
|
||||
if (firstChar === '#') {
|
||||
pCommand = pCommand.slice(1);
|
||||
|
|
@ -175,9 +197,9 @@
|
|||
|
||||
io.sockets.emit('message', lMsg);
|
||||
} else if (firstChar === ' ' || isContain)
|
||||
exec(pCommand, lExec_func);
|
||||
exec(pCommand, options, lExec_func);
|
||||
else
|
||||
getSpawn(pCommand, function(json) {
|
||||
getSpawn(pCommand, options, function(json) {
|
||||
jsonSend(pSocket, json);
|
||||
});
|
||||
}
|
||||
|
|
@ -212,14 +234,14 @@
|
|||
};
|
||||
}
|
||||
|
||||
function getSpawn(pCommand, callback) {
|
||||
function getSpawn(pCommand, options, callback) {
|
||||
var send, cmd, spawn,
|
||||
args = pCommand.split(' ');
|
||||
|
||||
pCommand = args.shift();
|
||||
|
||||
spawn = main.child_process.spawn;
|
||||
cmd = spawn(pCommand, args);
|
||||
cmd = spawn(pCommand, args, options);
|
||||
send = function(data, isError) {
|
||||
var lExec = {},
|
||||
msg = data.toString();
|
||||
|
|
@ -241,54 +263,60 @@
|
|||
cmd.on('error', Util.retFalse);
|
||||
}
|
||||
|
||||
function onCloudCmd(pCommand) {
|
||||
function onCloudCmd(pCommand, callback) {
|
||||
var lRet;
|
||||
|
||||
pCommand = Util.removeStr(pCommand, CLOUDCMD);
|
||||
|
||||
if (!equalPart(pCommand, ' '))
|
||||
lRet = HELP;
|
||||
if (!equalPart(pCommand, ' ')) {
|
||||
lRet = true;
|
||||
Util.exec(callback, HELP);
|
||||
}
|
||||
else {
|
||||
pCommand = Util.removeStr(pCommand, ' ');
|
||||
|
||||
if (equalPart(pCommand, 'update') && update) {
|
||||
lRet = true;
|
||||
update.get();
|
||||
lRet = {
|
||||
stdout: Util.addNewLine('update: ok')
|
||||
};
|
||||
|
||||
Util.exec(callback, {
|
||||
stdout: Util.addNewLine('update: ok')
|
||||
});
|
||||
}
|
||||
|
||||
if(Util.strCmp(pCommand, 'exit'))
|
||||
if (Util.strCmp(pCommand, 'exit'))
|
||||
process.exit();
|
||||
}
|
||||
|
||||
return lRet;
|
||||
}
|
||||
|
||||
function onCD(pCommand) {
|
||||
var lRet, lDir, lError,
|
||||
lMsg = '';
|
||||
function onCD(pCommand, currDir, callback) {
|
||||
var dir,
|
||||
paramDir = Util.removeStr(pCommand, 'cd ');
|
||||
|
||||
lDir = Util.removeStr(pCommand, 'cd ');
|
||||
if (equalPart(paramDir, ['/', '~', '.']))
|
||||
dir = paramDir;
|
||||
else
|
||||
dir = path.join(currDir, paramDir);
|
||||
|
||||
if (Util.isContainStr(lDir, '~'))
|
||||
lDir = Util.replaceStr(lDir, '~', HOME);
|
||||
console.log(dir, paramDir);
|
||||
|
||||
lError = Util.tryCatchLog(function() {
|
||||
process.chdir(lDir);
|
||||
exec('cd ' + dir + ' && pwd', function (error, stdout, stderr) {
|
||||
var lRet,
|
||||
lMsg = '',
|
||||
lError = error || stderr;
|
||||
|
||||
if (lError) {
|
||||
lError = Util.stringifyJSON(lError);
|
||||
lMsg = lError;
|
||||
}
|
||||
|
||||
Util.exec(callback, {
|
||||
stderr : lMsg,
|
||||
stdout : Util.rmNewLine(stdout)
|
||||
});
|
||||
});
|
||||
|
||||
if (lError) {
|
||||
lError = Util.stringifyJSON(lError);
|
||||
lMsg = lError;
|
||||
}
|
||||
|
||||
lRet = {
|
||||
stderr: lMsg
|
||||
};
|
||||
|
||||
return lRet;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
24
lib/util.js
24
lib/util.js
|
|
@ -404,15 +404,25 @@ Util = exports || {};
|
|||
* @param pStr2
|
||||
*/
|
||||
Util.isContainStrAtBegin = function(pStr1, pStr2) {
|
||||
var lRet;
|
||||
var i, n, length, subStr, lRet;
|
||||
|
||||
if ( Util.isString(pStr1) && Util.isString(pStr2) ) {
|
||||
var lLength = pStr2.length,
|
||||
lSubStr = pStr1.substring(0, lLength);
|
||||
if (Util.isString(pStr1))
|
||||
if (Util.isArray(pStr2)) {
|
||||
n = pStr2.length;
|
||||
|
||||
for(i = 0; i < n; i++) {
|
||||
lRet = Util.isContainStrAtBegin(pStr1, pStr2[i]);
|
||||
|
||||
if (lRet)
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
length = pStr2.length,
|
||||
subStr = pStr1.substring(0, length);
|
||||
|
||||
lRet = subStr === pStr2;
|
||||
}
|
||||
|
||||
lRet = lSubStr === pStr2;
|
||||
}
|
||||
|
||||
return lRet;
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue