Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,22 @@ jobs:
run: tool/yarn tsc --noEmit
- name: Prettier
run: tool/yarn prettier --check .
- name: Set up SSH
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Test in SSH environment
run: |
ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} 'cd /path/to/extension && tool/yarn test'
- name: Set up Dev Container
uses: devcontainers/ci@v0
with:
image: mcr.microsoft.com/vscode/devcontainers/base:0-focal
- name: Test in Dev Container
run: |
docker exec -w /path/to/extension devcontainer tool/yarn test
- name: Set up GitHub Codespaces
uses: actions/checkout@v3
- name: Test in GitHub Codespaces
run: |
codespaces exec -w /path/to/extension tool/yarn test
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
],
"extensionKind": [
"ui",
"workspace"
"workspace",
"remote"
],
"main": "./dist/extension.js",
"contributes": {
Expand Down
31 changes: 31 additions & 0 deletions src/config-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,34 @@ test('set persists config to disk', () => {
const f = fs.readFileSync(configPath, 'utf8');
expect(JSON.parse(f)).toEqual({ hosts });
});

test('withContext initializes remote-specific configurations if running in a remote context', () => {
vi.spyOn(vscode.env, 'remoteName', 'get').mockReturnValue('ssh-remote');
const cm = ConfigManager.withGlobalStorageUri(globalStorageUri);
expect(cm.config.remoteHost).toBe('ssh-remote');
});

test('setForHost handles remote-specific configurations', () => {
const cm = new ConfigManager(configPath);
const hostname = 'remote-host';
const remoteConfig = {
remoteHost: 'remote-host',
remotePort: 22,
remoteUser: 'remote-user',
};

cm.setForHost(hostname, 'remoteHost', remoteConfig.remoteHost);
cm.setForHost(hostname, 'remotePort', remoteConfig.remotePort);
cm.setForHost(hostname, 'remoteUser', remoteConfig.remoteUser);

expect(cm.config.hosts?.[hostname]?.remoteHost).toBe(remoteConfig.remoteHost);
expect(cm.config.hosts?.[hostname]?.remotePort).toBe(remoteConfig.remotePort);
expect(cm.config.hosts?.[hostname]?.remoteUser).toBe(remoteConfig.remoteUser);

const f = fs.readFileSync(configPath, 'utf8');
expect(JSON.parse(f)).toEqual({
hosts: {
[hostname]: remoteConfig,
},
});
});
13 changes: 12 additions & 1 deletion src/config-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ interface Host {
rootDir: string;
persistToSSHConfig?: boolean;
differentUserFromSSHConfig?: boolean;
remoteHost?: string;
remotePort?: number;
remoteUser?: string;
}

interface Config {
Expand All @@ -33,7 +36,15 @@ export class ConfigManager {
fs.mkdirSync(globalStoragePath);
}

return new ConfigManager(path.join(globalStoragePath, 'config.json'));
const configManager = new ConfigManager(path.join(globalStoragePath, 'config.json'));

// Detect if the extension is running in a remote context
const isRemote = !!vscode.env.remoteName;
if (isRemote) {
configManager.set('remoteHost', vscode.env.remoteName);
}

return configManager;
}

set<K extends keyof Config>(key: K, value: Config[K]) {
Expand Down
53 changes: 53 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export async function activate(context: vscode.ExtensionContext) {

const configManager = ConfigManager.withGlobalStorageUri(context.globalStorageUri);

// Detect if the extension is running in a remote context
const isRemote = !!vscode.env.remoteName;
vscode.commands.executeCommand('setContext', 'tailscale.isRemote', isRemote);

// walkthrough completion
tailscaleInstance.serveStatus().then((status) => {
// assume if we have any BackendState we are installed
Expand Down Expand Up @@ -240,6 +244,55 @@ export async function activate(context: vscode.ExtensionContext) {
}, 500);
})
);

// Update commands and UI elements to handle remote-specific scenarios
if (isRemote) {
context.subscriptions.push(
vscode.commands.registerCommand('tailscale.node.openTerminal', async (node: PeerRoot | FileExplorer) => {
const { addr, path } = extractAddrAndPath(node);

if (!addr) {
return;
}

const t = vscode.window.createTerminal(addr);
t.sendText(`ssh ${getUsername(configManager, addr)}@${addr}`);

if (path) {
t.sendText(`cd ${path}`);
}

t.show();
})
);

context.subscriptions.push(
vscode.commands.registerCommand('tailscale.node.openRemoteCode', async (node: PeerRoot | FileExplorer) => {
const { addr, path } = extractAddrAndPath(node);

if (addr && configManager.config.hosts?.[addr]?.persistToSSHConfig !== false) {
await syncSSHConfig(addr, configManager);
}

if (node instanceof PeerRoot && addr) {
vscode.commands.executeCommand('vscode.newWindow', {
remoteAuthority: `ssh-remote+${addr}`,
reuseWindow: false,
});
} else if (node instanceof FileExplorer && addr) {
vscode.commands.executeCommand(
'vscode.openFolder',
vscode.Uri.from({
scheme: 'vscode-remote',
authority: `ssh-remote+${addr}`,
path,
}),
{ forceNewWindow: true }
);
}
})
);
}
}

export function deactivate() {
Expand Down