1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2026-03-27 22:40:08 +01:00
Files
app/phpmon/Domain/Provision/ZshRunCommand.swift
Nico Verbruggen 9856840533 ♻️ Use discardableResult
- Removed ShellProtocol.quiet(), now that pipe() is discardable
- Detecting PHP versions is also discardable
- The system command is also discardable

This is a nice quality of life change overall, and gets rid of a couple
of silly `_ =` assignments.
2026-02-17 15:17:34 +01:00

58 lines
1.3 KiB
Swift

//
// Provision+zshrc.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 01/08/2025.
// Copyright © 2025 Nico Verbruggen. All rights reserved.
//
class ZshRunCommand {
// MARK: - Container
var container: Container
init(_ container: Container) {
self.container = container
}
// MARK: - Methods
/**
Adds a given line to .zshrc, which may be needed to adjust the PATH.
*/
@discardableResult
private func add(_ text: String) async -> Bool {
// Escape single quotes to prevent shell injection
let escaped = text.replacingOccurrences(of: "'", with: "'\\''")
// Actually add the line to .zshrc
let outcome = await container.shell.pipe("""
touch ~/.zshrc && \
grep -qxF '\(escaped)' ~/.zshrc \
|| echo '\n\n\(escaped)\n' >> ~/.zshrc
""")
// Validate the command executed correctly
if outcome.hasError {
return false
}
return true
}
/**
Adds Homebrew binaries to the PATH.
*/
public func addHomebrewPath() async {
await add("export PATH=$HOME/bin:/opt/homebrew/bin:$PATH")
}
/**
Adds PHP Monitor binaries to the PATH.
*/
public func addPhpMonitorPath() async {
await add("export PATH=$HOME/bin:~/.config/phpmon/bin:$PATH")
}
}