Skip to content

Commit

Permalink
refactor: Separate report generation into different functions (#38)
Browse files Browse the repository at this point in the history
This will hopefully make it easier to change and test different aspects.
  • Loading branch information
jbmorley authored Feb 15, 2025
1 parent 016b705 commit 1b36b87
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
6 changes: 6 additions & 0 deletions Sources/ReporterCore/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ struct Configuration: Codable {
let mailServer: Server
let folders: [String: Policy]

init(contentsOf url: URL) throws {
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
self = try decoder.decode(Self.self, from: data)
}

}
27 changes: 16 additions & 11 deletions Sources/ReporterCore/Reporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,11 @@ public class Reporter {
return Data(md5.finalize())
}

public static func run(configurationURL: URL, snapshotURL: URL) async throws {
let fileManager = FileManager.default

let console = Console()

// Load the configuration
console.log("Loading configuration...")
let data = try Data(contentsOf: configurationURL)
let decoder = JSONDecoder()
let configuration = try decoder.decode(Configuration.self, from: data)
static func report(configuration: Configuration, snapshotURL: URL, console: Console) async throws -> Report {

// Load the snapshot if it exists.
console.log("Loading state...")
let oldState = if fileManager.fileExists(atPath: snapshotURL.path) {
let oldState = if FileManager.default.fileExists(atPath: snapshotURL.path) {
try BinaryDecoder().decode(State.self,
from: try Data(contentsOf: snapshotURL))
} else {
Expand Down Expand Up @@ -144,6 +135,20 @@ public class Reporter {
report.folders.append(KeyedChanges(url: url, changes: changes))
}

return report
}

public static func run(configurationURL: URL, snapshotURL: URL) async throws {

let console = Console()

// Load the configuration.
console.log("Loading configuration...")
let configuration = try Configuration(contentsOf: configurationURL)

// Generate the report.
let report = try await report(configuration: configuration, snapshotURL: snapshotURL, console: console)

// Return early if there are no outstanding changes.
if report.isEmpty {
console.log("No changes detected; skipping report.")
Expand Down

0 comments on commit 1b36b87

Please sign in to comment.