2016-08-30 23:26:02 +00:00
|
|
|
function isJSON(jsonString) {
|
|
|
|
try {
|
|
|
|
var o = JSON.parse(jsonString);
|
|
|
|
if (o && typeof o === "object") {
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) { }
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-17 01:50:55 +00:00
|
|
|
// The Docker API often returns a list of JSON object.
|
2016-08-10 03:14:10 +00:00
|
|
|
// This handler wrap the JSON objects in an array.
|
2016-08-17 01:50:55 +00:00
|
|
|
// Used by the API in: Image push, Image create, Events query.
|
|
|
|
function jsonObjectsToArrayHandler(data) {
|
2016-08-17 00:27:54 +00:00
|
|
|
var str = "[" + data.replace(/\n/g, " ").replace(/\}\s*\{/g, "}, {") + "]";
|
|
|
|
return angular.fromJson(str);
|
|
|
|
}
|
|
|
|
|
2016-08-19 05:53:27 +00:00
|
|
|
// Image delete API returns an array on success and a string on error.
|
|
|
|
// This handler creates an array composed of a single object with a field 'message'
|
|
|
|
// from a string in case of error.
|
2016-08-10 03:14:10 +00:00
|
|
|
function deleteImageHandler(data) {
|
2016-08-19 05:53:27 +00:00
|
|
|
var response;
|
2016-08-30 23:26:02 +00:00
|
|
|
if (!isJSON(data)) {
|
2016-08-10 03:14:10 +00:00
|
|
|
var arr = [];
|
2016-08-19 05:53:27 +00:00
|
|
|
response = {};
|
|
|
|
response.message = data;
|
2016-08-10 03:14:10 +00:00
|
|
|
arr.push(response);
|
2016-08-30 23:26:02 +00:00
|
|
|
console.log(JSON.stringify(arr, null, 4));
|
2016-08-10 03:14:10 +00:00
|
|
|
return arr;
|
|
|
|
}
|
2016-08-19 05:53:27 +00:00
|
|
|
response = angular.fromJson(data);
|
2016-08-10 03:14:10 +00:00
|
|
|
return response;
|
|
|
|
}
|
2016-09-01 00:20:19 +00:00
|
|
|
|
|
|
|
// Network delete API returns an empty string on success.
|
|
|
|
// 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 an empty object on success or a JSON object with the field message container the error message
|
|
|
|
// on failure.
|
|
|
|
function deleteNetworkHandler(data) {
|
|
|
|
console.log(JSON.stringify(data, null, 4));
|
|
|
|
var response = {};
|
|
|
|
// No data is returned when deletion is successful (Docker 1.9 -> 1.12)
|
|
|
|
if (!data) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
// A string is returned when an error occurs (Docker < 1.12)
|
|
|
|
else if (data && !isJSON(data)) {
|
|
|
|
response.message = data;
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
// Docker 1.12 returns a valid JSON object when an error occurs
|
|
|
|
else {
|
|
|
|
response = angular.fromJson(data);
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|