Skip to content

Commit 53e91a8

Browse files
authored
fix(container-command): Fix the issue of container command parsing failure. (#7200)
1 parent 519c6bc commit 53e91a8

File tree

4 files changed

+36
-40
lines changed

4 files changed

+36
-40
lines changed

frontend/src/lang/modules/en.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,8 @@ const message = {
678678
containerExample: '80 or 80-88',
679679
exposePort: 'Expose port',
680680
exposeAll: 'Expose all',
681-
cmdHelper: "e.g. 'nginx' '-g' 'daemon off;' OR nginx -g daemon off;",
682-
entrypointHelper: 'e.g. /bin/sh -c',
681+
cmdHelper: 'e.g. nginx -g "daemon off;"',
682+
entrypointHelper: 'e.g. docker-entrypoint.sh',
683683
autoRemove: 'Auto remove',
684684
cpuQuota: 'NacosCPU',
685685
memoryLimit: 'Memory',

frontend/src/lang/modules/tw.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,8 @@ const message = {
652652
containerExample: '80 或者 80-88',
653653
exposePort: '暴露端口',
654654
exposeAll: '暴露所有',
655-
cmdHelper: "例: 'nginx' '-g' 'daemon off;' 或 nginx -g daemon off;",
656-
entrypointHelper: '例: /bin/sh -c',
655+
cmdHelper: '例: nginx -g "daemon off;"',
656+
entrypointHelper: '例: docker-entrypoint.sh',
657657
autoRemove: '容器退出後自動刪除容器',
658658
cpuQuota: 'CPU 限製',
659659
memoryLimit: '內存限製',

frontend/src/lang/modules/zh.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,8 @@ const message = {
654654
containerExample: '80 或者 80-88',
655655
exposePort: '暴露端口',
656656
exposeAll: '暴露所有',
657-
cmdHelper: "例: 'nginx' '-g' 'daemon off;' 或者 nginx -g daemon off;",
658-
entrypointHelper: '例: /bin/sh -c',
657+
cmdHelper: '例: nginx -g "daemon off;"',
658+
entrypointHelper: '例: docker-entrypoint.sh',
659659
autoRemove: '容器退出后自动删除容器',
660660
cpuQuota: 'CPU 限制',
661661
memoryLimit: '内存限制',

frontend/src/views/container/container/operate/index.vue

+30-34
Original file line numberDiff line numberDiff line change
@@ -357,23 +357,26 @@ const acceptParams = (params: DialogProps): void => {
357357
title.value = i18n.global.t('container.' + dialogData.value.title);
358358
if (params.title === 'edit') {
359359
dialogData.value.rowData.memory = Number(dialogData.value.rowData.memory.toFixed(2));
360-
dialogData.value.rowData.cmd = dialogData.value.rowData.cmd || [];
360+
361361
let itemCmd = '';
362362
for (const item of dialogData.value.rowData.cmd) {
363-
itemCmd += `'${item}' `;
363+
if (item.indexOf(' ') !== -1) {
364+
itemCmd += `"${item.replaceAll('"', '\\"')}" `;
365+
} else {
366+
itemCmd += item + ' ';
367+
}
364368
}
365-
dialogData.value.rowData.cmdStr = itemCmd ? itemCmd.substring(0, itemCmd.length - 1) : '';
366-
369+
dialogData.value.rowData.cmdStr = itemCmd.trimEnd();
367370
let itemEntrypoint = '';
368-
if (dialogData.value.rowData?.entrypoint) {
369-
for (const item of dialogData.value.rowData.entrypoint) {
370-
itemEntrypoint += `'${item}' `;
371+
for (const item of dialogData.value.rowData.entrypoint) {
372+
if (item.indexOf(' ') !== -1) {
373+
itemEntrypoint += `"${item.replaceAll('"', '\\"')}" `;
374+
} else {
375+
itemEntrypoint += item + ' ';
371376
}
372377
}
378+
dialogData.value.rowData.entrypointStr = itemEntrypoint.trimEnd();
373379
374-
dialogData.value.rowData.entrypointStr = itemEntrypoint
375-
? itemEntrypoint.substring(0, itemEntrypoint.length - 1)
376-
: '';
377380
dialogData.value.rowData.labels = dialogData.value.rowData.labels || [];
378381
dialogData.value.rowData.env = dialogData.value.rowData.env || [];
379382
dialogData.value.rowData.labelsStr = dialogData.value.rowData.labels.join('\n');
@@ -501,34 +504,16 @@ const submit = async () => {
501504
}
502505
dialogData.value.rowData!.cmd = [];
503506
if (dialogData.value.rowData?.cmdStr) {
504-
if (dialogData.value.rowData?.cmdStr.indexOf(`'`) !== -1) {
505-
let itemCmd = dialogData.value.rowData!.cmdStr.split(`'`);
506-
for (const cmd of itemCmd) {
507-
if (cmd && cmd !== ' ') {
508-
dialogData.value.rowData!.cmd.push(cmd);
509-
}
510-
}
511-
} else {
512-
let itemCmd = dialogData.value.rowData!.cmdStr.split(` `);
513-
for (const cmd of itemCmd) {
514-
dialogData.value.rowData!.cmd.push(cmd);
515-
}
507+
let itemCmd = splitWithQuotes(dialogData.value.rowData?.cmdStr);
508+
for (const item of itemCmd) {
509+
dialogData.value.rowData!.cmd.push(item.replace(/(?<!\\)"/g, '').replaceAll('\\"', '"'));
516510
}
517511
}
518512
dialogData.value.rowData!.entrypoint = [];
519513
if (dialogData.value.rowData?.entrypointStr) {
520-
if (dialogData.value.rowData?.entrypointStr.indexOf(`'`) !== -1) {
521-
let itemEntrypoint = dialogData.value.rowData!.entrypointStr.split(`'`);
522-
for (const entry of itemEntrypoint) {
523-
if (entry && entry !== ' ') {
524-
dialogData.value.rowData!.entrypoint.push(entry);
525-
}
526-
}
527-
} else {
528-
let itemEntrypoint = dialogData.value.rowData!.entrypointStr.split(` `);
529-
for (const entry of itemEntrypoint) {
530-
dialogData.value.rowData!.entrypoint.push(entry);
531-
}
514+
let itemEntrypoint = splitWithQuotes(dialogData.value.rowData?.entrypointStr);
515+
for (const item of itemEntrypoint) {
516+
dialogData.value.rowData!.entrypoint.push(item.replace(/(?<!\\)"/g, '').replaceAll('\\"', '"'));
532517
}
533518
}
534519
if (dialogData.value.rowData!.publishAllPorts) {
@@ -644,6 +629,17 @@ const isFromApp = (rowData: Container.ContainerHelper) => {
644629
}
645630
return false;
646631
};
632+
633+
const splitWithQuotes = (str) => {
634+
str = str.replace(/\\"/g, '<quota>');
635+
const regex = /(?=(?:[^'"]|['"][^'"]*['"])*$)\s+/g;
636+
let parts = str.split(regex).filter(Boolean);
637+
let returnList = [];
638+
for (const item of parts) {
639+
returnList.push(item.replaceAll('<quota>', '\\"'));
640+
}
641+
return returnList;
642+
};
647643
defineExpose({
648644
acceptParams,
649645
});

0 commit comments

Comments
 (0)