npmci/ts/mod_docker/mod.classes.dockerregistry.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

2018-04-04 20:25:13 +00:00
import * as plugins from './mod.plugins';
import { bash } from '../npmci.bash';
2017-08-27 23:03:59 +00:00
export interface IDockerRegistryConstructorOptions {
2018-04-04 20:25:13 +00:00
registryUrl: string;
username: string;
password: string;
2017-08-27 23:03:59 +00:00
}
export class DockerRegistry {
2018-04-04 20:25:13 +00:00
registryUrl: string;
username: string;
password: string;
constructor(optionsArg: IDockerRegistryConstructorOptions) {
this.registryUrl = optionsArg.registryUrl;
this.username = optionsArg.username;
this.password = optionsArg.password;
plugins.beautylog.info(`created DockerRegistry for ${this.registryUrl}`);
2017-08-27 23:03:59 +00:00
}
2018-04-04 20:25:13 +00:00
static fromEnvString(envString: string): DockerRegistry {
let dockerRegexResultArray = envString.split('|');
2017-08-27 23:03:59 +00:00
if (dockerRegexResultArray.length !== 3) {
2018-04-04 20:25:13 +00:00
plugins.beautylog.error('malformed docker env var...');
process.exit(1);
return;
2017-08-27 23:03:59 +00:00
}
2018-04-04 20:25:13 +00:00
let registryUrl = dockerRegexResultArray[0];
let username = dockerRegexResultArray[1];
let password = dockerRegexResultArray[2];
2017-08-27 23:03:59 +00:00
return new DockerRegistry({
registryUrl: registryUrl,
username: username,
password: password
2018-04-04 20:25:13 +00:00
});
2017-08-27 23:03:59 +00:00
}
2018-04-04 20:25:13 +00:00
async login() {
2017-08-27 23:03:59 +00:00
if (this.registryUrl === 'docker.io') {
2018-04-04 20:25:13 +00:00
await bash(`docker login -u ${this.username} -p ${this.password}`);
plugins.beautylog.info('Logged in to standard docker hub');
2017-08-27 23:03:59 +00:00
} else {
2018-04-04 20:25:13 +00:00
await bash(`docker login -u ${this.username} -p ${this.password} ${this.registryUrl}`);
2017-08-27 23:03:59 +00:00
}
2018-04-04 20:25:13 +00:00
plugins.beautylog.ok(`docker authenticated for ${this.registryUrl}!`);
2017-08-27 23:03:59 +00:00
}
}