@controlplane/cli
Version:
Control Plane Corporation CLI
149 lines • 5.69 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.newDefaultSession = void 0;
const axios = require("axios");
const uuid = require("uuid");
const format_1 = require("../format/format");
const objects_1 = require("../util/objects");
const logger_1 = require("../util/logger");
const errors_1 = require("../util/errors");
function newDefaultSession(env, args) {
var _a, _b, _c;
let pfName = env.profileManager.guessDefault();
if (args.profile) {
// override
pfName = args.profile;
}
let profile;
if (pfName) {
profile = env.profileManager.find(pfName);
if (!profile) {
throw new Error(`profile <${pfName}> does not exist`);
}
}
else {
const endpoint = args.endpoint || process.env['CPLN_ENDPOINT'] || 'https://api.cpln.io';
const token = args.token || process.env['CPLN_TOKEN'];
// we can talk to to the API with a token & endpoint
if (endpoint && token) {
profile = {
name: 'anonymous',
request: {
endpoint: endpoint,
token: token,
},
context: {},
format: {},
tokenServer: {
code: '',
secret: '',
},
};
}
else {
throw new Error('no profile defined. Set a default one or provide one using --profile');
}
}
const sid = uuid.v4();
logger_1.logger.debug('Starting session ' + sid);
const session = {
id: sid,
env: env,
profile: profile,
debug: (0, objects_1.pick)(args, 'debug', 'verbose'),
context: (0, objects_1.merge)((_a = profile.context) !== null && _a !== void 0 ? _a : {}, {
org: process.env['CPLN_ORG'],
gvc: process.env['CPLN_GVC'],
}, (0, objects_1.pick)(args, 'org', 'gvc', 'profile'), {
profile: pfName,
}),
request: (0, objects_1.merge)((_b = profile.request) !== null && _b !== void 0 ? _b : {}, {
endpoint: process.env['CPLN_ENDPOINT'],
token: process.env['CPLN_TOKEN'],
}, (0, objects_1.pick)(args, 'endpoint', 'insecure', 'timeout', 'token')),
format: (0, objects_1.merge)((_c = profile.format) !== null && _c !== void 0 ? _c : {}, (0, objects_1.pick)(args, 'color', 'max', 'output', 'ts')),
err: (obj) => {
(0, format_1.write)(env.err, obj);
},
out: (obj) => {
(0, format_1.write)(env.out, obj);
},
end(message) {
this.out(message + '\n');
process.exit(0);
},
abort(why) {
var _a;
logger_1.logger.debug('Abnormal exit ', why);
if (why.message) {
// prefer message to the error
this.err(why.message + '\n');
}
else {
this.errFormat(why.error);
}
process.exit((_a = why.exitCode) !== null && _a !== void 0 ? _a : 1);
},
async outFormat(obj, options) {
try {
this.out(await (0, format_1.formatAsync)(this.request, obj, options !== null && options !== void 0 ? options : this.format));
}
catch (e) {
if (e instanceof errors_1.PrettifyTableError) {
this.err(e.message);
throw e.catchedError;
}
if (e instanceof errors_1.FormatNamesError) {
this.err(e.message);
process.exit(1);
}
throw e;
}
},
errFormat(obj, options) {
this.err((0, format_1.format)(obj, { ...(options !== null && options !== void 0 ? options : this.format), _hints: { kind: 'error' } }));
},
};
// Inform about where the profile comes from
if (!args.profile && !!process.env.CPLN_PROFILE) {
logger_1.logger.debug(`Using profile "${process.env.CPLN_PROFILE}" from environment variable "CPLN_PROFILE"`);
}
// Inform about where the token comes from
if (args.token) {
logger_1.logger.debug('Using token from argument "--token"');
}
else if (!!process.env.CPLN_TOKEN) {
logger_1.logger.debug('Using token from environment variable "CPLN_TOKEN"');
}
else if (session.request.token) {
logger_1.logger.info('Using token from the profile');
}
// Inform about where the org comes from
if (!args.org && !!process.env.CPLN_ORG) {
logger_1.logger.debug(`Using org "${process.env.CPLN_ORG}" from environment variable "CPLN_ORG"`);
}
// Inform about where the gvc comes from
if (!args.gvc && !!process.env.CPLN_GVC) {
logger_1.logger.debug(`Using gvc "${process.env.CPLN_GVC}" from environment variable "CPLN_GVC"`);
}
// Inform about where the endpoint comes from
if (!args.endpoint && !!process.env.CPLN_ENDPOINT) {
logger_1.logger.debug(`Using endpoint "${process.env.CPLN_ENDPOINT}" from environment variable "CPLN_ENDPOINT"`);
}
let discovery;
Object.defineProperty(session, 'discovery', {
get: () => {
if (!discovery) {
discovery = axios.default
.get('/discovery', {
baseURL: session.request.endpoint,
})
.then((res) => res.data);
}
return discovery;
},
});
return session;
}
exports.newDefaultSession = newDefaultSession;
//# sourceMappingURL=session.js.map
;