Skip to content

Commit 3ed18b6

Browse files
committed
Rework output methods to allow quiet and async sequence variants
1 parent 7186f09 commit 3ed18b6

File tree

1 file changed

+65
-14
lines changed

1 file changed

+65
-14
lines changed

Sources/SwiftlyCore/ModeledCommandLine.swift

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ import Foundation
22
import Subprocess
33
import SystemPackage
44

5-
public enum CommandLineError: Error {
6-
case invalidArgs
7-
case errorExit(exitCode: Int32, program: String)
8-
case unknownVersion
9-
}
10-
115
public protocol Runnable {
126
func config() -> Configuration
137
}
@@ -24,6 +18,7 @@ extension Runnable {
2418
error: Error = .discarded
2519
) async throws -> CollectedResult<Output, Error> {
2620
var c = self.config()
21+
2722
// TODO: someday the configuration might have its own environment from the modeled commands. That will require this to be able to merge the environment from the commands with the provided environment.
2823
c.environment = environment
2924

@@ -40,6 +35,7 @@ extension Runnable {
4035
quiet: Bool = false,
4136
) async throws {
4237
var c = self.config()
38+
4339
// TODO: someday the configuration might have its own environment from the modeled commands. That will require this to be able to merge the environment from the commands with the provided environment.
4440
c.environment = environment
4541

@@ -59,23 +55,78 @@ extension Runnable {
5955

6056
public protocol Output: Runnable {}
6157

62-
// TODO: look into making this something that can be Decodable (i.e. streamable)
6358
extension Output {
6459
public func output(
6560
environment: Environment = .inherit,
66-
limit: Int
61+
limit: Int,
62+
quiet: Bool = false
6763
) async throws -> String? {
6864
var c = self.config()
65+
6966
// TODO: someday the configuration might have its own environment from the modeled commands. That will require this to be able to merge the environment from the commands with the provided environment.
7067
c.environment = environment
7168

72-
let output = try await Subprocess.run(
73-
self.config(),
74-
output: .string(limit: limit),
75-
error: .standardError
76-
)
69+
if !quiet {
70+
let result = try await Subprocess.run(
71+
self.config(),
72+
output: .string(limit: limit),
73+
error: .standardError
74+
)
7775

78-
return output.standardOutput
76+
if !result.terminationStatus.isSuccess {
77+
throw RunProgramError(terminationStatus: result.terminationStatus, config: c)
78+
}
79+
80+
return result.standardOutput
81+
} else {
82+
let result = try await Subprocess.run(
83+
self.config(),
84+
output: .string(limit: limit),
85+
error: .discarded
86+
)
87+
88+
if !result.terminationStatus.isSuccess {
89+
throw RunProgramError(terminationStatus: result.terminationStatus, config: c)
90+
}
91+
92+
return result.standardOutput
93+
}
94+
}
95+
96+
public func output(
97+
environment: Environment = .inherit,
98+
limit _: Int,
99+
quiet: Bool = false,
100+
body: (AsyncBufferSequence) -> Void
101+
) async throws {
102+
var c = self.config()
103+
104+
// TODO: someday the configuration might have its own environment from the modeled commands. That will require this to be able to merge the environment from the commands with the provided environment.
105+
c.environment = environment
106+
107+
if !quiet {
108+
let result = try await Subprocess.run(
109+
self.config(),
110+
error: .standardError
111+
) { _, sequence in
112+
body(sequence)
113+
}
114+
115+
if !result.terminationStatus.isSuccess {
116+
throw RunProgramError(terminationStatus: result.terminationStatus, config: c)
117+
}
118+
} else {
119+
let result = try await Subprocess.run(
120+
self.config(),
121+
error: .discarded
122+
) { _, sequence in
123+
body(sequence)
124+
}
125+
126+
if !result.terminationStatus.isSuccess {
127+
throw RunProgramError(terminationStatus: result.terminationStatus, config: c)
128+
}
129+
}
79130
}
80131
}
81132

0 commit comments

Comments
 (0)