feat: connect remote container and showHome
This commit is contained in:
@@ -29,6 +29,11 @@
|
||||
"command": "superide.showHome",
|
||||
"title": "SuperIDE Home",
|
||||
"category": "Super IDE"
|
||||
},
|
||||
{
|
||||
"command": "superide.connectRemoteContainer",
|
||||
"title": "Connect to a Remote Container",
|
||||
"category": "Super IDE"
|
||||
}
|
||||
],
|
||||
"menus": {
|
||||
@@ -91,7 +96,8 @@
|
||||
"vscode:package": "webpack --mode production && vsce package"
|
||||
},
|
||||
"dependencies": {
|
||||
"fs-plus": "~3.1.1"
|
||||
"fs-plus": "~3.1.1",
|
||||
"node-ssh": "^13.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "~7.21.3",
|
||||
|
77
src/home.js
77
src/home.js
@@ -1,6 +1,79 @@
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
export default class SIHome {
|
||||
constructor() {
|
||||
|
||||
constructor(context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
toggle(url) {
|
||||
const panel = vscode.window.createWebviewPanel(
|
||||
'myWebview',
|
||||
'My Webview',
|
||||
vscode.ViewColumn.One,
|
||||
{}
|
||||
);
|
||||
|
||||
panel.webview.options = {
|
||||
enableScripts: true,
|
||||
};
|
||||
|
||||
panel.webview.html = this.getWebviewContent();
|
||||
|
||||
panel.webview.onDidReceiveMessage(
|
||||
(message) => {
|
||||
switch (message.command) {
|
||||
case 'alert':
|
||||
vscode.window.showInformationMessage(message.text);
|
||||
return;
|
||||
case 'openFolder':
|
||||
this.openFolderInVSCode(message.path);
|
||||
return;
|
||||
}
|
||||
},
|
||||
undefined,
|
||||
this.context.subscriptions
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
openFolderInVSCode(path) {
|
||||
vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(path))
|
||||
.then((result) => {
|
||||
console.log('Opened folder: ', result);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error opening folder: ', error);
|
||||
});
|
||||
}
|
||||
|
||||
getWebviewContent() {
|
||||
return `
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Cat Coding</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Cat Coding</h1>
|
||||
<img src="https://media.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif" width="300" />
|
||||
<button onclick="showAlert()">Show Alert</button>
|
||||
<button onclick="openFolder()">Open Project</button>
|
||||
|
||||
<script>
|
||||
const vscode = acquireVsCodeApi();
|
||||
|
||||
function showAlert() {
|
||||
vscode.postMessage({ command: 'alert', text: 'Hello from the Webview!' });
|
||||
}
|
||||
|
||||
function openFolder() {
|
||||
vscode.postMessage({ command: 'openFolder', path: 'D:/Programs/hello' });
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
}
|
||||
}
|
84
src/main.js
84
src/main.js
@@ -1,7 +1,12 @@
|
||||
import vscode from 'vscode';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import os from 'os';
|
||||
import QuickAccessTreeProvider from './views/quick-access-tree';
|
||||
import SIHome from './home';
|
||||
import * as utils from './utils';
|
||||
|
||||
import QuickAccessTreeProvider from './views/quick-access-tree';
|
||||
import vscode from 'vscode';
|
||||
const {NodeSSH} = require('node-ssh')
|
||||
|
||||
class SuperIDEExtension {
|
||||
constructor() {
|
||||
@@ -12,6 +17,7 @@ class SuperIDEExtension {
|
||||
|
||||
async activate(context) {
|
||||
this.context = context;
|
||||
this.siHome = new SIHome(context);
|
||||
|
||||
this.subscriptions.push(
|
||||
vscode.window.registerTreeDataProvider(
|
||||
@@ -29,7 +35,66 @@ class SuperIDEExtension {
|
||||
vscode.commands.executeCommand('superide.showHome');
|
||||
}
|
||||
|
||||
firstConnect(host, username, password, port, publicKey) {
|
||||
// TODO: finish firstConnect (automatically)
|
||||
async connectRemoteContainer() {
|
||||
const host = 'mengning.com.cn';
|
||||
const username = 'linux';
|
||||
const password = 'a7%Xs&&TXG';
|
||||
const port = 31874;
|
||||
|
||||
console.log('Connecting to remote container...');
|
||||
// detect if local ssh config has the host registered
|
||||
// TODO: use remote.SSH.configFile
|
||||
const sshConfigPath = path.join(os.homedir(), '.ssh', 'config');
|
||||
if (fs.existsSync(sshConfigPath)) {
|
||||
const config = fs.readFileSync(sshConfigPath, 'utf8');
|
||||
// TODO: more robust regex
|
||||
if (config.includes(host)) {
|
||||
console.log('Host already registered in local ssh config');
|
||||
// TODO: connect the specified folder
|
||||
} else {
|
||||
// the host has not been registered in the local ssh config
|
||||
|
||||
// connect the host and add the public key to the remote authorized_keys
|
||||
const defaultPublicKeyPath = path.join(os.homedir(), '.ssh', 'id_rsa.pub');
|
||||
// TODO: if there is no public key, generate one
|
||||
const publicKey = fs.readFileSync(defaultPublicKeyPath, 'utf8');
|
||||
|
||||
await this.firstConnect(
|
||||
host,
|
||||
username,
|
||||
password,
|
||||
port,
|
||||
publicKey
|
||||
)
|
||||
|
||||
// append the host to the local ssh config file
|
||||
const sshConfigContent =
|
||||
`\n
|
||||
Host ${host}
|
||||
HostName ${host}
|
||||
Port ${port}
|
||||
User ${username}
|
||||
PreferredAuthentications publickey
|
||||
IdentityFile ~/.ssh/id_rsa
|
||||
`;
|
||||
// append the host to the local ssh config file
|
||||
fs.writeFileSync(sshConfigPath, sshConfigContent, { encoding: 'utf8', flag: 'a' });
|
||||
console.log('Host registered in local ssh config');
|
||||
|
||||
// connect the host with remote-ssh extension
|
||||
await vscode.commands.executeCommand('opensshremotes.openEmptyWindowInCurrentWindow', host)
|
||||
.then(() => {
|
||||
console.log('Connected to host');
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error connecting to host: ', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async firstConnect(host, username, password, port, publicKey) {
|
||||
const ssh = new NodeSSH();
|
||||
ssh.connect({
|
||||
host: host,
|
||||
@@ -38,9 +103,8 @@ class SuperIDEExtension {
|
||||
port: port
|
||||
}).then(() => {
|
||||
console.log('Connected successfully');
|
||||
ssh.execCommand(`mkdir ~/.ssh &&
|
||||
touch ~/.ssh/authorized_keys &&
|
||||
echo ${publicKey} >> ~/.ssh/authorized_keys`, )
|
||||
ssh.execCommand(`mkdir -p ~/.ssh &&
|
||||
echo '${publicKey}' >> ~/.ssh/authorized_keys`, )
|
||||
.then(result => {
|
||||
if (result.stdout) console.log('STDOUT: ' + result.stdout);
|
||||
if (result.stderr) console.log('STDERR: ' + result.stderr);
|
||||
@@ -50,11 +114,13 @@ class SuperIDEExtension {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
registerGlobalCommands() {
|
||||
this.subscriptions.push(
|
||||
vscode.commands.registerCommand('superide.showHome', (startUrl) =>
|
||||
this.siHome.toggle(startUrl)
|
||||
vscode.commands.registerCommand('superide.showHome', (url) =>
|
||||
this.siHome.toggle(url)
|
||||
),
|
||||
vscode.commands.registerCommand('superide.connectRemoteContainer', () =>
|
||||
this.connectRemoteContainer()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@@ -33,7 +33,7 @@ export default class QuickAccessTreeProvider {
|
||||
undefined,
|
||||
undefined,
|
||||
vscode.TreeItemCollapsibleState.Expanded,
|
||||
[new QuickItem('hello', 'superide.hello')]
|
||||
[new QuickItem('Connect Remote Container', 'superide.connectRemoteContainer')]
|
||||
),
|
||||
];
|
||||
}
|
||||
|
Reference in New Issue
Block a user