mirror of https://github.com/portainer/portainer
81 lines
2.8 KiB
JavaScript
81 lines
2.8 KiB
JavaScript
function isJSONArray(jsonString) {
|
|
return Object.prototype.toString.call(jsonString) === '[object Array]';
|
|
}
|
|
|
|
function isJSON(jsonString) {
|
|
try {
|
|
var o = JSON.parse(jsonString);
|
|
if (o && typeof o === 'object') {
|
|
return o;
|
|
}
|
|
}
|
|
catch (e) { }
|
|
return false;
|
|
}
|
|
|
|
// The Docker API often returns a list of JSON object.
|
|
// This handler wrap the JSON objects in an array.
|
|
// Used by the API in: Image push, Image create, Events query.
|
|
function jsonObjectsToArrayHandler(data) {
|
|
var str = '[' + data.replace(/\n/g, ' ').replace(/\}\s*\{/g, '}, {') + ']';
|
|
return angular.fromJson(str);
|
|
}
|
|
|
|
// The Docker API often returns an empty string or a valid JSON object on success (Docker 1.9 -> Docker 1.12).
|
|
// On error, it returns either an error message as a string (Docker < 1.12) or a JSON object with the field message
|
|
// container the error (Docker = 1.12)
|
|
// This handler ensure a valid JSON object is returned in any case.
|
|
// Used by the API in: container deletion, network deletion, network creation, volume creation,
|
|
// container exec, exec resize.
|
|
function genericHandler(data) {
|
|
var response = {};
|
|
// No data is returned when deletion is successful (Docker 1.9 -> 1.12)
|
|
if (!data) {
|
|
return response;
|
|
}
|
|
// A string is returned on failure (Docker < 1.12)
|
|
else if (!isJSON(data)) {
|
|
response.message = data;
|
|
}
|
|
// Docker 1.12 returns a valid JSON object when an error occurs
|
|
else {
|
|
response = angular.fromJson(data);
|
|
}
|
|
return response;
|
|
}
|
|
|
|
// The Docker API returns the logs as a single string.
|
|
// This handler will return an array with each line being an entry.
|
|
// It will also strip the 8 first characters of each line and remove any ANSI code related character sequences.
|
|
function logsHandler(data) {
|
|
var logs = data;
|
|
logs = logs.substring(8);
|
|
logs = logs.replace(/\n(.{8})/g, '\n\r');
|
|
logs = logs.replace(
|
|
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
|
|
return logs.split('\n');
|
|
}
|
|
|
|
// Image delete API returns an array on success (Docker 1.9 -> Docker 1.12).
|
|
// On error, it returns either an error message as a string (Docker < 1.12) or a JSON object with the field message
|
|
// container the error (Docker = 1.12).
|
|
// This handler returns the original array on success or a newly created array containing
|
|
// only one JSON object with the field message filled with the error message on failure.
|
|
function deleteImageHandler(data) {
|
|
// A string is returned on failure (Docker < 1.12)
|
|
var response = [];
|
|
if (!isJSON(data)) {
|
|
response.push({message: data});
|
|
}
|
|
// A JSON object is returned on failure (Docker = 1.12)
|
|
else if (!isJSONArray(data)) {
|
|
var json = angular.fromJson(data);
|
|
response.push(json);
|
|
}
|
|
// An array is returned on success (Docker 1.9 -> 1.12)
|
|
else {
|
|
response = angular.fromJson(data);
|
|
}
|
|
return response;
|
|
}
|