2017-11-27 15:30:59 +00:00
|
|
|
import { Component } from '@angular/core'
|
|
|
|
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
|
2018-01-19 14:31:28 +00:00
|
|
|
import { ToastrService } from 'ngx-toastr'
|
2017-11-27 15:30:59 +00:00
|
|
|
import { ConfigService, AppService } from 'terminus-core'
|
|
|
|
import { SettingsTabComponent } from 'terminus-settings'
|
|
|
|
import { SSHService } from '../services/ssh.service'
|
2019-06-14 21:47:48 +00:00
|
|
|
import { SSHConnection, SSHConnectionGroup } from '../api'
|
2017-11-27 15:30:59 +00:00
|
|
|
|
2019-03-07 01:05:26 +00:00
|
|
|
/** @hidden */
|
2017-11-27 15:30:59 +00:00
|
|
|
@Component({
|
|
|
|
template: require('./sshModal.component.pug'),
|
2018-09-10 15:40:52 +00:00
|
|
|
styles: [require('./sshModal.component.scss')],
|
2017-11-27 15:30:59 +00:00
|
|
|
})
|
|
|
|
export class SSHModalComponent {
|
|
|
|
connections: SSHConnection[]
|
2019-06-14 21:47:48 +00:00
|
|
|
childFolders: SSHConnectionGroup[]
|
2017-11-27 15:30:59 +00:00
|
|
|
quickTarget: string
|
|
|
|
lastConnection: SSHConnection
|
2019-06-14 21:47:48 +00:00
|
|
|
childGroups: SSHConnectionGroup[]
|
2018-09-04 20:39:00 +00:00
|
|
|
groupCollapsed: {[id: string]: boolean} = {}
|
2017-11-27 15:30:59 +00:00
|
|
|
|
|
|
|
constructor (
|
|
|
|
public modalInstance: NgbActiveModal,
|
|
|
|
private config: ConfigService,
|
|
|
|
private ssh: SSHService,
|
|
|
|
private app: AppService,
|
2018-01-19 14:31:28 +00:00
|
|
|
private toastr: ToastrService,
|
2017-11-27 15:30:59 +00:00
|
|
|
) { }
|
|
|
|
|
|
|
|
ngOnInit () {
|
|
|
|
this.connections = this.config.store.ssh.connections
|
|
|
|
if (window.localStorage.lastConnection) {
|
|
|
|
this.lastConnection = JSON.parse(window.localStorage.lastConnection)
|
|
|
|
}
|
2018-09-04 20:39:00 +00:00
|
|
|
this.refresh()
|
2017-11-27 15:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
quickConnect () {
|
|
|
|
let user = 'root'
|
|
|
|
let host = this.quickTarget
|
2018-01-04 20:13:46 +00:00
|
|
|
let port = 22
|
2017-11-27 15:30:59 +00:00
|
|
|
if (host.includes('@')) {
|
|
|
|
[user, host] = host.split('@')
|
|
|
|
}
|
2018-01-04 20:13:46 +00:00
|
|
|
if (host.includes(':')) {
|
|
|
|
port = parseInt(host.split(':')[1])
|
|
|
|
host = host.split(':')[0]
|
|
|
|
}
|
|
|
|
|
2019-06-14 15:49:42 +00:00
|
|
|
const connection: SSHConnection = {
|
2017-11-27 15:30:59 +00:00
|
|
|
name: this.quickTarget,
|
2019-06-14 21:47:48 +00:00
|
|
|
host,
|
|
|
|
user,
|
|
|
|
port,
|
2017-11-27 15:30:59 +00:00
|
|
|
}
|
|
|
|
window.localStorage.lastConnection = JSON.stringify(connection)
|
|
|
|
this.connect(connection)
|
|
|
|
}
|
|
|
|
|
2018-11-11 12:45:47 +00:00
|
|
|
clearLastConnection () {
|
|
|
|
window.localStorage.lastConnection = null
|
|
|
|
this.lastConnection = null
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:30:59 +00:00
|
|
|
connect (connection: SSHConnection) {
|
|
|
|
this.close()
|
2019-01-06 10:14:13 +00:00
|
|
|
this.ssh.openTab(connection).catch(error => {
|
2018-01-19 14:31:28 +00:00
|
|
|
this.toastr.error(`Could not connect: ${error}`)
|
2018-10-09 03:24:15 +00:00
|
|
|
}).then(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.app.activeTab.emitFocused()
|
|
|
|
})
|
2017-11-27 15:30:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
manageConnections () {
|
|
|
|
this.close()
|
|
|
|
this.app.openNewTab(SettingsTabComponent, { activeTab: 'ssh' })
|
|
|
|
}
|
|
|
|
|
|
|
|
close () {
|
|
|
|
this.modalInstance.close()
|
|
|
|
}
|
2018-09-04 08:36:01 +00:00
|
|
|
|
2018-09-04 20:39:00 +00:00
|
|
|
refresh () {
|
|
|
|
this.childGroups = []
|
2018-09-04 08:36:01 +00:00
|
|
|
|
2018-09-04 20:39:00 +00:00
|
|
|
let connections = this.connections
|
|
|
|
if (this.quickTarget) {
|
2018-09-04 20:49:12 +00:00
|
|
|
connections = connections.filter(connection => (connection.name + connection.group).toLowerCase().includes(this.quickTarget))
|
2018-09-04 20:39:00 +00:00
|
|
|
}
|
2018-09-04 08:36:01 +00:00
|
|
|
|
2019-06-14 15:49:42 +00:00
|
|
|
for (const connection of connections) {
|
2018-09-04 20:39:00 +00:00
|
|
|
connection.group = connection.group || null
|
|
|
|
let group = this.childGroups.find(x => x.name === connection.group)
|
|
|
|
if (!group) {
|
|
|
|
group = {
|
|
|
|
name: connection.group,
|
|
|
|
connections: [],
|
2018-09-04 08:36:01 +00:00
|
|
|
}
|
2018-09-04 20:39:00 +00:00
|
|
|
this.childGroups.push(group)
|
2018-09-04 08:36:01 +00:00
|
|
|
}
|
2018-09-04 20:39:00 +00:00
|
|
|
group.connections.push(connection)
|
2018-09-04 08:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-27 15:30:59 +00:00
|
|
|
}
|