@adpt/cli
Version:
AdaptJS command line interface
109 lines • 3.42 kB
JavaScript
;
/*
* Copyright 2019 Unbounded Systems, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const utils_1 = require("@adpt/utils");
const execa_1 = tslib_1.__importDefault(require("execa"));
const path_1 = require("path");
const shellwords_ts_1 = tslib_1.__importDefault(require("shellwords-ts"));
const which_1 = tslib_1.__importDefault(require("which"));
function isOpenSsh(exeAbsPath) {
if (!path_1.isAbsolute(exeAbsPath)) {
throw new utils_1.InternalError(`isOpenSsh: '${exeAbsPath}' is not an absolute path`);
}
try {
const { stderr } = execa_1.default.sync(exeAbsPath, ["-V"]);
return stderr.includes("OpenSSH");
}
catch (err) {
return false;
}
}
exports.isOpenSsh = isOpenSsh;
function gitSshExe() {
let exe;
const gitSshCmd = process.env.GIT_SSH_COMMAND;
if (gitSshCmd) {
const first = shellwords_ts_1.default.split(gitSshCmd)[0];
if (first)
exe = first;
else
throw new Error(`Error parsing GIT_SSH_COMMAND value '${gitSshCmd}'`);
}
if (!exe) {
const gitSsh = process.env.GIT_SSH;
if (gitSsh)
exe = gitSsh;
}
if (!exe)
exe = "ssh";
// Return absolute path if we can
if (path_1.isAbsolute(exe))
return exe;
try {
return which_1.default.sync(exe);
}
catch (err) {
return exe;
}
}
exports.gitSshExe = gitSshExe;
function gitUsesOpenSsh() {
return isOpenSsh(gitSshExe());
}
exports.gitUsesOpenSsh = gitUsesOpenSsh;
async function withGitSshCommand(hostKeyCheck, f) {
if (hostKeyCheck === "unset")
return f(); // No environment changes
const origVal = process.env.GIT_SSH_COMMAND;
const newVal = addHostKeyChecking(hostKeyCheck);
try {
process.env.GIT_SSH_COMMAND = newVal;
return await f();
}
finally {
if (origVal !== undefined)
process.env.GIT_SSH_COMMAND = origVal;
else
delete process.env.GIT_SSH_COMMAND;
}
}
exports.withGitSshCommand = withGitSshCommand;
function addHostKeyChecking(hostKeyCheck) {
let exe = "ssh";
let args = [];
const gitSshCmd = process.env.GIT_SSH_COMMAND;
if (gitSshCmd) {
const parsed = shellwords_ts_1.default.split(gitSshCmd);
if (parsed.length === 0)
throw new Error(`Error parsing GIT_SSH_COMMAND value '${gitSshCmd}'`);
exe = parsed[0];
args = parsed.slice(1);
}
else {
const gitSsh = process.env.GIT_SSH;
if (gitSsh)
exe = gitSsh;
}
return shellwords_ts_1.default.join([
exe,
"-o", `StrictHostKeyChecking=${hostKeyCheck}`,
...args
]);
}
exports.addHostKeyChecking = addHostKeyChecking;
//# sourceMappingURL=ssh.js.map