From 9de4fc671267a118dafcb5fed1fdcb24d4cf448e Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Mon, 29 Aug 2022 18:15:13 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Don't=20use=20`whoami`=20and=20u?= =?UTF-8?q?se=20NSHomeDirectory()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - This commit replaces the usage of `whoami` with `id -un`. - This also changes all `~` replacements with the result of calling the `NSHomeDirectory()` which may differ from `id -un` (#189) --- phpmon/Common/Core/Paths.swift | 6 +++- phpmon/Common/Helpers/Filesystem.swift | 33 ++++++++++--------- phpmon/Common/PHP/PHP Version/PhpHelper.swift | 6 ++-- phpmon/Common/PHP/PhpConfigurationFile.swift | 11 ++----- .../Nginx/NginxConfigurationFile.swift | 10 ++---- .../Integrations/Valet/Sites/ValetSite.swift | 2 +- phpmon/Domain/Preferences/Preferences.swift | 2 +- phpmon/Domain/Presets/Preset.swift | 2 +- phpmon/Domain/Presets/PresetHelper.swift | 2 +- phpmon/Domain/Warnings/WarningManager.swift | 2 +- 10 files changed, 35 insertions(+), 41 deletions(-) diff --git a/phpmon/Common/Core/Paths.swift b/phpmon/Common/Core/Paths.swift index 465951b..89b5d43 100644 --- a/phpmon/Common/Core/Paths.swift +++ b/phpmon/Common/Core/Paths.swift @@ -21,7 +21,7 @@ public class Paths { init() { baseDir = App.architecture != "x86_64" ? .opt : .usr - userName = String(Shell.pipe("whoami").split(separator: "\n")[0]) + userName = String(Shell.pipe("id -un").split(separator: "\n")[0]) } public func detectBinaryPaths() { @@ -57,6 +57,10 @@ public class Paths { return shared.userName } + public static var homePath: String { + return NSHomeDirectory() + } + public static var cellarPath: String { return "\(shared.baseDir.rawValue)/Cellar" } diff --git a/phpmon/Common/Helpers/Filesystem.swift b/phpmon/Common/Helpers/Filesystem.swift index 652ae44..a6d4152 100644 --- a/phpmon/Common/Helpers/Filesystem.swift +++ b/phpmon/Common/Helpers/Filesystem.swift @@ -16,7 +16,7 @@ class Filesystem { */ public static func exists(_ path: String) -> Bool { return FileManager.default.fileExists( - atPath: path.replacingOccurrences(of: "~", with: "/Users/\(Paths.whoami)") + atPath: path.replacingOccurrences(of: "~", with: Paths.homePath) ) } @@ -26,13 +26,29 @@ class Filesystem { public static func fileExists(_ path: String) -> Bool { var isDirectory: ObjCBool = true let exists = FileManager.default.fileExists( - atPath: path.replacingOccurrences(of: "~", with: "/Users/\(Paths.whoami)"), + atPath: path.replacingOccurrences(of: "~", with: Paths.homePath), isDirectory: &isDirectory ) return exists && !isDirectory.boolValue } + /** + Checks if a directory exists at the provided path. + */ + public static func directoryExists(_ path: String) -> Bool { + var isDirectory: ObjCBool = true + let exists = FileManager.default.fileExists( + atPath: path.replacingOccurrences(of: "~", with: Paths.homePath), + isDirectory: &isDirectory + ) + + return exists && isDirectory.boolValue + } + + /** + Checks if a given file is a symbolic link. + */ public static func fileIsSymlink(_ path: String) -> Bool { do { let attribs = try FileManager.default.attributesOfItem(atPath: path) @@ -42,17 +58,4 @@ class Filesystem { } } - /** - Checks if a directory exists at the provided path. - */ - public static func directoryExists(_ path: String) -> Bool { - var isDirectory: ObjCBool = true - let exists = FileManager.default.fileExists( - atPath: path.replacingOccurrences(of: "~", with: "/Users/\(Paths.whoami)"), - isDirectory: &isDirectory - ) - - return exists && isDirectory.boolValue - } - } diff --git a/phpmon/Common/PHP/PHP Version/PhpHelper.swift b/phpmon/Common/PHP/PHP Version/PhpHelper.swift index 9224635..9775edd 100644 --- a/phpmon/Common/PHP/PHP Version/PhpHelper.swift +++ b/phpmon/Common/PHP/PHP Version/PhpHelper.swift @@ -17,10 +17,10 @@ class PhpHelper { let dotless = version.replacingOccurrences(of: ".", with: "") // Determine the dotless name for this PHP version - let destination = "/Users/\(Paths.whoami)/.config/phpmon/bin/pm\(dotless)" + let destination = "\(Paths.homePath)/.config/phpmon/bin/pm\(dotless)" // Check if the ~/.config/phpmon/bin directory is in the PATH - let inPath = Shell.user.PATH.contains("/Users/\(Paths.whoami)/.config/phpmon/bin") + let inPath = Shell.user.PATH.contains("\(Paths.homePath)/.config/phpmon/bin") // Check if we can create symlinks (`/usr/local/bin` must be writable) let canWriteSymlinks = FileManager.default.isWritableFile(atPath: "/usr/local/bin/") @@ -81,7 +81,7 @@ class PhpHelper { } private static func createSymlink(_ dotless: String) { - let source = "/Users/\(Paths.whoami)/.config/phpmon/bin/pm\(dotless)" + let source = "\(Paths.homePath)/.config/phpmon/bin/pm\(dotless)" let destination = "/usr/local/bin/pm\(dotless)" if !Filesystem.fileExists(destination) { diff --git a/phpmon/Common/PHP/PhpConfigurationFile.swift b/phpmon/Common/PHP/PhpConfigurationFile.swift index 277aa6f..a0d4191 100644 --- a/phpmon/Common/PHP/PhpConfigurationFile.swift +++ b/phpmon/Common/PHP/PhpConfigurationFile.swift @@ -32,18 +32,11 @@ class PhpConfigurationFile: CreatedFromFile { /** Resolves a PHP configuration file (.ini) */ static func from(filePath: String) -> Self? { - let path = filePath.replacingOccurrences( - of: "~", - with: "/Users/\(Paths.whoami)" - ) + let path = filePath.replacingOccurrences(of: "~", with: Paths.homePath) do { let fileContents = try String(contentsOfFile: path) - - return Self.init( - path: path, - contents: fileContents - ) + return Self.init(path: path, contents: fileContents) } catch { Log.warn("Could not read the PHP configuration file at: `\(filePath)`") return nil diff --git a/phpmon/Domain/Integrations/Nginx/NginxConfigurationFile.swift b/phpmon/Domain/Integrations/Nginx/NginxConfigurationFile.swift index c8dade6..3ea9eee 100644 --- a/phpmon/Domain/Integrations/Nginx/NginxConfigurationFile.swift +++ b/phpmon/Domain/Integrations/Nginx/NginxConfigurationFile.swift @@ -21,18 +21,12 @@ class NginxConfigurationFile: CreatedFromFile { /** Resolves an nginx configuration file (.conf) */ static func from(filePath: String) -> Self? { - let path = filePath.replacingOccurrences( - of: "~", - with: "/Users/\(Paths.whoami)" - ) + let path = filePath.replacingOccurrences(of: "~", with: Paths.homePath) do { let fileContents = try String(contentsOfFile: path) - return Self.init( - path: path, - contents: fileContents - ) + return Self.init(path: path, contents: fileContents) } catch { Log.warn("Could not read the nginx configuration file at: `\(filePath)`") return nil diff --git a/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift b/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift index b5c2f43..e59282c 100644 --- a/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift +++ b/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift @@ -20,7 +20,7 @@ class ValetSite: DomainListable { /// replacing the user's home folder with ~. lazy var absolutePathRelative: String = { return self.absolutePath - .replacingOccurrences(of: "/Users/\(Paths.whoami)", with: "~") + .replacingOccurrences(of: Paths.homePath, with: "~") }() /// The TLD used to locate this site. diff --git a/phpmon/Domain/Preferences/Preferences.swift b/phpmon/Domain/Preferences/Preferences.swift index ea7f564..1b3e4fd 100644 --- a/phpmon/Domain/Preferences/Preferences.swift +++ b/phpmon/Domain/Preferences/Preferences.swift @@ -222,7 +222,7 @@ class Preferences { moveOutdatedConfigurationFile() // Attempt to load the file if it exists - let url = URL(fileURLWithPath: "/Users/\(Paths.whoami)/.config/phpmon/config.json") + let url = URL(fileURLWithPath: "\(Paths.homePath)/.config/phpmon/config.json") if Filesystem.fileExists(url.path) { Log.info("A custom ~/.config/phpmon/config.json file was found. Attempting to parse...") diff --git a/phpmon/Domain/Presets/Preset.swift b/phpmon/Domain/Presets/Preset.swift index 642c79b..b5d931f 100644 --- a/phpmon/Domain/Presets/Preset.swift +++ b/phpmon/Domain/Presets/Preset.swift @@ -264,7 +264,7 @@ struct Preset: Codable, Equatable { try! String(data: data, encoding: .utf8)! .write( - toFile: "/Users/\(Paths.whoami)/.config/phpmon/preset_revert.json", + toFile: "\(Paths.homePath)/.config/phpmon/preset_revert.json", atomically: true, encoding: .utf8 ) diff --git a/phpmon/Domain/Presets/PresetHelper.swift b/phpmon/Domain/Presets/PresetHelper.swift index 86bc0ae..6ff1242 100644 --- a/phpmon/Domain/Presets/PresetHelper.swift +++ b/phpmon/Domain/Presets/PresetHelper.swift @@ -16,7 +16,7 @@ class PresetHelper { public static func loadRollbackPresetFromFile() { guard let revert = try? String( - contentsOfFile: "/Users/\(Paths.whoami)/.config/phpmon/preset_revert.json", + contentsOfFile: "\(Paths.homePath)/.config/phpmon/preset_revert.json", encoding: .utf8 ) else { PresetHelper.rollbackPreset = nil diff --git a/phpmon/Domain/Warnings/WarningManager.swift b/phpmon/Domain/Warnings/WarningManager.swift index 0c2a9f0..2211899 100644 --- a/phpmon/Domain/Warnings/WarningManager.swift +++ b/phpmon/Domain/Warnings/WarningManager.swift @@ -32,7 +32,7 @@ class WarningManager { ), Warning( command: { - return !Shell.user.PATH.contains("/Users/\(Paths.whoami)/.config/phpmon/bin") && + return !Shell.user.PATH.contains("\(Paths.homePath)/.config/phpmon/bin") && !FileManager.default.isWritableFile(atPath: "/usr/local/bin/") }, name: "Helpers cannot be symlinked and not in PATH",