UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

141 lines 4.88 kB
"use strict"; // https://github.com/docker/docker-credential-helpers/blob/master/credentials/credentials.go#L68 Object.defineProperty(exports, "__esModule", { value: true }); exports.Version = exports.Erase = exports.Store = exports.Get = void 0; const version_1 = require("../util/version"); const command_1 = require("../cli/command"); const logger_1 = require("../util/logger"); const io_1 = require("../util/io"); const fs_1 = require("fs"); class Get extends command_1.Command { constructor() { super(); this.command = 'get'; this.describe = 'get'; } builder(yargs) { return yargs; } async handle() { var _a, _b; const input = await (0, io_1.readTextFile)('-'); logger_1.logger.debug(`get-input------\n${input}\n------`); let token = ''; let foundInStore = false; try { if ((0, fs_1.existsSync)(this.env.profileManager.dockerAuthPath)) { logger_1.logger.debug('Using stored file'); const dockerAuthContent = await (0, io_1.readTextFile)(this.env.profileManager.dockerAuthPath); const dockerAuth = JSON.parse(dockerAuthContent); const credentials = dockerAuth[input]; if (credentials) { foundInStore = true; token = credentials.Secret; } } } catch (e) { } if (!foundInStore) { logger_1.logger.debug('Using user token'); // trigger token refresh try { await this.client.get('/about'); } catch (e) { } // get token from session token = (_a = this.session.request.token) !== null && _a !== void 0 ? _a : (_b = this.session.profile.authInfo) === null || _b === void 0 ? void 0 : _b.accessToken; } const creds = { Secret: token, // bearer token requires user being set this value // https://docs.docker.com/engine/reference/commandline/login/#credentials-store Username: '<token>', }; const res = JSON.stringify(creds, undefined, 2); this.session.out(res); // TODO should not we redact the secret instead? const redacted = { ...creds, Username: '<redacted>' }; logger_1.logger.debug(`get-output------\n${JSON.stringify(redacted, undefined, 2)}\n------`); } } exports.Get = Get; class Store extends command_1.Command { constructor() { super(); this.command = 'store'; this.describe = 'store'; } builder(yargs) { return yargs; } async handle() { const input = await (0, io_1.readTextFile)('-'); logger_1.logger.debug(`store: '${input}'`); const inputData = JSON.parse(input); let dockerAuth = {}; try { const dockerAuthContent = await (0, io_1.readTextFile)(this.env.profileManager.dockerAuthPath); dockerAuth = JSON.parse(dockerAuthContent); } catch (e) { logger_1.logger.info('Failed to read or parse docker-auth file.'); } dockerAuth[inputData.ServerURL] = inputData; (0, fs_1.writeFileSync)(this.env.profileManager.dockerAuthPath, JSON.stringify(dockerAuth, null, 2)); } } exports.Store = Store; class Erase extends command_1.Command { constructor() { super(); this.command = 'erase'; this.describe = 'erase'; } builder(yargs) { return yargs; } async handle() { const input = await (0, io_1.readTextFile)('-'); logger_1.logger.debug(`erase: '${input}'`); let dockerAuth = {}; try { const dockerAuthContent = await (0, io_1.readTextFile)(this.env.profileManager.dockerAuthPath); dockerAuth = JSON.parse(dockerAuthContent); delete dockerAuth[input]; } catch (e) { logger_1.logger.info('Failed to read or parse docker-auth file.'); } (0, fs_1.writeFileSync)(this.env.profileManager.dockerAuthPath, JSON.stringify(dockerAuth, null, 2)); } } exports.Erase = Erase; // export class List extends Command { // command = 'list'; // describe = 'list'; // constructor() { // super(); // } // builder(yargs: Argv<Arguments>): Argv { // return yargs; // } // async handle() { // const input = await readTextFile('-'); // logger.debug(`list: '${input}'`); // } // } class Version extends command_1.Command { constructor() { super(); this.command = 'version'; this.describe = 'version'; } builder(yargs) { return yargs; } async handle() { console.log(JSON.stringify(version_1.About, null, 2)); } } exports.Version = Version; //# sourceMappingURL=commands.js.map