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

Add support for custom environment vars (#183)

This commit adds support for custom environment variables, which can be
set in PHP Monitor's custom configuration file.

Let's say you wish to customize the `COMPOSER_HOME` env variable, like
in #183. You can fix this like so:

```json
{
    "scan_apps": [],
    "services": [],
    "presets": []
    "export": {
        "COMPOSER_HOME": "/absolute/path/to/composer/folder"
    }
}
```

Please note that while it is possible to set the `PATH` this way, you
WILL MOST CERTAINLY break PHP Monitor in the process. Setting other
environment variables should generally not pose an issue, unless said
environment variable affects the output of the shell that PHP Monitor
uses internally.
This commit is contained in:
2022-07-25 21:40:02 +02:00
parent a1df2deec5
commit 418d1e2479
3 changed files with 35 additions and 5 deletions

View File

@ -32,6 +32,9 @@ public class Shell {
*/
public var shell: String = "/bin/sh"
/** Additional exports that are sent if `requiresPath` is set to true. */
public var exports: String = ""
/**
Singleton to access a user shell (with --login)
*/
@ -114,13 +117,23 @@ public class Shell {
Creates a new process with the correct PATH and shell.
*/
public func createTask(for command: String, requiresPath: Bool) -> Process {
let tailoredCommand = requiresPath
? "export PATH=\(Paths.binPath):$PATH && \(command)"
: command
var completeCommand = ""
if requiresPath {
// Basic export (PATH)
completeCommand += "export PATH=\(Paths.binPath):$PATH && "
// Put additional exports in between
if !self.exports.isEmpty {
completeCommand += "\(self.exports) && "
}
}
completeCommand += command
let task = Process()
task.launchPath = self.shell
task.arguments = ["--noprofile", "-norc", "--login", "-c", tailoredCommand]
task.arguments = ["--noprofile", "-norc", "--login", "-c", completeCommand]
return task
}

View File

@ -12,6 +12,7 @@ struct CustomPrefs: Decodable {
let scanApps: [String]
let presets: [Preset]?
let services: [String]?
let environmentVariables: [String: String]?
public func hasPresets() -> Bool {
return self.presets != nil && !self.presets!.isEmpty
@ -21,9 +22,20 @@ struct CustomPrefs: Decodable {
return self.services != nil && !self.services!.isEmpty
}
public func hasEnvironmentVariables() -> Bool {
return self.environmentVariables != nil && !self.environmentVariables!.keys.isEmpty
}
public func getEnvironmentVariables() -> String {
return self.environmentVariables!.map { (key, value) in
return "export \(key)=\(value)"
}.joined(separator: "&&")
}
private enum CodingKeys: String, CodingKey {
case scanApps = "scan_apps"
case presets = "presets"
case services = "services"
case environmentVariables = "export"
}
}

View File

@ -65,7 +65,7 @@ class Preferences {
public init() {
Preferences.handleFirstTimeLaunch()
cachedPreferences = Self.cache()
customPreferences = CustomPrefs(scanApps: [], presets: [], services: [])
customPreferences = CustomPrefs(scanApps: [], presets: [], services: [], environmentVariables: [:])
loadCustomPreferences()
}
@ -252,6 +252,11 @@ class Preferences {
if customPreferences.hasServices() {
Log.info("There are custom services: \(customPreferences.services!)")
}
if customPreferences.hasEnvironmentVariables() {
Log.info("Configuring the additional exports...")
Shell.user.exports = customPreferences.getEnvironmentVariables()
}
} catch {
Log.warn("The ~/.config/phpmon/config.json file seems to be missing or malformed.")
}