1/// What `covane publish` was asked to do, read from the arguments after the command word.
2public struct PublishOptions: Sendable, Equatable {
3 public static let commandWord = "publish"
4
5 public static let usage = """
6 usage: covane publish --site <report folder> --pages-branch <name> [--title <text>]
7 [--commit <sha>] [--subject <text>]
8 [--run-url <https-url>] [--junit <[label=]file>]...
9 [--tests-outcome success|failure|skipped|cancelled]
10 [--report-outcome success|failure|skipped|cancelled]
11 [--runs-to-keep <n>]
12 """
13
14 /// The report folder to publish; unused, and so need not exist, when an outcome says
15 /// the run has no coverage.
16 public let siteFolder: String
17 public let pagesBranch: String
18 public let title: String
19 /// Nil to take the repository's current commit.
20 public let commit: String?
21 /// Nil to take the current commit's subject.
22 public let subject: String?
23 public let runURL: String?
24 public let junitInputs: [JUnitInput]
25 public let testsOutcome: ActionOutcome
26 public let reportOutcome: ActionOutcome
27 public let runsToKeep: Int
28
29 /// - Throws: `ReportOptionsError` for an unknown option, an option without its value,
30 /// a missing required option, an outcome that isn't one of the four, or a run count
31 /// that isn't a whole number of at least one.
3212 public init(arguments: [String]) throws {
3312 let values = try OptionValues(arguments: arguments, allowed: Self.options)
3410 siteFolder = try values.required("--site")
359 pagesBranch = try values.required("--pages-branch")
368 title = values["--title"] ?? ReportOptions.defaultTitle
378 commit = values["--commit"]
388 subject = values["--subject"]
398 runURL = try values.runURL()
407 junitInputs = try values.all("--junit").map(JUnitInput.init)
417 testsOutcome = try Self.outcome(values, "--tests-outcome")
426 reportOutcome = try Self.outcome(values, "--report-outcome")
436 runsToKeep = try Self.runsToKeep(values)
443 }
45
46 private static let options: Set = [
47 "--run-url", "--junit", "--site", "--pages-branch", "--title", "--commit", "--subject",
48 "--tests-outcome",
49 "--report-outcome", "--runs-to-keep",
50 ]
51
5213 private static func outcome(_ values: OptionValues, _ option: String) throws -> ActionOutcome {
5313 guard let value = values[option] else { return .success }
543 guard let outcome = ActionOutcome(rawValue: value) else {
551 throw ReportOptionsError.notAnOutcome(option: option, value: value)
562 }
572 return outcome
5813 }
59
606 private static func runsToKeep(_ values: OptionValues) throws -> Int {
616 let option = "--runs-to-keep"
626 guard let value = values[option] else { return PublishedLayout.defaultRunsToKeep }
634 guard let count = Int(value), count >= 1 else {
643 throw ReportOptionsError.notPositive(option: option, value: value)
653 }
661 return count
676 }
68}