From f721741b2308a0bc972bee7e8a4b25cfadeb7dc4 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Tue, 31 Dec 2024 01:03:24 +0530 Subject: [PATCH] Default DeviceInfoProvider --- .../Anisette/DeviceInfo+Fetching.swift | 4 ++-- .../GrandSlam/Anisette/DeviceInfo.swift | 18 ++++++++++++++++-- Sources/SupersignCLISupport/AuthCommand.swift | 14 -------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Sources/Supersign/GrandSlam/Anisette/DeviceInfo+Fetching.swift b/Sources/Supersign/GrandSlam/Anisette/DeviceInfo+Fetching.swift index 5c0df5f..ae4288b 100644 --- a/Sources/Supersign/GrandSlam/Anisette/DeviceInfo+Fetching.swift +++ b/Sources/Supersign/GrandSlam/Anisette/DeviceInfo+Fetching.swift @@ -83,7 +83,7 @@ extension DeviceInfo { } } -#elseif os(Linux) || os(Windows) +#else extension DeviceInfo { public static func current() -> DeviceInfo? { return DeviceInfo( @@ -97,5 +97,5 @@ extension DeviceInfo { } #endif -// FIXME: Figure out DeviceInfo.current() on Windows/Linux +// FIXME: Figure out DeviceInfo.current() outside macOS // (see above) diff --git a/Sources/Supersign/GrandSlam/Anisette/DeviceInfo.swift b/Sources/Supersign/GrandSlam/Anisette/DeviceInfo.swift index 64a0fc9..f1d46b2 100644 --- a/Sources/Supersign/GrandSlam/Anisette/DeviceInfo.swift +++ b/Sources/Supersign/GrandSlam/Anisette/DeviceInfo.swift @@ -92,14 +92,28 @@ public struct DeviceInfo: Codable, Sendable { } -public struct DeviceInfoProvider: TestDependencyKey, Sendable { +extension DeviceInfo { + public enum FetchError: Error { + case couldNotFetch + } + + fileprivate static func fetch() throws -> Self { + guard let deviceInfo = DeviceInfo.current() else { + throw FetchError.couldNotFetch + } + return deviceInfo + } +} + +public struct DeviceInfoProvider: DependencyKey, Sendable { public var fetch: @Sendable () throws -> DeviceInfo public init(fetch: @escaping @Sendable () throws -> DeviceInfo) { self.fetch = fetch } - public static let testValue = DeviceInfoProvider(fetch: unimplemented()) + private static let current = Result { try DeviceInfo.fetch() } + public static let liveValue = DeviceInfoProvider { try current.get() } } extension DependencyValues { diff --git a/Sources/SupersignCLISupport/AuthCommand.swift b/Sources/SupersignCLISupport/AuthCommand.swift index f8ce071..87ad635 100644 --- a/Sources/SupersignCLISupport/AuthCommand.swift +++ b/Sources/SupersignCLISupport/AuthCommand.swift @@ -201,17 +201,3 @@ struct AuthCommand: AsyncParsableCommand { defaultSubcommand: AuthLoginCommand.self ) } - -extension DeviceInfo { - static func fetch() throws -> Self { - guard let deviceInfo = DeviceInfo.current() else { - throw Console.Error("Could not fetch client info.") - } - return deviceInfo - } -} - -extension DeviceInfoProvider: DependencyKey { - private static let current = Result { try DeviceInfo.fetch() } - public static let liveValue = DeviceInfoProvider { try current.get() } -}