sshModal.component.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { Component } from '@angular/core'
  2. import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
  3. import { ToastrService } from 'ngx-toastr'
  4. import { ConfigService, AppService } from 'terminus-core'
  5. import { SettingsTabComponent } from 'terminus-settings'
  6. import { SSHService } from '../services/ssh.service'
  7. import { SSHConnection, SSHConnectionGroup } from '../api'
  8. /** @hidden */
  9. @Component({
  10. template: require('./sshModal.component.pug'),
  11. styles: [require('./sshModal.component.scss')],
  12. })
  13. export class SSHModalComponent {
  14. connections: SSHConnection[]
  15. childFolders: SSHConnectionGroup[]
  16. quickTarget: string
  17. lastConnection: SSHConnection|null = null
  18. childGroups: SSHConnectionGroup[]
  19. groupCollapsed: {[id: string]: boolean} = {}
  20. constructor (
  21. public modalInstance: NgbActiveModal,
  22. private config: ConfigService,
  23. private ssh: SSHService,
  24. private app: AppService,
  25. private toastr: ToastrService,
  26. ) { }
  27. ngOnInit () {
  28. this.connections = this.config.store.ssh.connections
  29. if (window.localStorage.lastConnection) {
  30. this.lastConnection = JSON.parse(window.localStorage.lastConnection)
  31. }
  32. this.refresh()
  33. }
  34. quickConnect () {
  35. let user = 'root'
  36. let host = this.quickTarget
  37. let port = 22
  38. if (host.includes('@')) {
  39. [user, host] = host.split('@')
  40. }
  41. if (host.includes(':')) {
  42. port = parseInt(host.split(':')[1])
  43. host = host.split(':')[0]
  44. }
  45. const connection: SSHConnection = {
  46. name: this.quickTarget,
  47. host,
  48. user,
  49. port,
  50. }
  51. window.localStorage.lastConnection = JSON.stringify(connection)
  52. this.connect(connection)
  53. }
  54. clearLastConnection () {
  55. window.localStorage.lastConnection = null
  56. this.lastConnection = null
  57. }
  58. connect (connection: SSHConnection) {
  59. this.close()
  60. this.ssh.openTab(connection).catch(error => {
  61. this.toastr.error(`Could not connect: ${error}`)
  62. }).then(() => {
  63. setTimeout(() => {
  64. this.app.activeTab.emitFocused()
  65. })
  66. })
  67. }
  68. manageConnections () {
  69. this.close()
  70. this.app.openNewTab(SettingsTabComponent, { activeTab: 'ssh' })
  71. }
  72. close () {
  73. this.modalInstance.close()
  74. }
  75. refresh () {
  76. this.childGroups = []
  77. let connections = this.connections
  78. if (this.quickTarget) {
  79. connections = connections.filter((connection: SSHConnection) => (connection.name + connection.group!).toLowerCase().includes(this.quickTarget))
  80. }
  81. for (const connection of connections) {
  82. connection.group = connection.group || undefined
  83. let group = this.childGroups.find(x => x.name === connection.group)
  84. if (!group) {
  85. group = {
  86. name: connection.group!,
  87. connections: [],
  88. }
  89. this.childGroups.push(group!)
  90. }
  91. group.connections.push(connection)
  92. }
  93. }
  94. }