This repository was archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathExecuteTaskNowShellOut.swift
93 lines (86 loc) · 3.43 KB
/
ExecuteTaskNowShellOut.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// ExecuteTaskNowShellOut.swift
// RsyncOSX
//
// Created by Thomas Evensen on 12/07/2020.
// Copyright © 2020 Thomas Evensen. All rights reserved.
//
// swiftlint:disable line_length
import Foundation
import ShellOut
final class ExecuteTaskNowShellOut: ExecuteTaskNow {
var error: Bool = false
func executepretask() async throws {
if let index = index {
if let pretask = configurations?.getConfigurations()?[index].pretask {
do {
try await shellOut(to: pretask)
} catch {
let outputprocess = OutputfromProcess()
outputprocess.addlinefromoutput(str: "ShellOut: execute pretask failed")
_ = Logfile(TrimTwo(outputprocess.getOutput() ?? []).trimmeddata, error: true)
}
}
}
}
func executeposttask() async throws {
if let index = index {
if let posttask = configurations?.getConfigurations()?[index].posttask {
do {
try await shellOut(to: posttask)
} catch {
let outputprocess = OutputfromProcess()
outputprocess.addlinefromoutput(str: "ShellOut: execute posttask failed")
_ = Logfile(TrimTwo(outputprocess.getOutput() ?? []).trimmeddata, error: true)
}
}
}
}
@MainActor
override func executetasknow() async {
if let index = index {
// Execute pretask
if configurations?.getConfigurations()?[index].executepretask == 1 {
do {
try await executepretask()
} catch let e {
let error = e as? ShellOutError
let outputprocess = OutputfromProcess()
outputprocess.addlinefromoutput(str: "ShellOut: pretask fault, aborting")
outputprocess.addlinefromoutput(str: error?.message ?? "")
_ = Logfile(TrimTwo(outputprocess.getOutput() ?? []).trimmeddata, error: true)
self.error = true
}
}
guard error == false else { return }
if let hiddenID = configurations?.gethiddenID(index: index) {
if let arguments = configurations?.arguments4rsync(hiddenID: hiddenID, argtype: .arg) {
startstopindicators?.startIndicatorExecuteTaskNow()
let process = RsyncProcessAsync(arguments: arguments,
config: configurations?.getConfigurations()?[index],
processtermination: processtermination)
await process.executeProcess()
}
}
}
}
override func processtermination(data: [String]?) {
startstopindicators?.stopIndicator()
if let index = index {
configurations?.setCurrentDateonConfiguration(index: index, outputfromrsync: data)
}
deinitDelegate?.deinitexecutetasknow()
command = nil
presentoutputfromrsync(data: data)
// Execute pretask
if let index = index {
if configurations?.getConfigurations()?[index].executeposttask == 1 {
Task {
do {
try await executeposttask()
}
}
}
}
}
}