SPB Git

Repository Public

WorkspaceController+Hosts.swift 2.9 KB 53 lines swift

1import Foundation2import PersistenceCore3import RemoteCore4import WorkspaceCore56extension WorkspaceController {7    /// Hosts = concrete `~/.ssh/config` entries decorated with stored user metadata, plus manual entries.8    /// Host IDs are derived deterministically from the alias so persisted `.ssh` panes stay valid.9    func loadHosts() {10        let records = (try? persistence?.hosts.all()) ?? []11        let byAlias = Dictionary(records.map { ($0.alias, $0) }, uniquingKeysWith: { a, _ in a })12        var result: [HostID: SSHHost] = [:]13        for var host in SSHConfigParser.loadUserConfig() {14            if let r = byAlias[host.alias] {15                host.label = r.label; host.group = r.group; host.tags = r.tags; host.tmuxSession = r.tmuxSession16                host.remoteDirectory = r.remoteDirectory; host.favorite = r.favorite17            }18            host.id = Self.hostUUID(for: host.alias)19            result[HostID(rawValue: host.id)] = host20        }21        for r in records where r.source == "manual" && result.values.first(where: { $0.alias == r.alias }) == nil {22            let host = SSHHost(id: Self.hostUUID(for: r.alias), alias: r.alias, hostName: r.hostName, user: r.user, port: r.port, label: r.label, group: r.group,23                               tags: r.tags, tmuxSession: r.tmuxSession, remoteDirectory: r.remoteDirectory, favorite: r.favorite, source: .manual)24            result[HostID(rawValue: host.id)] = host25        }26        hosts = result27    }2829    func saveHost(_ host: SSHHost) {30        let record = HostRecord(id: host.id, alias: host.alias, hostName: host.hostName, user: host.user, port: host.port, label: host.label, group: host.group,31                                tags: host.tags, tmuxSession: host.tmuxSession, remoteDirectory: host.remoteDirectory, favorite: host.favorite, source: host.source.rawValue)32        try? persistence?.hosts.upsert(record)33        loadHosts()34    }3536    func deleteManualHost(_ host: SSHHost) {37        guard host.source == .manual else { return }38        try? persistence?.hosts.delete(alias: host.alias)39        loadHosts()40    }4142    /// UUID v5-style deterministic id from the alias (stable across launches and machines).43    static func hostUUID(for alias: String) -> UUID {44        var hash: UInt64 = 0xcbf29ce48422232545        for byte in alias.utf8 { hash ^= UInt64(byte); hash &*= 0x100000001b3 }46        var second: UInt64 = 0x84222325cbf29ce447        for byte in alias.utf8.reversed() { second ^= UInt64(byte); second &*= 0x100000001b3 }48        let bytes = withUnsafeBytes(of: hash.bigEndian) { Array($0) } + withUnsafeBytes(of: second.bigEndian) { Array($0) }49        return UUID(uuid: (bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6] & 0x0f | 0x50, bytes[7],50                           bytes[8] & 0x3f | 0x80, bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]))51    }52}53
Files main
Menu

Theme