2020-04-11 22:29:20 +08:00
|
|
|
const { join } = require('path');
|
2023-01-02 08:02:44 +08:00
|
|
|
const { writeToFile } = require('./helpers');
|
2020-04-11 22:29:20 +08:00
|
|
|
|
2023-01-02 18:41:31 +08:00
|
|
|
const getPrivateKeyPath = (filename) => {
|
2023-01-02 08:02:44 +08:00
|
|
|
const { HOME } = process.env;
|
|
|
|
|
const dir = join(HOME || __dirname, '.ssh');
|
2023-01-02 18:41:31 +08:00
|
|
|
return {
|
|
|
|
|
dir,
|
|
|
|
|
filename,
|
|
|
|
|
path: join(dir, filename)
|
|
|
|
|
};
|
|
|
|
|
};
|
2020-04-11 22:29:20 +08:00
|
|
|
|
2023-01-02 18:41:31 +08:00
|
|
|
const addSshKey = (content, deployKeyName) => {
|
|
|
|
|
const { dir, filename } = getPrivateKeyPath(deployKeyName);
|
2023-01-02 08:02:44 +08:00
|
|
|
writeToFile({ dir, filename: 'known_hosts', content: '' });
|
2023-01-02 18:41:31 +08:00
|
|
|
console.log('✅ [SSH] known_hosts file ensured', dir);
|
2023-01-02 08:02:44 +08:00
|
|
|
writeToFile({ dir, filename, content, isRequired: true });
|
2023-01-02 18:41:31 +08:00
|
|
|
console.log('✅ [SSH] key added to `.ssh` dir ', dir, filename);
|
2020-04-11 22:29:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
2023-01-02 18:41:31 +08:00
|
|
|
getPrivateKeyPath,
|
2020-04-11 22:29:20 +08:00
|
|
|
addSshKey
|
2023-01-02 08:02:44 +08:00
|
|
|
};
|