Files
smartssh/ts/smartssh.classes.sshconfig.ts
T

55 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-10-11 13:05:29 +02:00
import * as plugins from './smartssh.plugins.js';
import * as helpers from './smartssh.classes.helpers.js';
import { SshKey } from './smartssh.classes.sshkey.js';
2016-06-24 20:58:55 +02:00
2016-05-31 19:16:45 +02:00
export class SshConfig {
2018-09-17 22:32:31 +02:00
private _sshKeyArray: SshKey[];
constructor(sshKeyArrayArg: SshKey[]) {
this._sshKeyArray = sshKeyArrayArg;
}
2016-06-25 02:29:34 +02:00
2018-09-17 22:32:31 +02:00
/**
* stores a config file
*/
store(dirPathArg: string) {
plugins.fs.ensureDirSync(dirPathArg);
2018-09-17 22:32:31 +02:00
let configArray: configObject[] = [];
for (const sshKey of this._sshKeyArray) {
let configString = '';
2018-09-17 22:32:31 +02:00
if (sshKey.host) {
configString =
'Host ' +
sshKey.host +
'\n' +
' HostName ' +
sshKey.host +
'\n' +
' IdentityFile ~/.ssh/' +
sshKey.host +
'\n' +
' StrictHostKeyChecking no' +
'\n';
}
configArray.push({
configString: configString,
authorized: sshKey.authorized,
2022-10-11 13:05:29 +02:00
sshKey: sshKey,
2018-09-17 22:32:31 +02:00
});
2016-06-25 15:30:57 +02:00
}
2018-09-17 22:32:31 +02:00
let configFile: string = '';
for (const config of configArray) {
configFile = configFile + config.configString + '\n';
2016-05-31 19:00:52 +02:00
}
plugins.fs.writeFileSync(plugins.path.join(dirPathArg, 'config'), configFile);
2018-09-17 22:32:31 +02:00
}
read(dirPathArg: string) {
return plugins.fs.readFileSync(plugins.path.join(dirPathArg, 'config'), 'utf8');
2018-09-17 22:32:31 +02:00
}
}
2016-06-25 02:10:53 +02:00
2018-09-17 22:32:31 +02:00
export interface configObject {
configString: string;
authorized: boolean;
sshKey: SshKey;
}