| 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 | | [--tests-outcome success|failure|skipped|cancelled] |
| 9 | | [--report-outcome success|failure|skipped|cancelled] |
| 10 | | [--runs-to-keep <n>] |
| 11 | | """ |
| 12 | | |
| 13 | | /// The report folder to publish; unused, and so need not exist, when an outcome says |
| 14 | | /// the run has no coverage. |
| 15 | | public let siteFolder: String |
| 16 | | public let pagesBranch: String |
| 17 | | public let title: String |
| 18 | | /// Nil to take the repository's current commit. |
| 19 | | public let commit: String? |
| 20 | | /// Nil to take the current commit's subject. |
| 21 | | public let subject: String? |
| 22 | | public let testsOutcome: ActionOutcome |
| 23 | | public let reportOutcome: ActionOutcome |
| 24 | | public let runsToKeep: Int |
| 25 | | |
| 26 | | /// - Throws: `ReportOptionsError` for an unknown option, an option without its value, |
| 27 | | /// a missing required option, an outcome that isn't one of the four, or a run count |
| 28 | | /// that isn't a whole number of at least one. |
| 29 | 10 | public init(arguments: [String]) throws { |
| 30 | 10 | let values = try OptionValues(arguments: arguments, allowed: Self.options) |
| 31 | 8 | siteFolder = try values.required("--site") |
| 32 | 7 | pagesBranch = try values.required("--pages-branch") |
| 33 | 6 | title = values["--title"] ?? ReportOptions.defaultTitle |
| 34 | 6 | commit = values["--commit"] |
| 35 | 6 | subject = values["--subject"] |
| 36 | 6 | testsOutcome = try Self.outcome(values, "--tests-outcome") |
| 37 | 5 | reportOutcome = try Self.outcome(values, "--report-outcome") |
| 38 | 5 | runsToKeep = try Self.runsToKeep(values) |
| 39 | 2 | } |
| 40 | | |
| 41 | | private static let options: Set = [ |
| 42 | | "--site", "--pages-branch", "--title", "--commit", "--subject", "--tests-outcome", |
| 43 | | "--report-outcome", "--runs-to-keep", |
| 44 | | ] |
| 45 | | |
| 46 | 11 | private static func outcome(_ values: OptionValues, _ option: String) throws -> ActionOutcome { |
| 47 | 11 | guard let value = values[option] else { return .success } |
| 48 | 3 | guard let outcome = ActionOutcome(rawValue: value) else { |
| 49 | 1 | throw ReportOptionsError.notAnOutcome(option: option, value: value) |
| 50 | 2 | } |
| 51 | 2 | return outcome |
| 52 | 11 | } |
| 53 | | |
| 54 | 5 | private static func runsToKeep(_ values: OptionValues) throws -> Int { |
| 55 | 5 | let option = "--runs-to-keep" |
| 56 | 5 | guard let value = values[option] else { return PublishedLayout.defaultRunsToKeep } |
| 57 | 4 | guard let count = Int(value), count >= 1 else { |
| 58 | 3 | throw ReportOptionsError.notPositive(option: option, value: value) |
| 59 | 3 | } |
| 60 | 1 | return count |
| 61 | 5 | } |
| 62 | | } |