RouterOSCommand unterstützt jetzt neben .add auch .set (bestehenden Eintrag ändern statt neuen anzulegen) — nötig, weil WLAN-Interfaces schon vor jeder Konfiguration existieren. SSH löst das per CLI-eigenem "set [find field=value] ..." inline auf; REST hat dafür keine Entsprechung und muss den Eintrag erst per GET suchen (matchField/ matchValue), seine .id auslesen, dann PATCH auf restPath/<id> senden (RestTransport.findItemID). Mit dem Nutzer abgestimmte Entscheidung gegen die einfachere "WLAN nur über SSH"-Variante. WifiNetworkConfig: pro erkanntem Legacy-Wireless-Interface (/interface wireless, type=wlan) eine SSID/Passwort-Konfiguration, Sicherheitsprofil (WPA2) wird zuerst angelegt, dann per set mit dem Interface verknüpft. Geräte ohne WLAN zeigen einen Hinweistext statt des Formulars (User-Anforderung: muss berücksichtigt werden). Geräte mit dem neueren "wifi"-Treiber (type=wifi, wifiwave2/802.11ax) werden erkannt, aber bewusst nicht unterstützt -- anderes Menü, eigener Umbau nötig, dazu Hinweistext. cliPath wurde in allen bisherigen Command-Buildern (Wan/Lan/Vlan) zu menuPath + .add migriert, da RouterOSCommand jetzt operation-basiert ist statt den Aktionswort im Pfad-String zu verstecken. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HReLXMbmPvtQ23p1iWiJNW
79 lines
3.0 KiB
Swift
79 lines
3.0 KiB
Swift
import Foundation
|
|
|
|
/// A single RouterOS configuration change, expressed once and executed over either transport
|
|
/// (rendered as a CLI line for SSH, as a JSON body — plus a lookup for `.set` — for REST).
|
|
struct RouterOSCommand: Equatable, Identifiable {
|
|
enum Operation: Equatable {
|
|
/// Creates a new item under `restPath` (SSH: `<menuPath> add ...`, REST: `POST`).
|
|
case add
|
|
/// Modifies an existing item matched by one field's value (SSH: `<menuPath> set [find
|
|
/// field=value] ...`; REST has no such inline lookup, so it needs a GET first to find
|
|
/// the item's `.id`, then `PATCH restPath/<id>`).
|
|
case set(matchField: String, matchValue: String)
|
|
}
|
|
|
|
var id: String {
|
|
switch operation {
|
|
case .add:
|
|
return "add:\(menuPath):\(summary)"
|
|
case .set(let field, let value):
|
|
return "set:\(menuPath):\(field)=\(value):\(summary)"
|
|
}
|
|
}
|
|
|
|
/// RouterOS menu path without the action word, e.g. "/ip address" or "/interface wireless".
|
|
let menuPath: String
|
|
/// REST resource path under `/rest/`, e.g. "ip/address" or "interface/wireless".
|
|
let restPath: String
|
|
let operation: Operation
|
|
/// RouterOS "words" (key=value fields) to add or change.
|
|
let arguments: [String: String]
|
|
/// Human-readable description shown on the review screen before applying.
|
|
let summary: String
|
|
|
|
static func add(menuPath: String, restPath: String, arguments: [String: String], summary: String) -> RouterOSCommand {
|
|
RouterOSCommand(menuPath: menuPath, restPath: restPath, operation: .add, arguments: arguments, summary: summary)
|
|
}
|
|
|
|
static func set(
|
|
menuPath: String,
|
|
restPath: String,
|
|
matchField: String,
|
|
matchValue: String,
|
|
arguments: [String: String],
|
|
summary: String
|
|
) -> RouterOSCommand {
|
|
RouterOSCommand(
|
|
menuPath: menuPath,
|
|
restPath: restPath,
|
|
operation: .set(matchField: matchField, matchValue: matchValue),
|
|
arguments: arguments,
|
|
summary: summary
|
|
)
|
|
}
|
|
|
|
/// Renders as a RouterOS CLI line, e.g. `/ip address add address=192.168.88.1/24 interface=bridge`
|
|
/// or `/interface wireless set [find name=wlan1] ssid=Home`.
|
|
var cliLine: String {
|
|
let args = renderedArguments
|
|
switch operation {
|
|
case .add:
|
|
return args.isEmpty ? "\(menuPath) add" : "\(menuPath) add \(args)"
|
|
case .set(let field, let value):
|
|
let finder = "[find \(field)=\(Self.quoteIfNeeded(value))]"
|
|
return args.isEmpty ? "\(menuPath) set \(finder)" : "\(menuPath) set \(finder) \(args)"
|
|
}
|
|
}
|
|
|
|
private var renderedArguments: String {
|
|
arguments
|
|
.sorted { $0.key < $1.key }
|
|
.map { "\($0.key)=\(Self.quoteIfNeeded($0.value))" }
|
|
.joined(separator: " ")
|
|
}
|
|
|
|
private static func quoteIfNeeded(_ value: String) -> String {
|
|
value.contains(" ") ? "\"\(value)\"" : value
|
|
}
|
|
}
|