From 1fb8b46622b5777e626178337a3ce8f26efca06b Mon Sep 17 00:00:00 2001 From: Matt Soghoian Date: Mon, 13 Aug 2018 21:36:51 -0700 Subject: [PATCH] Add support for docker login command, update tests and readme. --- README.md | 23 +++++++++++++++++++++++ src/index.spec.ts | 39 +++++++++++++++++++++++++++++++++++++++ src/index.ts | 8 ++++++++ 3 files changed, 70 insertions(+) diff --git a/README.md b/README.md index 09cd886..2305cfe 100644 --- a/README.md +++ b/README.md @@ -578,6 +578,29 @@ docker.command('search nginxcont').then(function (data) { // } ``` +* docker login + +```js +docker.command('login -u myusername -p mypassword').then(function (data) { + console.log('data = ', data); + // Successful login + }, function (rejected) { + console.log('rejected = ', rejected); + // Failed login + }); + +// data = { command: 'docker login -u myusername -p mypassword ', +// raw: 'Login Succeeded\n', +// login: 'Login Succeeded' } + +// rejected = error: 'Error: Command failed: docker login -u fakeUsername -p fakePassword +// WARNING! Using --password via the CLI is insecure. Use --password-stdin. +// Error response from daemon: Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password +// ' stdout = '' stderr = 'WARNING! Using --password via the CLI is insecure. Use --password-stdin. +// Error response from daemon: Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password +``` + + ## License MIT diff --git a/src/index.spec.ts b/src/index.spec.ts index 536bb4a..8af734d 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -95,4 +95,43 @@ test('docker-cli-js', t => { }); }); + t.test('login success', t => { + + let docker = new Docker(); + + // if this these credentials ever fail, they should be replaced with new valid ones. + return docker.command('login -u myusername -p mypassword').then(function(data) { + console.log('data = ', data); + + // if login succeeds, these tests should pass + t.notOk(/error/.test(data)); + t.ok(data.login); + }, function(data) { + console.log('data = ', data); + + // if login is rejected, these tests should fail + t.notOk(/error/.test(data)); + t.ok(data.login); + }); + }); + + t.test('login fail', t => { + + let docker = new Docker(); + + return docker.command('login -u fakeUsername -p fakePassword').then(function (data) { + console.log('data = ', data); + + // if login succeeds, these tests should fail + t.ok(/error/.test(data)); + t.notOk(data.login); + }, function (data) { + console.log('data = ', data); + + // if login is rejected, these tests should pass + t.ok(/error/.test(data)); + t.notOk(data.login); + }); + }); + }); diff --git a/src/index.ts b/src/index.ts index 64c3daa..d44ab57 100644 --- a/src/index.ts +++ b/src/index.ts @@ -124,6 +124,14 @@ const extractResult = function (result: any) { resultp.images = cliTable2Json(lines); + return resultp; + }, + }, + { + re: / login /, + run: function (resultp: any) { + resultp.login = resultp.raw.trim(); + return resultp; }, },