From 93b257ba5d6c0939274b77ce345379172245b857 Mon Sep 17 00:00:00 2001 From: vybe Date: Wed, 20 May 2026 19:37:08 +0000 Subject: [PATCH] refactor: delegate shell command construction to lib.js functions Replace inline shell command building in QML with calls to parseClipboardDetection, parseStatusResult, buildToggleCommand, and buildCopyCommand. This centralizes sanitization logic and removes duplicated try/catch and ternary blocks from the UI layer. --- tailscalectl/TailscaleWidget.qml | 33 +++++++++++++------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/tailscalectl/TailscaleWidget.qml b/tailscalectl/TailscaleWidget.qml index 235ffc0..705070e 100644 --- a/tailscalectl/TailscaleWidget.qml +++ b/tailscalectl/TailscaleWidget.qml @@ -66,7 +66,7 @@ PluginComponent { stdout: StdioCollector { onStreamFinished: { - root.clipboardCmd = this.text.trim() + root.clipboardCmd = TailscaleLib.parseClipboardDetection(this.text) } } @@ -82,19 +82,11 @@ PluginComponent { stdout: StdioCollector { onStreamFinished: { - try { - const data = JSON.parse(this.text) - root.isConnected = data.BackendState === "Running" - root.tailscaleIP = (data.Self?.TailscaleIPs?.[0]) || "" - root.currentExitNode = TailscaleLib.findActiveExitNode(data.Peer || {}) - root.peers = TailscaleLib.parsePeers(data.Peer || {}) - } catch (e) { - root.isConnected = false - root.tailscaleIP = "" - root.currentExitNode = "" - root.peers = [] - ToastService.showError("tailscalectl", "Failed to parse Tailscale status") - } + const state = TailscaleLib.parseStatusResult(this.text) + root.isConnected = state.isConnected + root.tailscaleIP = state.tailscaleIP + root.currentExitNode = state.currentExitNode + root.peers = state.peers } } @@ -119,11 +111,7 @@ PluginComponent { ToastService.showError("tailscalectl", "Tailscale not available") return } - if (root.isConnected) { - toggleProcess.command = ["tailscale", "down"] - } else { - toggleProcess.command = ["tailscale", "up"] - } + toggleProcess.command = TailscaleLib.buildToggleCommand(root.isConnected) toggleProcess.running = true } @@ -141,7 +129,12 @@ PluginComponent { ToastService.showError("tailscalectl", "No clipboard tool found") return } - copyProcess.command = ["sh", "-c", "printf '%s' '" + text.replace(/'/g, "'\\''") + "' | " + root.clipboardCmd] + var cmd = TailscaleLib.buildCopyCommand(text, root.clipboardCmd) + if (!cmd) { + ToastService.showError("tailscalectl", "Invalid clipboard command") + return + } + copyProcess.command = cmd copyProcess.running = true ToastService.showInfo("Copied " + text + " to clipboard") }