@sasjs/cli
Version:
Command line interface for SASjs
386 lines (385 loc) • 20.5 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var _a, _b;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContextCommand = void 0;
var utils_1 = require("@sasjs/utils");
var command_1 = require("../../types/command");
var targetCommand_1 = require("../../types/command/targetCommand");
var utils_2 = require("../../utils");
var create_1 = require("./create");
var delete_1 = require("./delete");
var edit_1 = require("./edit");
var export_1 = require("./export");
var parseConfig_1 = require("./internal/parseConfig");
var validateConfigPath_1 = require("./internal/validateConfigPath");
var list_1 = require("./list");
var ContextSubCommand;
(function (ContextSubCommand) {
ContextSubCommand["Create"] = "create";
ContextSubCommand["Delete"] = "delete";
ContextSubCommand["Edit"] = "edit";
ContextSubCommand["Export"] = "export";
ContextSubCommand["List"] = "list";
})(ContextSubCommand || (ContextSubCommand = {}));
var syntax = (_a = {},
_a[ContextSubCommand.Create] = 'context <subCommand>',
_a[ContextSubCommand.Delete] = 'context <subCommand> <contextName>',
_a[ContextSubCommand.Edit] = 'context <subCommand> [contextName]',
_a[ContextSubCommand.Export] = 'context <subCommand> <contextName>',
_a[ContextSubCommand.List] = 'context <subCommand>',
_a);
var usage = (_b = {},
_b[ContextSubCommand.Create] = 'sasjs context create --source <source-JSON-file-path> --target <target-name>',
_b[ContextSubCommand.Delete] = 'sasjs context delete <context-name> --target <target-name>',
_b[ContextSubCommand.Edit] = 'sasjs context edit <contextName> --source <source-JSON-file-path> --target <target-name>',
_b[ContextSubCommand.Export] = 'sasjs context export <context-name> --target <target-name>',
_b[ContextSubCommand.List] = 'sasjs context list --target <target-name>',
_b);
var description = 'Performs operations on contexts.';
var examples = [
{
command: 'sasjs context create -s source.json -t myTarget',
description: 'Creates a context using the configuration from the provided JSON file on the server specified in the target.'
},
{
command: 'sasjs context delete myContext -t myTarget',
description: 'Deletes the specified context from the server specified in the target.'
},
{
command: 'sasjs context edit -s source.json -t myTarget',
description: 'Edit a context using the configuration from the provided JSON file on the server specified in the target.'
},
{
command: 'sasjs context export myContext -t myTarget',
description: 'Exports the definition for the specified context into a JSON file in the current directory.'
},
{
command: 'sasjs context list -t myTarget',
description: 'Lists the accessible and inaccessible compute contexts on the server specified in the target.'
}
];
var commandParseOptions = {
source: {
type: 'string',
alias: 's',
demandOption: true,
description: 'Path to a JSON file containing the context configuration.'
}
};
var ContextCommand = /** @class */ (function (_super) {
__extends(ContextCommand, _super);
function ContextCommand(args) {
var _a, _b;
var subCommand = args[3];
var parseOptions = subCommand === ContextSubCommand.Create ||
subCommand === ContextSubCommand.Edit
? commandParseOptions
: {};
return _super.call(this, args, {
parseOptions: parseOptions,
usage: (_a = usage[subCommand]) !== null && _a !== void 0 ? _a : usage[ContextSubCommand.Create],
description: description,
examples: examples,
syntax: (_b = syntax[subCommand]) !== null && _b !== void 0 ? _b : syntax[ContextSubCommand.Create]
}) || this;
}
ContextCommand.prototype.execute = function () {
return __awaiter(this, void 0, void 0, function () {
var returnCode, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = this.parsed.subCommand;
switch (_a) {
case ContextSubCommand.Create: return [3 /*break*/, 1];
case ContextSubCommand.Delete: return [3 /*break*/, 3];
case ContextSubCommand.Edit: return [3 /*break*/, 5];
case ContextSubCommand.Export: return [3 /*break*/, 7];
case ContextSubCommand.List: return [3 /*break*/, 9];
}
return [3 /*break*/, 11];
case 1: return [4 /*yield*/, this.executeCreateContext()];
case 2:
returnCode = _b.sent();
return [3 /*break*/, 12];
case 3: return [4 /*yield*/, this.executeDeleteContext()];
case 4:
returnCode = _b.sent();
return [3 /*break*/, 12];
case 5: return [4 /*yield*/, this.executeEditContext()];
case 6:
returnCode = _b.sent();
return [3 /*break*/, 12];
case 7: return [4 /*yield*/, this.executeExportContext()];
case 8:
returnCode = _b.sent();
return [3 /*break*/, 12];
case 9: return [4 /*yield*/, this.executeListContext()];
case 10:
returnCode = _b.sent();
return [3 /*break*/, 12];
case 11:
returnCode = command_1.ReturnCode.InvalidCommand;
return [3 /*break*/, 12];
case 12: return [2 /*return*/, returnCode];
}
});
});
};
ContextCommand.prototype.getConfig = function () {
return __awaiter(this, void 0, void 0, function () {
var configPath, isValidPath, content;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this._config) {
return [2 /*return*/, this._config];
}
configPath = this.parsed.source;
return [4 /*yield*/, (0, validateConfigPath_1.validateConfigPath)(configPath)];
case 1:
isValidPath = _a.sent();
if (!isValidPath) {
throw new Error('Invalid source JSON path.');
}
return [4 /*yield*/, (0, utils_1.readFile)(configPath).catch(function () {
throw new Error("Error reading file at ".concat(configPath, "."));
})];
case 2:
content = _a.sent();
this._config = (0, parseConfig_1.parseConfig)(content);
return [2 /*return*/, this._config];
}
});
});
};
ContextCommand.prototype.executeCreateContext = function () {
return __awaiter(this, void 0, void 0, function () {
var target, sasjs, config, authConfig, returnCode;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getTargetInfo()];
case 1:
target = (_a.sent()).target;
sasjs = (0, utils_2.getSASjs)(target);
return [4 /*yield*/, this.getConfig().catch(function (err) {
var _a;
(_a = process.logger) === null || _a === void 0 ? void 0 : _a.error("Unable to create context. Error fetching context configuration from ".concat(_this.parsed.source, ": "), err);
return null;
})];
case 2:
config = _a.sent();
if (!config) {
return [2 /*return*/, command_1.ReturnCode.InternalError];
}
return [4 /*yield*/, (0, utils_2.getAuthConfig)(target).catch(function (err) {
var _a;
(_a = process.logger) === null || _a === void 0 ? void 0 : _a.error('Unable to create context. Error fetching auth config: ', err);
return null;
})];
case 3:
authConfig = _a.sent();
if (!authConfig) {
return [2 /*return*/, command_1.ReturnCode.InternalError];
}
return [4 /*yield*/, (0, create_1.create)(config, sasjs, authConfig.access_token)
.then(function () {
return command_1.ReturnCode.Success;
})
.catch(function () {
return command_1.ReturnCode.InternalError;
})];
case 4:
returnCode = _a.sent();
return [2 /*return*/, returnCode];
}
});
});
};
ContextCommand.prototype.executeDeleteContext = function () {
return __awaiter(this, void 0, void 0, function () {
var target, sasjs, authConfig, returnCode;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getTargetInfo()];
case 1:
target = (_a.sent()).target;
sasjs = (0, utils_2.getSASjs)(target);
return [4 /*yield*/, (0, utils_2.getAuthConfig)(target).catch(function (err) {
var _a;
(_a = process.logger) === null || _a === void 0 ? void 0 : _a.error('Unable to create context. Error fetching auth config: ', err);
return null;
})];
case 2:
authConfig = _a.sent();
if (!authConfig) {
return [2 /*return*/, command_1.ReturnCode.InternalError];
}
return [4 /*yield*/, (0, delete_1.deleteContext)(this.parsed.contextName, sasjs, authConfig.access_token)
.then(function () {
return command_1.ReturnCode.Success;
})
.catch(function () {
return command_1.ReturnCode.InternalError;
})];
case 3:
returnCode = _a.sent();
return [2 /*return*/, returnCode];
}
});
});
};
ContextCommand.prototype.executeEditContext = function () {
return __awaiter(this, void 0, void 0, function () {
var target, sasjs, config, authConfig, returnCode;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getTargetInfo()];
case 1:
target = (_a.sent()).target;
sasjs = (0, utils_2.getSASjs)(target);
return [4 /*yield*/, this.getConfig().catch(function (err) {
var _a;
(_a = process.logger) === null || _a === void 0 ? void 0 : _a.error("Unable to edit context. Error fetching context configuration from ".concat(_this.parsed.source, ": "), err);
return null;
})];
case 2:
config = _a.sent();
if (!config)
return [2 /*return*/, command_1.ReturnCode.InternalError];
return [4 /*yield*/, (0, utils_2.getAuthConfig)(target).catch(function (err) {
var _a;
(_a = process.logger) === null || _a === void 0 ? void 0 : _a.error('Unable to edit context. Error fetching auth config: ', err);
return null;
})];
case 3:
authConfig = _a.sent();
if (!authConfig)
return [2 /*return*/, command_1.ReturnCode.InternalError];
return [4 /*yield*/, (0, edit_1.edit)(this.parsed.contextName, config, sasjs, authConfig.access_token)
.then(function () { return command_1.ReturnCode.Success; })
.catch(function () { return command_1.ReturnCode.InternalError; })];
case 4:
returnCode = _a.sent();
return [2 /*return*/, returnCode];
}
});
});
};
ContextCommand.prototype.executeExportContext = function () {
return __awaiter(this, void 0, void 0, function () {
var target, sasjs, authConfig, returnCode;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getTargetInfo()];
case 1:
target = (_a.sent()).target;
sasjs = (0, utils_2.getSASjs)(target);
return [4 /*yield*/, (0, utils_2.getAuthConfig)(target).catch(function (err) {
var _a;
(_a = process.logger) === null || _a === void 0 ? void 0 : _a.error('Unable to create context. Error fetching auth config: ', err);
return null;
})];
case 2:
authConfig = _a.sent();
if (!authConfig)
return [2 /*return*/, command_1.ReturnCode.InternalError];
return [4 /*yield*/, (0, export_1.exportContext)(this.parsed.contextName, sasjs, authConfig.access_token)
.then(function () { return command_1.ReturnCode.Success; })
.catch(function () { return command_1.ReturnCode.InternalError; })];
case 3:
returnCode = _a.sent();
return [2 /*return*/, returnCode];
}
});
});
};
ContextCommand.prototype.executeListContext = function () {
var _a;
return __awaiter(this, void 0, void 0, function () {
var target, sasjs, authConfig, returnCode;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.getTargetInfo()];
case 1:
target = (_b.sent()).target;
if (target.serverType !== utils_1.ServerType.SasViya) {
(_a = process.logger) === null || _a === void 0 ? void 0 : _a.error("'context list' command is only supported for SAS Viya build targets.\nPlease try again with a different target.");
return [2 /*return*/, command_1.ReturnCode.InternalError];
}
sasjs = (0, utils_2.getSASjs)(target);
return [4 /*yield*/, (0, utils_2.getAuthConfig)(target).catch(function (err) {
var _a;
(_a = process.logger) === null || _a === void 0 ? void 0 : _a.error('Unable to create context. Error fetching auth config: ', err);
return null;
})];
case 2:
authConfig = _b.sent();
if (!authConfig)
return [2 /*return*/, command_1.ReturnCode.InternalError];
return [4 /*yield*/, (0, list_1.list)(target, sasjs, authConfig)
.then(function () { return command_1.ReturnCode.Success; })
.catch(function () { return command_1.ReturnCode.InternalError; })];
case 3:
returnCode = _b.sent();
return [2 /*return*/, returnCode];
}
});
});
};
return ContextCommand;
}(targetCommand_1.TargetCommand));
exports.ContextCommand = ContextCommand;