From 4a9b5fc3ea6037327eed3d6468f223a0961f6046 Mon Sep 17 00:00:00 2001 From: Dave Nicolson Date: Sun, 5 May 2024 19:39:04 +0200 Subject: [PATCH] Check if CLI tool is a symlink --- syntax_highlight_cli/main.swift | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/syntax_highlight_cli/main.swift b/syntax_highlight_cli/main.swift index 333f712..378b165 100644 --- a/syntax_highlight_cli/main.swift +++ b/syntax_highlight_cli/main.swift @@ -9,7 +9,22 @@ import Cocoa import OSLog -let cliUrl = URL(fileURLWithPath: CommandLine.arguments[0]) +func getCliUrl() -> URL { + let fileManager = FileManager.default + let currentExecutablePath = URL(fileURLWithPath: CommandLine.arguments[0]) + + let attributes = try? fileManager.attributesOfItem(atPath: currentExecutablePath.path) + + if let fileType = attributes?[.type] as? FileAttributeType, fileType == .typeSymbolicLink { + if let originalPath = try? fileManager.destinationOfSymbolicLink(atPath: currentExecutablePath.path) { + return URL(fileURLWithPath: originalPath) + } + } + + return currentExecutablePath +} + +let cliUrl = getCliUrl() var standardError = FileHandle.standardError