diff --git a/vscode-extension/extension.js b/vscode-extension/extension.js index f79bbb7..acc58c1 100644 --- a/vscode-extension/extension.js +++ b/vscode-extension/extension.js @@ -84,12 +84,13 @@ async function configureHost() { }); const sshDir = path.join(os.homedir(), '.ssh'); fs.mkdirSync(sshDir, { recursive: true }); - const generatedPath = expandHomePath( + const mainConfig = path.join(sshDir, 'config'); + const generatedPath = resolveGeneratedSshConfigPath( + mainConfig, config.get('generatedSshConfig') || path.join(sshDir, 'config.dosh'), os.homedir(), process.platform ); - const mainConfig = path.join(sshDir, 'config'); ensureInclude(mainConfig, sshIncludeTarget(mainConfig, generatedPath, process.platform)); upsertBlock(generatedPath, alias, block); return { alias, generatedPath }; @@ -143,6 +144,18 @@ function sshIncludeTarget(mainConfigPath, generatedPath, platform = process.plat return sshConfigPathWord(includePath, platform); } +function resolveGeneratedSshConfigPath( + mainConfigPath, + configuredPath, + home = os.homedir(), + platform = process.platform +) { + const pathApi = platform === 'win32' ? path.win32 : path; + const mainDir = pathApi.resolve(pathApi.dirname(mainConfigPath)); + const expanded = expandHomePath(configuredPath, home, platform); + return pathApi.resolve(mainDir, expanded); +} + function upsertBlock(configPath, alias, block) { const dir = path.dirname(configPath); if (dir && dir !== '.') { @@ -185,7 +198,9 @@ async function showSshConfig() { const api = requireVscode(); const config = api.workspace.getConfiguration('dosh'); const sshDir = path.join(os.homedir(), '.ssh'); - const generatedPath = expandHomePath( + const mainConfig = path.join(sshDir, 'config'); + const generatedPath = resolveGeneratedSshConfigPath( + mainConfig, config.get('generatedSshConfig') || path.join(sshDir, 'config.dosh'), os.homedir(), process.platform @@ -270,6 +285,7 @@ module.exports = { deactivate, expandHomePath, ensureInclude, + resolveGeneratedSshConfigPath, sshBlock, sshConfigPathWord, shellQuote, diff --git a/vscode-extension/extension.test.js b/vscode-extension/extension.test.js index 27ce282..746e2b3 100644 --- a/vscode-extension/extension.test.js +++ b/vscode-extension/extension.test.js @@ -7,6 +7,7 @@ const path = require('node:path'); const { expandHomePath, ensureInclude, + resolveGeneratedSshConfigPath, shellQuote, sshBlock, sshConfigPathWord, @@ -101,6 +102,27 @@ test('home-relative generated config paths expand for POSIX and Windows', () => ); }); +test('relative generated config paths resolve beside the main ssh config', () => { + assert.equal( + resolveGeneratedSshConfigPath( + '/Users/palav/.ssh/config', + 'config.dosh', + '/Users/palav', + 'darwin' + ), + '/Users/palav/.ssh/config.dosh' + ); + assert.equal( + resolveGeneratedSshConfigPath( + 'C:\\Users\\palav\\.ssh\\config', + 'dosh\\config.dosh', + 'C:\\Users\\palav', + 'win32' + ), + 'C:\\Users\\palav\\.ssh\\dosh\\config.dosh' + ); +}); + test('config writers create missing parent directories', () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'dosh-vscode-')); const mainConfig = path.join(root, 'ssh', 'config');