Skip to content

Commit d9406de

Browse files
authored
Merge pull request #4 from msoghoian/login
Add support for docker login command, update tests and readme.
2 parents aa03aa2 + 1fb8b46 commit d9406de

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,29 @@ docker.command('search nginxcont').then(function (data) {
578578
// }
579579
```
580580

581+
* docker login
582+
583+
```js
584+
docker.command('login -u myusername -p mypassword').then(function (data) {
585+
console.log('data = ', data);
586+
// Successful login
587+
}, function (rejected) {
588+
console.log('rejected = ', rejected);
589+
// Failed login
590+
});
591+
592+
// data = { command: 'docker login -u myusername -p mypassword ',
593+
// raw: 'Login Succeeded\n',
594+
// login: 'Login Succeeded' }
595+
596+
// rejected = error: 'Error: Command failed: docker login -u fakeUsername -p fakePassword
597+
// WARNING! Using --password via the CLI is insecure. Use --password-stdin.
598+
// Error response from daemon: Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password
599+
// ' stdout = '' stderr = 'WARNING! Using --password via the CLI is insecure. Use --password-stdin.
600+
// Error response from daemon: Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password
601+
```
602+
603+
581604
## License
582605

583606
MIT

src/index.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,43 @@ test('docker-cli-js', t => {
9595
});
9696
});
9797

98+
t.test('login success', t => {
99+
100+
let docker = new Docker();
101+
102+
// if this these credentials ever fail, they should be replaced with new valid ones.
103+
return docker.command('login -u myusername -p mypassword').then(function(data) {
104+
console.log('data = ', data);
105+
106+
// if login succeeds, these tests should pass
107+
t.notOk(/error/.test(data));
108+
t.ok(data.login);
109+
}, function(data) {
110+
console.log('data = ', data);
111+
112+
// if login is rejected, these tests should fail
113+
t.notOk(/error/.test(data));
114+
t.ok(data.login);
115+
});
116+
});
117+
118+
t.test('login fail', t => {
119+
120+
let docker = new Docker();
121+
122+
return docker.command('login -u fakeUsername -p fakePassword').then(function (data) {
123+
console.log('data = ', data);
124+
125+
// if login succeeds, these tests should fail
126+
t.ok(/error/.test(data));
127+
t.notOk(data.login);
128+
}, function (data) {
129+
console.log('data = ', data);
130+
131+
// if login is rejected, these tests should pass
132+
t.ok(/error/.test(data));
133+
t.notOk(data.login);
134+
});
135+
});
136+
98137
});

src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ const extractResult = function (result: any) {
124124

125125
resultp.images = cliTable2Json(lines);
126126

127+
return resultp;
128+
},
129+
},
130+
{
131+
re: / login /,
132+
run: function (resultp: any) {
133+
resultp.login = resultp.raw.trim();
134+
127135
return resultp;
128136
},
129137
},

0 commit comments

Comments
 (0)