From 08e309790867988a7760a559ab515b9b1d171adb Mon Sep 17 00:00:00 2001 From: Matt Soghoian Date: Mon, 27 Aug 2018 21:06:25 -0700 Subject: [PATCH 1/2] Add support for docker pull, update tests and readme. --- README.md | 20 ++++++++++++++++++++ src/index.spec.ts | 33 +++++++++++++++++++++++++++++++++ src/index.ts | 8 ++++++++ 3 files changed, 61 insertions(+) diff --git a/README.md b/README.md index 2305cfe..fd9b899 100644 --- a/README.md +++ b/README.md @@ -600,6 +600,26 @@ docker.command('login -u myusername -p mypassword').then(function (data) { // Error response from daemon: Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password ``` +* docker pull + +```js +docker.command('pull nginx:latest').then(function (data) { + console.log('data = ', data); + // Successfully pulled image + }, function (rejected) { + console.log('rejected = ', rejected); + // Failed to pull image + }); + +// data = { command: 'docker pull nginx:1.15.2 ', +// raw:'1.15.2: Pulling from library/nginx\nDigest: sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424\nStatus: Image is up to date for nginx:1.15.2\n', +// login: '1.15.2: Pulling from library/nginx\nDigest: sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424\nStatus: Image is up to date for nginx:1.15.2' } + +// rejected = error: 'Error: Command failed: docker pull nginx:999.999.999 +// Error response from daemon: manifest for nginx:999.999.999 not found +// ' stdout = '' stderr = 'Error response from daemon: manifest for nginx:999.999.999 not found +``` + ## License diff --git a/src/index.spec.ts b/src/index.spec.ts index 627bd0f..ed82d75 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -132,4 +132,37 @@ test("docker-cli-js", (t) => { }); }); + t.test("pull latest", (t) => { + + const docker = new Docker(); + + return docker.command("pull nginx").then(function(data) { + console.log("data = ", data); + t.ok(data.login); + }); + }); + + t.test("pull specific tag", (t) => { + + const docker = new Docker(); + + return docker.command("pull nginx:1.15.2").then(function(data) { + console.log("data = ", data); + t.ok(data.login); + }); + }); + + t.test("pull intentionally failed, invalid image", (t) => { + + const docker = new Docker(); + + return docker.command("pull nginx:999.999.999").then(function(data) { + console.log("data = ", data); + t.notOk(data.login); + }, function(rejected) { + console.log("rejected = ", rejected); + t.ok(/error/.test(rejected)); + }); + }); + }); diff --git a/src/index.ts b/src/index.ts index ab95573..b59eed5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -130,6 +130,14 @@ const extractResult = function(result: any) { run(resultp: any) { resultp.login = resultp.raw.trim(); + return resultp; + }, + }, + { + re: / pull /, + run(resultp: any) { + resultp.login = resultp.raw.trim(); + return resultp; }, }, From 18d38b2c5df9206cd5b5a305e97e7de6be6da9b1 Mon Sep 17 00:00:00 2001 From: Matt Soghoian Date: Mon, 27 Aug 2018 21:33:48 -0700 Subject: [PATCH 2/2] Add support for docker push, update tests and readme. --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ src/index.spec.ts | 25 +++++++++++++++++++++++++ src/index.ts | 8 ++++++++ 3 files changed, 72 insertions(+) diff --git a/README.md b/README.md index fd9b899..2988c42 100644 --- a/README.md +++ b/README.md @@ -620,6 +620,45 @@ docker.command('pull nginx:latest').then(function (data) { // ' stdout = '' stderr = 'Error response from daemon: manifest for nginx:999.999.999 not found ``` +* docker push + +```js +docker.command('push nginx:latest').then(function (data) { + console.log('data = ', data); + // Successfully pulled image + }, function (rejected) { + console.log('rejected = ', rejected); + // Failed to pull image + }); + +// data = { command: 'docker push mattsoghoian/test ', +// raw: +// 'The push refers to repository [docker.io//]\n08d25fa0442e: Preparing\na8c4aeeaa045: Preparing\ncdb3f9544e4c: Preparing\n08d25fa0442e: Mounted from library/nginx\na8c4aeeaa045: Mounted from library/nginx\ncdb3f9544e4c: Mounted from library/nginx\nlatest: digest: sha256:4ffd9758ea9ea360fd87d0cee7a2d1cf9dba630bb57ca36b3108dcd3708dc189 size: 948\n', +// login: +// 'The push refers to repository [docker.io//]\n08d25fa0442e: Preparing\na8c4aeeaa045: Preparing\ncdb3f9544e4c: Preparing\n08d25fa0442e: Mounted from library/nginx\na8c4aeeaa045: Mounted from library/nginx\ncdb3f9544e4c: Mounted from library/nginx\nlatest: digest: sha256:4ffd9758ea9ea360fd87d0cee7a2d1cf9dba630bb57ca36b3108dcd3708dc189 size: 948' } + +// rejected = error: 'Error: Command failed: docker push nginx +// An image does not exist locally with the tag: nginx +// ' stdout = 'The push refers to repository [docker.io/library/nginx] +// ' stderr = 'An image does not exist locally with the tag: nginx + +// rejected = error: 'Error: Command failed: docker push nginx +// errors: +// denied: requested access to the resource is denied +// unauthorized: authentication required +// ' stdout = 'The push refers to repository [docker.io/library/nginx] +// 08d25fa0442e: Preparing +// a8c4aeeaa045: Preparing +// cdb3f9544e4c: Preparing +// cdb3f9544e4c: Layer already exists +// 08d25fa0442e: Layer already exists +// a8c4aeeaa045: Layer already exists +// ' stderr = 'errors: +// denied: requested access to the resource is denied +// unauthorized: authentication required +// ' +``` + ## License diff --git a/src/index.spec.ts b/src/index.spec.ts index ed82d75..007b810 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -165,4 +165,29 @@ test("docker-cli-js", (t) => { }); }); + t.test("push intentionally failed, denied repo access", (t) => { + + const docker = new Docker(); + + return docker.command("push nginx").then(function(data) { + console.log("data = ", data); + t.ok(data.login); + }, function(rejected) { + console.log("rejected = ", rejected); + t.ok(/error/.test(rejected)); + }); + }); + + t.test("push intentionally failed, local image does not exist", (t) => { + + const docker = new Docker(); + + return docker.command("push dmarionertfulthestoncoag").then(function(data) { + console.log("data = ", data); + t.ok(data.login); + }, function(rejected) { + console.log("rejected = ", rejected); + t.ok(/error/.test(rejected)); + }); + }); }); diff --git a/src/index.ts b/src/index.ts index b59eed5..9ab899d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -138,6 +138,14 @@ const extractResult = function(result: any) { run(resultp: any) { resultp.login = resultp.raw.trim(); + return resultp; + }, + }, + { + re: / push /, + run(resultp: any) { + resultp.login = resultp.raw.trim(); + return resultp; }, },