npmci/ts/manager.docker/mod.classes.dockerregistry.ts

49 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

import { logger } from '../npmci.logging.js';
import * as plugins from './mod.plugins.js';
import { bash } from '../npmci.bash.js';
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-11-24 14:00:19 +00:00
public registryUrl: string;
public username: string;
public password: string;
2018-04-04 20:25:13 +00:00
constructor(optionsArg: IDockerRegistryConstructorOptions) {
this.registryUrl = optionsArg.registryUrl;
this.username = optionsArg.username;
this.password = optionsArg.password;
2018-11-24 14:00:19 +00:00
logger.log('info', `created DockerRegistry for ${this.registryUrl}`);
2017-08-27 23:03:59 +00:00
}
2018-11-24 14:00:19 +00:00
public static fromEnvString(envString: string): DockerRegistry {
const dockerRegexResultArray = envString.split('|');
2017-08-27 23:03:59 +00:00
if (dockerRegexResultArray.length !== 3) {
2018-11-24 14:00:19 +00:00
logger.log('error', 'malformed docker env var...');
2018-04-04 20:25:13 +00:00
process.exit(1);
return;
2017-08-27 23:03:59 +00:00
}
2023-06-26 00:43:45 +00:00
const registryUrl = dockerRegexResultArray[0].replace('https://', '').replace('http://', '');
2018-11-24 14:00:19 +00:00
const username = dockerRegexResultArray[1];
const password = dockerRegexResultArray[2];
2017-08-27 23:03:59 +00:00
return new DockerRegistry({
registryUrl: registryUrl,
username: username,
2021-05-14 18:11:12 +00:00
password: password,
2018-04-04 20:25:13 +00:00
});
2017-08-27 23:03:59 +00:00
}
2018-11-24 14:00:19 +00:00
public 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}`);
2018-11-24 14:00:19 +00:00
logger.log('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-11-24 14:00:19 +00:00
logger.log('ok', `docker authenticated for ${this.registryUrl}!`);
2017-08-27 23:03:59 +00:00
}
}