SPB Git

Repository Public

HostStore.swift 3.3 KB 55 lines swift

1import Foundation23/// User metadata for SSH hosts (labels, groups, tags, favourites, tmux/remote dir). Keyed by the4/// ssh alias so entries discovered from `~/.ssh/config` can be decorated without copying secrets.5public struct HostRecord: Codable, Sendable, Hashable, Identifiable {6    public var id: UUID7    public var alias: String8    public var hostName: String?9    public var user: String?10    public var port: Int?11    public var label: String?12    public var group: String?13    public var tags: [String]14    public var tmuxSession: String?15    public var remoteDirectory: String?16    public var favorite: Bool17    public var source: String1819    public init(id: UUID = UUID(), alias: String, hostName: String? = nil, user: String? = nil, port: Int? = nil, label: String? = nil, group: String? = nil,20                tags: [String] = [], tmuxSession: String? = nil, remoteDirectory: String? = nil, favorite: Bool = false, source: String = "ssh_config") {21        self.id = id; self.alias = alias; self.hostName = hostName; self.user = user; self.port = port; self.label = label; self.group = group22        self.tags = tags; self.tmuxSession = tmuxSession; self.remoteDirectory = remoteDirectory; self.favorite = favorite; self.source = source23    }24}2526public final class HostStore: Sendable {27    private let db: SQLiteDatabase28    public init(database: SQLiteDatabase) { self.db = database }2930    public func all() throws -> [HostRecord] {31        try db.query("SELECT * FROM ssh_hosts ORDER BY favorite DESC, alias").compactMap { row in32            guard let id = row.string("id").flatMap(UUID.init(uuidString:)), let alias = row.string("alias") else { return nil }33            let tags = (try? JSONDecoder().decode([String].self, from: Data((row.string("tags_json") ?? "[]").utf8))) ?? []34            return HostRecord(id: id, alias: alias, hostName: row.string("hostname"), user: row.string("user"), port: row.int("port"), label: row.string("label"),35                              group: row.string("group_name"), tags: tags, tmuxSession: row.string("tmux_session"), remoteDirectory: row.string("remote_directory"),36                              favorite: row.bool("favorite"), source: row.string("source") ?? "ssh_config")37        }38    }3940    public func upsert(_ h: HostRecord) throws {41        let tags = String(decoding: try JSONEncoder().encode(h.tags), as: UTF8.self)42        try db.execute("""43            INSERT INTO ssh_hosts(id, alias, hostname, user, port, label, group_name, tags_json, tmux_session, remote_directory, favorite, source)44            VALUES(?,?,?,?,?,?,?,?,?,?,?,?)45            ON CONFLICT(alias) DO UPDATE SET hostname=excluded.hostname, user=excluded.user, port=excluded.port, label=excluded.label, group_name=excluded.group_name,46              tags_json=excluded.tags_json, tmux_session=excluded.tmux_session, remote_directory=excluded.remote_directory, favorite=excluded.favorite, source=excluded.source47            """, [.text(h.id.uuidString), .text(h.alias), SQLiteValue(h.hostName), SQLiteValue(h.user), SQLiteValue(h.port), SQLiteValue(h.label), SQLiteValue(h.group),48                  .text(tags), SQLiteValue(h.tmuxSession), SQLiteValue(h.remoteDirectory), SQLiteValue(h.favorite), .text(h.source)])49    }5051    public func delete(alias: String) throws {52        try db.execute("DELETE FROM ssh_hosts WHERE alias=?", [.text(alias)])53    }54}55
Files main
Menu

Theme