UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

114 lines 3.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isolatedProfileManager = exports.defaultProfileManager = exports.ProfileManager = void 0; const mkdirp = require("mkdirp"); const fs = require("fs"); const logger_1 = require("../util/logger"); const io_1 = require("../util/io"); class ProfileManager { constructor(storeRoot) { this.storeRoot = storeRoot; const logDir = storeRoot + '/log'; mkdirp.sync(logDir); this.logDir = logDir; const profileDir = storeRoot + '/profiles'; mkdirp.sync(profileDir); this.profilesRoot = profileDir; const dockerAuthDir = storeRoot + '/credentials'; mkdirp.sync(dockerAuthDir); const dockerAuthPath = dockerAuthDir + '/docker-auth.json'; try { fs.writeFileSync(dockerAuthPath, JSON.stringify({}, null, 2), { flag: 'wx' }); } catch (e) { logger_1.logger.info('File at dockerAuthPath already created.'); } this.dockerAuthPath = dockerAuthPath; } list() { const res = []; const listing = fs.readdirSync(this.profilesRoot); for (const path of listing) { if (!path.endsWith('.json')) { continue; } const storePath = this.profilesRoot + '/' + path; const buf = fs.readFileSync(storePath); const obj = JSON.parse(buf.toString('utf-8')); res.push(obj); } return res; } readDefault() { const storePath = this.profilesRoot + '/default'; try { const buf = fs.readFileSync(storePath); return buf.toString('utf-8'); } catch (_a) { return undefined; } } guessDefault() { // look CPLN_PROFILE, then try default, return const envProfile = process.env['CPLN_PROFILE']; if (envProfile) { return envProfile; } const explicitProfile = this.readDefault(); if (explicitProfile) { return explicitProfile; } const all = this.list(); if (all.length == 1) { // smart default return all[0].name; } return undefined; } storeDefault(name) { const storePath = this.profilesRoot + '/default'; fs.writeFileSync(storePath, name); } find(name) { const storePath = this.profilesRoot + '/' + name + '.json'; let buf; try { buf = fs.readFileSync(storePath); const obj = JSON.parse(buf.toString('utf-8')); return obj; } catch (_a) { return undefined; } } delete(name) { const storePath = this.profilesRoot + '/' + name + '.json'; let buf; try { buf = fs.readFileSync(storePath); fs.unlinkSync(storePath); const obj = JSON.parse(buf.toString('utf-8')); return obj; } catch (_a) { return undefined; } } update(profile) { const path = this.profilesRoot + '/' + profile.name + '.json'; fs.writeFileSync(path, JSON.stringify(profile, undefined, 2)); } } exports.ProfileManager = ProfileManager; function defaultProfileManager() { const profilesRoot = (0, io_1.getRootDir)(); return new ProfileManager(profilesRoot); } exports.defaultProfileManager = defaultProfileManager; function isolatedProfileManager() { const profilesRoot = './tmp/pf' + Date.now(); return new ProfileManager(profilesRoot); } exports.isolatedProfileManager = isolatedProfileManager; //# sourceMappingURL=manager.js.map