From 7e8d256edf4352a9835bf35141907eae1ab9bbad Mon Sep 17 00:00:00 2001 From: Levi Yan Date: Thu, 11 Jul 2024 12:42:23 +0800 Subject: [PATCH] refactor: separate the functions that connect container and first conect container --- src/main.ts | 81 ++--------------------------------------------------- 1 file changed, 2 insertions(+), 79 deletions(-) diff --git a/src/main.ts b/src/main.ts index ab31c8f..a2db457 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,12 +1,7 @@ -import vscode from 'vscode'; -import fs from 'fs'; -import path from 'path'; -import os from 'os'; +import * as vscode from 'vscode'; import QuickAccessTreeProvider from './views/quick-access-tree'; import SIHome from './home'; -const { NodeSSH } = require('node-ssh') - export class SuperIDEExtension { siHome: SIHome; @@ -29,84 +24,12 @@ export class SuperIDEExtension { vscode.commands.executeCommand('superide.showHome'); } - // 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 = - `\nHost ${host}\n HostName ${host}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ~/.ssh/id_rsa\n `; - // 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 - try { - await vscode.commands.executeCommand('opensshremotes.openEmptyWindowInCurrentWindow', host) - } catch (error) { - console.log("Error connecting to host: ", error); - } - } - } - } - - async firstConnect(host: string, username: string, password: string, port: number, publicKey: string): Promise { - const ssh = new NodeSSH(); - - try { - await ssh.connect({ - host: host, - username: username, - password: password, - port: port - }); - - await ssh.execCommand(`mkdir -p ~/.ssh && echo '${publicKey}' >> ~/.ssh/authorized_keys`); - console.log('Public key added to remote authorized_keys'); - } catch (error) { - console.error('Error adding public key: ', error); - } finally { - await ssh.dispose(); - } - } registerGlobalCommands(context: vscode.ExtensionContext) { context.subscriptions.push( - vscode.commands.registerCommand('superide.showHome', (url) => + vscode.commands.registerCommand('superide.showHome', (url: string) => this.siHome.toggle(url) ), - vscode.commands.registerCommand('superide.connectRemoteContainer', () => - this.connectRemoteContainer() - ) ); } }