diff --git a/tabby-electron/src/index.ts b/tabby-electron/src/index.ts index eda5431d..733e4006 100644 --- a/tabby-electron/src/index.ts +++ b/tabby-electron/src/index.ts @@ -17,7 +17,7 @@ import { ElectronService } from './services/electron.service' import { ElectronHotkeyProvider } from './hotkeys' import { ElectronConfigProvider } from './config' import { EditSFTPContextMenu } from './sftpContextMenu' -import { OpenSSHImporter } from './openSSHImport' +import { OpenSSHImporter, StaticFileImporter } from './sshImporters' @NgModule({ providers: [ @@ -32,7 +32,8 @@ import { OpenSSHImporter } from './openSSHImport' { provide: ConfigProvider, useClass: ElectronConfigProvider, multi: true }, { provide: FileProvider, useClass: ElectronFileProvider, multi: true }, { provide: SFTPContextMenuItemProvider, useClass: EditSFTPContextMenu, multi: true }, - { provide: SSHProfileImporter, useClass: OpenSSHImporter, multi: true }, + { provide: SSHProfileImporter, useExisting: OpenSSHImporter, multi: true }, + { provide: SSHProfileImporter, useExisting: StaticFileImporter, multi: true }, ], }) export default class ElectronModule { diff --git a/tabby-electron/src/openSSHImport.ts b/tabby-electron/src/sshImporters.ts similarity index 83% rename from tabby-electron/src/openSSHImport.ts rename to tabby-electron/src/sshImporters.ts index 06c8de6e..e8eb3a57 100644 --- a/tabby-electron/src/openSSHImport.ts +++ b/tabby-electron/src/sshImporters.ts @@ -1,15 +1,20 @@ import * as fs from 'fs/promises' +import * as fsSync from 'fs' import * as path from 'path' import slugify from 'slugify' +import * as yaml from 'js-yaml' +import { Injectable } from '@angular/core' import { PartialProfile } from 'tabby-core' import { SSHProfileImporter, PortForwardType, SSHProfile, SSHProfileOptions } from 'tabby-ssh' -function deriveID (name: string): string { - return 'openssh-config:' + slugify(name) -} +import { ElectronService } from './services/electron.service' + +@Injectable({ providedIn: 'root' }) export class OpenSSHImporter extends SSHProfileImporter { async getProfiles (): Promise[]> { + const deriveID = name => 'openssh-config:' + slugify(name) + const results: PartialProfile[] = [] const configPath = path.join(process.env.HOME ?? '~', '.ssh', 'config') try { @@ -127,3 +132,34 @@ export class OpenSSHImporter extends SSHProfileImporter { } } } + +@Injectable({ providedIn: 'root' }) +export class StaticFileImporter extends SSHProfileImporter { + private configPath: string + + constructor ( + electron: ElectronService, + ) { + super() + this.configPath = path.join(electron.app.getPath('userData'), 'ssh-profiles.yaml') + } + + async getProfiles (): Promise[]> { + const deriveID = name => 'file-config:' + slugify(name) + + if (!fsSync.existsSync(this.configPath)) { + return [] + } + + const content = await fs.readFile(this.configPath, 'utf8') + if (!content) { + return [] + } + + return (yaml.load(content) as PartialProfile[]).map(item => ({ + ...item, + id: deriveID(item.name), + type: 'ssh', + })) + } +}