@@ -51,6 +51,30 @@ private enum MenuItemTag: Int {
5151 case upgradeBrew = 3
5252}
5353
54+ // MARK: - Brew Package Types
55+ private enum BrewPackageType {
56+ case formulae
57+ case casks
58+
59+ var commandSuffix : String {
60+ switch self {
61+ case . formulae:
62+ return " --formula "
63+ case . casks:
64+ return " --cask "
65+ }
66+ }
67+
68+ var description : String {
69+ switch self {
70+ case . formulae:
71+ return " formulae "
72+ case . casks:
73+ return " casks "
74+ }
75+ }
76+ }
77+
5478// MARK: - Async Utilities
5579private func withTimeout< T> ( seconds: TimeInterval , operation: @escaping ( ) async throws -> T ) async throws -> T {
5680 try await withThrowingTaskGroup ( of: T . self) { group in
@@ -354,27 +378,20 @@ class SparkdockMenubarApp: NSObject, NSApplicationDelegate {
354378 }
355379
356380 // Get outdated formulae count
357- let formulaeCount = await getBrewOutdatedCount ( brewPath: brewPath, type: " formulae " )
381+ let formulaeCount = await getBrewOutdatedCount ( brewPath: brewPath, type: . formulae)
358382 // Get outdated casks count
359- let casksCount = await getBrewOutdatedCount ( brewPath: brewPath, type: " casks " )
383+ let casksCount = await getBrewOutdatedCount ( brewPath: brewPath, type: . casks)
360384
361385 let totalCount = formulaeCount + casksCount
362386 AppConstants . logger. info ( " Found \( formulaeCount) outdated formulae and \( casksCount) outdated casks (total: \( totalCount) ) " )
363387 return ( formulaeCount, casksCount)
364388 }
365389
366- private func getBrewOutdatedCount( brewPath: String , type: String ) async -> Int {
390+ private func getBrewOutdatedCount( brewPath: String , type: BrewPackageType ) async -> Int {
367391 let process = Process ( )
368392 process. executableURL = URL ( fileURLWithPath: " /bin/sh " )
369393
370- let command = switch type {
371- case " formulae " :
372- " \( brewPath) outdated --formula --quiet | wc -l "
373- case " casks " :
374- " \( brewPath) outdated --cask --quiet | wc -l "
375- default :
376- " \( brewPath) outdated --quiet | wc -l "
377- }
394+ let command = " \( brewPath) outdated \( type. commandSuffix) --quiet | wc -l "
378395
379396 process. arguments = [ " -c " , command]
380397
@@ -412,22 +429,22 @@ class SparkdockMenubarApp: NSObject, NSApplicationDelegate {
412429 )
413430
414431 if !finished {
415- AppConstants . logger. error ( " Brew outdated check ( \( type) ) process timed out after \( AppConstants . processTimeout) seconds " )
432+ AppConstants . logger. error ( " Brew outdated check ( \( type. description ) ) process timed out after \( AppConstants . processTimeout) seconds " )
416433 return 0
417434 }
418435
419436 if terminationStatus == 0 {
420437 let data = pipe. fileHandleForReading. readDataToEndOfFile ( )
421438 let output = String ( data: data, encoding: . utf8) ? . trimmingCharacters ( in: . whitespacesAndNewlines) ?? " 0 "
422439 let count = Int ( output) ?? 0
423- AppConstants . logger. info ( " Found \( count) outdated \( type) " )
440+ AppConstants . logger. info ( " Found \( count) outdated \( type. description ) " )
424441 return count
425442 } else {
426- AppConstants . logger. warning ( " Brew outdated check ( \( type) ) failed with exit code \( terminationStatus) " )
443+ AppConstants . logger. warning ( " Brew outdated check ( \( type. description ) ) failed with exit code \( terminationStatus) " )
427444 return 0
428445 }
429446 } catch {
430- AppConstants . logger. error ( " Failed to run brew outdated check ( \( type) ): \( error. localizedDescription) " )
447+ AppConstants . logger. error ( " Failed to run brew outdated check ( \( type. description ) ): \( error. localizedDescription) " )
431448 return 0
432449 }
433450 }
0 commit comments