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'
|
|
|
|
import { SSHConnection } from '../api'
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: require('./sshModal.component.pug'),
|
|
|
|
//styles: [require('./sshModal.component.scss')],
|
|
|
|
})
|
|
|
|
export class SSHModalComponent {
|
|
|
|
connections: SSHConnection[]
|
|
|
|
quickTarget: string
|
|
|
|
lastConnection: SSHConnection
|
2018-09-04 08:36:01 +00:00
|
|
|
currentPath: string
|
|
|
|
childFolders: string[]
|
|
|
|
childConnections: SSHConnection[]
|
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 08:36:01 +00:00
|
|
|
this.currentPath = "/"
|
|
|
|
this.findChildren()
|
|
|
|
}
|
|
|
|
|
|
|
|
filter () {
|
|
|
|
if (!this.quickTarget) {
|
|
|
|
this.findChildren()
|
|
|
|
}
|
2018-09-04 08:39:52 +00:00
|
|
|
else {
|
2018-09-04 08:36:01 +00:00
|
|
|
this.childFolders = [];
|
|
|
|
this.childConnections = this.connections.filter(connection => connection.name.toLowerCase().indexOf(this.quickTarget) >= 0)
|
|
|
|
}
|
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]
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:30:59 +00:00
|
|
|
let connection: SSHConnection = {
|
|
|
|
name: this.quickTarget,
|
2018-01-04 20:13:46 +00:00
|
|
|
host, user, port
|
2017-11-27 15:30:59 +00:00
|
|
|
}
|
|
|
|
window.localStorage.lastConnection = JSON.stringify(connection)
|
|
|
|
this.connect(connection)
|
|
|
|
}
|
|
|
|
|
|
|
|
connect (connection: SSHConnection) {
|
|
|
|
this.close()
|
|
|
|
this.ssh.connect(connection).catch(error => {
|
2018-01-19 14:31:28 +00:00
|
|
|
this.toastr.error(`Could not connect: ${error}`)
|
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
|
|
|
|
|
|
|
findChildren () {
|
|
|
|
this.childFolders = []
|
|
|
|
this.childConnections = []
|
|
|
|
|
|
|
|
if (this.currentPath != "/")
|
|
|
|
this.childFolders.push("..")
|
|
|
|
|
|
|
|
for (let connection of this.connections) {
|
|
|
|
if (!connection.path)
|
|
|
|
connection.path = "/"
|
|
|
|
if (connection.path.startsWith(this.currentPath)) {
|
|
|
|
let folder = connection.path.substr(this.currentPath.length, connection.path.indexOf("/", this.currentPath.length) - this.currentPath.length)
|
|
|
|
if (folder.length == 0) {
|
|
|
|
this.childConnections.push(connection)
|
|
|
|
}
|
|
|
|
else if (this.childFolders.indexOf(folder) < 0) {
|
|
|
|
this.childFolders.push(folder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cd (path: string) {
|
|
|
|
if (path == "..") {
|
|
|
|
path = this.currentPath.substr(0, this.currentPath.lastIndexOf("/", this.currentPath.length - 2) + 1)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
path = this.currentPath + path + '/'
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentPath = path
|
|
|
|
this.findChildren()
|
|
|
|
}
|
2017-11-27 15:30:59 +00:00
|
|
|
}
|