1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-02 20:35:25 +02:00

refactor(docker/containers): migrate commands tab to react [EE-5208] (#10085)

This commit is contained in:
Chaim Lev-Ari 2023-09-04 19:07:29 +01:00 committed by GitHub
parent 46e73ee524
commit f7366d9788
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
42 changed files with 1783 additions and 951 deletions

View file

@ -1,5 +1,4 @@
import _ from 'lodash-es';
import splitargs from 'splitargs/src/splitargs';
const portPattern = /^([1-9]|[1-5]?[0-9]{2,4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/m;
@ -65,18 +64,6 @@ angular.module('portainer.docker').factory('ContainerHelper', [
'use strict';
var helper = {};
helper.commandStringToArray = function (command) {
return splitargs(command);
};
helper.commandArrayToString = function (array) {
return array
.map(function (elem) {
return "'" + elem + "'";
})
.join(' ');
};
helper.configFromContainer = function (container) {
var config = container.Config;
// HostConfig

View file

@ -0,0 +1,9 @@
import { splitargs } from './splitargs';
export function commandStringToArray(command: string) {
return splitargs(command);
}
export function commandArrayToString(array: string[]) {
return array.map((elem) => `'${elem}'`).join(' ');
}

View file

@ -0,0 +1,68 @@
/**
* Created by elgs on 7/2/14.
*/
import { splitargs } from './splitargs';
describe('splitargs Suite', () => {
beforeEach(() => {});
afterEach(() => {});
it('should split double quoted string', () => {
const i = " I said 'I am sorry.', and he said \"it doesn't matter.\" ";
const o = splitargs(i);
expect(7).toBe(o.length);
expect(o[0]).toBe('I');
expect(o[1]).toBe('said');
expect(o[2]).toBe('I am sorry.,');
expect(o[3]).toBe('and');
expect(o[4]).toBe('he');
expect(o[5]).toBe('said');
expect(o[6]).toBe("it doesn't matter.");
});
it('should split pure double quoted string', () => {
const i = 'I said "I am sorry.", and he said "it doesn\'t matter."';
const o = splitargs(i);
expect(o).toHaveLength(7);
expect(o[0]).toBe('I');
expect(o[1]).toBe('said');
expect(o[2]).toBe('I am sorry.,');
expect(o[3]).toBe('and');
expect(o[4]).toBe('he');
expect(o[5]).toBe('said');
expect(o[6]).toBe("it doesn't matter.");
});
it('should split single quoted string', () => {
const i = 'I said "I am sorry.", and he said "it doesn\'t matter."';
const o = splitargs(i);
expect(o).toHaveLength(7);
expect(o[0]).toBe('I');
expect(o[1]).toBe('said');
expect(o[2]).toBe('I am sorry.,');
expect(o[3]).toBe('and');
expect(o[4]).toBe('he');
expect(o[5]).toBe('said');
expect(o[6]).toBe("it doesn't matter.");
});
it('should split pure single quoted string', () => {
const i = "I said 'I am sorry.', and he said \"it doesn't matter.\"";
const o = splitargs(i);
expect(o).toHaveLength(7);
expect(o[0]).toBe('I');
expect(o[1]).toBe('said');
expect(o[2]).toBe('I am sorry.,');
expect(o[3]).toBe('and');
expect(o[4]).toBe('he');
expect(o[5]).toBe('said');
expect(o[6]).toBe("it doesn't matter.");
});
it('should split to 4 empty strings', () => {
const i = ',,,';
const o = splitargs(i, ',', true);
expect(o).toHaveLength(4);
});
});

View file

@ -0,0 +1,114 @@
/**
Splits strings into tokens by given separator except treating quoted part as a single token.
#Usage
```javascript
var splitargs = require('splitargs');
var i1 = "I said 'I am sorry.', and he said \"it doesn't matter.\"";
var o1 = splitargs(i1);
console.log(o1);
[ 'I',
'said',
'I am sorry.,',
'and',
'he',
'said',
'it doesn\'t matter.' ]
var i2 = "I said \"I am sorry.\", and he said \"it doesn't matter.\"";
var o2 = splitargs(i2);
console.log(o2);
[ 'I',
'said',
'I am sorry.,',
'and',
'he',
'said',
'it doesn\'t matter.' ]
var i3 = 'I said "I am sorry.", and he said "it doesn\'t matter."';
var o3 = splitargs(i3);
console.log(o3);
[ 'I',
'said',
'I am sorry.,',
'and',
'he',
'said',
'it doesn\'t matter.' ]
var i4 = 'I said \'I am sorry.\', and he said "it doesn\'t matter."';
var o4 = splitargs(i4);
console.log(o4);
[ 'I',
'said',
'I am sorry.,',
'and',
'he',
'said',
'it doesn\'t matter.' ]
```
*/
export function splitargs(
input: string,
sep?: RegExp | string,
keepQuotes = false
) {
const separator = sep || /\s/g;
let singleQuoteOpen = false;
let doubleQuoteOpen = false;
let tokenBuffer = [];
const ret = [];
const arr = input.split('');
for (let i = 0; i < arr.length; ++i) {
const element = arr[i];
const matches = element.match(separator);
// TODO rewrite without continue
/* eslint-disable no-continue */
if (element === "'" && !doubleQuoteOpen) {
if (keepQuotes) {
tokenBuffer.push(element);
}
singleQuoteOpen = !singleQuoteOpen;
continue;
} else if (element === '"' && !singleQuoteOpen) {
if (keepQuotes) {
tokenBuffer.push(element);
}
doubleQuoteOpen = !doubleQuoteOpen;
continue;
}
/* eslint-enable no-continue */
if (!singleQuoteOpen && !doubleQuoteOpen && matches) {
if (tokenBuffer.length > 0) {
ret.push(tokenBuffer.join(''));
tokenBuffer = [];
} else if (sep) {
ret.push(element);
}
} else {
tokenBuffer.push(element);
}
}
if (tokenBuffer.length > 0) {
ret.push(tokenBuffer.join(''));
} else if (sep) {
ret.push('');
}
return ret;
}