mirror of
https://github.com/nextcloud/desktop.git
synced 2024-12-19 12:22:16 +03:00
bc22272573
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
69 lines
1.8 KiB
Swift
69 lines
1.8 KiB
Swift
/*
|
|
* Copyright (C) 2024 by Claudio Cambra <claudio.cambra@nextcloud.com>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
* for more details.
|
|
*/
|
|
|
|
import Foundation
|
|
|
|
var task: Process?
|
|
|
|
@discardableResult
|
|
func run(
|
|
_ launchPath: String,
|
|
_ args: [String],
|
|
env: [String: String]? = nil,
|
|
quiet: Bool = false
|
|
) -> Int32 {
|
|
defer { task = nil }
|
|
task = Process()
|
|
|
|
signal(SIGINT) { _ in
|
|
task?.terminate() // Send terminate signal to the task
|
|
exit(0) // Exit the script after cleanup
|
|
}
|
|
|
|
task?.launchPath = launchPath
|
|
task?.arguments = args
|
|
|
|
if let env,
|
|
let combinedEnv = task?.environment?.merging(env, uniquingKeysWith: { (_, new) in new })
|
|
{
|
|
task?.environment = combinedEnv
|
|
}
|
|
|
|
if quiet {
|
|
task?.standardOutput = nil
|
|
task?.standardError = nil
|
|
}
|
|
|
|
task?.launch()
|
|
task?.waitUntilExit()
|
|
return task?.terminationStatus ?? 1
|
|
}
|
|
|
|
func run(
|
|
_ launchPath: String,
|
|
_ args: String...,
|
|
env: [String: String]? = nil,
|
|
quiet: Bool = false
|
|
) -> Int32 {
|
|
return run(launchPath, args, env: env, quiet: quiet)
|
|
}
|
|
|
|
@discardableResult
|
|
func shell(_ commands: String..., env: [String: String]? = nil, quiet: Bool = false) -> Int32 {
|
|
return run("/bin/zsh", ["-c"] + commands, env: env, quiet: quiet)
|
|
}
|
|
|
|
func commandExists(_ command: String) -> Bool {
|
|
return run("/usr/bin/type", command, quiet: true) == 0
|
|
}
|