| 1 | | /// What `covane publish` was asked to do, read from the arguments after the command word. |
| 2 | | public 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|folder>]... |
| 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. |
| 32 | 12 | public init(arguments: [String]) throws { |
| 33 | 12 | let values = try OptionValues(arguments: arguments, allowed: Self.options) |
| 34 | 10 | siteFolder = try values.required("--site") |
| 35 | 9 | pagesBranch = try values.required("--pages-branch") |
| 36 | 8 | title = values["--title"] ?? ReportOptions.defaultTitle |
| 37 | 8 | commit = values["--commit"] |
| 38 | 8 | subject = values["--subject"] |
| 39 | 8 | runURL = try values.runURL() |
| 40 | 7 | junitInputs = try values.all("--junit").map(JUnitInput.init) |
| 41 | 7 | testsOutcome = try Self.outcome(values, "--tests-outcome") |
| 42 | 6 | reportOutcome = try Self.outcome(values, "--report-outcome") |
| 43 | 6 | runsToKeep = try Self.runsToKeep(values) |
| 44 | 3 | } |
| 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 | | |
| 52 | 13 | private static func outcome(_ values: OptionValues, _ option: String) throws -> ActionOutcome { |
| 53 | 13 | guard let value = values[option] else { return .success } |
| 54 | 3 | guard let outcome = ActionOutcome(rawValue: value) else { |
| 55 | 1 | throw ReportOptionsError.notAnOutcome(option: option, value: value) |
| 56 | 2 | } |
| 57 | 2 | return outcome |
| 58 | 13 | } |
| 59 | | |
| 60 | 6 | private static func runsToKeep(_ values: OptionValues) throws -> Int { |
| 61 | 6 | let option = "--runs-to-keep" |
| 62 | 6 | guard let value = values[option] else { return PublishedLayout.defaultRunsToKeep } |
| 63 | 4 | guard let count = Int(value), count >= 1 else { |
| 64 | 3 | throw ReportOptionsError.notPositive(option: option, value: value) |
| 65 | 3 | } |
| 66 | 1 | return count |
| 67 | 6 | } |
| 68 | | } |