@copado/copado-cli
Version:
Copado Developer CLI
70 lines • 3.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const theme_1 = __importDefault(require("../../../service/theme"));
const core_1 = require("@salesforce/core");
const filesystem_1 = require("../../../copado_commons/filesystem");
const sf_plugins_core_1 = require("@salesforce/sf-plugins-core");
core_1.Messages.importMessagesDirectory(__dirname);
const messages = core_1.Messages.loadMessages('@copado/copado-cli', 'copado_auth');
class AuthSet extends sf_plugins_core_1.SfCommand {
async run() {
let copadousername;
const { flags } = await this.parse(AuthSet);
const alias = flags.alias;
const username = flags.targetusername;
const usernames = await this.getAllUserName();
if (username) {
copadousername = usernames.includes(username) ? username : '';
}
else if (alias) {
const stateAggregator = await core_1.StateAggregator.getInstance();
const allAlias = stateAggregator.aliases.getAll();
if (allAlias.hasOwnProperty(alias)) {
copadousername = stateAggregator.aliases.resolveUsername(alias);
}
}
else {
throw new core_1.SfError(messages.getMessage('set.invalidFlag'));
}
if (copadousername) {
const instanceUrl = (await core_1.AuthInfo.create({ username: copadousername })).getFields().instanceUrl;
await this.overwritingUser(copadousername).catch(err => new core_1.SfError(err));
new sf_plugins_core_1.Ux().log(`${theme_1.default.status.Success(messages.getMessage('set.success'), copadousername, messages.getMessage('set.instanceUrl'), instanceUrl)}`);
}
else if (alias) {
throw new core_1.SfError(`${messages.getMessage('set.noUserFoundWithAlias')}`);
}
else if (username) {
throw new core_1.SfError(`${messages.getMessage('set.noUserFoundWithUsername')}`);
}
return { copadousername };
}
// PRIVATE
async overwritingUser(copadousername) {
let configAsGroup = await filesystem_1.CopadoFiles.getUserConfig(copadousername);
const configValues = new Map([
[`defaultlistview`, `Cli Listview`],
[`externalid`, '']
]);
await filesystem_1.CopadoFiles.setCurrentUser(copadousername);
await filesystem_1.CopadoFiles.setInGroup(configAsGroup, new Map([['copadousername', copadousername]]), 'auth');
await filesystem_1.CopadoFiles.setInGroup(configAsGroup, configValues, 'config');
}
async getAllUserName() {
return await core_1.AuthInfo.listAllAuthorizations().then(auth => {
return auth === null || auth === void 0 ? void 0 : auth.map(authfile => authfile.username);
});
}
}
exports.default = AuthSet;
AuthSet.description = messages.getMessage('set.description');
AuthSet.examples = [messages.getMessage('set.example1'), messages.getMessage('set.example2')];
AuthSet.supportsUsername = true;
AuthSet.flags = {
targetusername: sf_plugins_core_1.Flags.string({ char: 'u', description: messages.getMessage('set.flags.targetusername') }),
alias: sf_plugins_core_1.Flags.string({ char: 'a', description: messages.getMessage('set.flags.alias'), exclusive: ['targetusername'] })
};
//# sourceMappingURL=set.js.map