1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 03:50:08 +02:00

🐛 Don't use whoami and use NSHomeDirectory()

- 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)
This commit is contained in:
2022-08-29 18:15:13 +02:00
parent f4b1e0745a
commit 9de4fc6712
10 changed files with 35 additions and 41 deletions

View File

@ -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"
}

View File

@ -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
}
}

View File

@ -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) {

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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...")

View File

@ -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
)

View File

@ -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

View File

@ -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",