From 733f84b40c5cdde8ac76d9c4b8388a137aebe7a4 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Mon, 23 May 2022 15:05:26 +0100 Subject: [PATCH] Use swift-log instead of print (#50) --- Package.swift | 5 ++- Sources/SotoCodeGenerator/main.swift | 4 ++ Sources/SotoCodeGeneratorLib/AwsService.swift | 43 +++++++++++-------- .../SotoCodeGeneratorLib/SotoCodeGen.swift | 37 ++++++++++------ 4 files changed, 56 insertions(+), 33 deletions(-) diff --git a/Package.swift b/Package.swift index c4d4c6a..0dd44bb 100644 --- a/Package.swift +++ b/Package.swift @@ -12,6 +12,7 @@ let package = Package( .package(url: "https://github.com/apple/swift-argument-parser.git", from: "0.3.0"), .package(url: "https://github.com/hummingbird-project/hummingbird-mustache.git", from: "1.0.0"), .package(url: "https://github.com/nicklockwood/SwiftFormat.git", .exact("0.48.17")), + .package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"), ], targets: [ .target( @@ -19,6 +20,7 @@ let package = Package( dependencies: [ .byName(name: "SotoCodeGeneratorLib"), .product(name: "ArgumentParser", package: "swift-argument-parser"), + .product(name: "Logging", package: "swift-log") ] ), .target( @@ -27,7 +29,8 @@ let package = Package( .product(name: "SotoSmithy", package: "soto-smithy"), .product(name: "SotoSmithyAWS", package: "soto-smithy"), .product(name: "HummingbirdMustache", package: "hummingbird-mustache"), - .product(name: "SwiftFormat", package: "SwiftFormat") + .product(name: "SwiftFormat", package: "SwiftFormat"), + .product(name: "Logging", package: "swift-log") ] ), .testTarget( diff --git a/Sources/SotoCodeGenerator/main.swift b/Sources/SotoCodeGenerator/main.swift index ef77acc..6b80273 100644 --- a/Sources/SotoCodeGenerator/main.swift +++ b/Sources/SotoCodeGenerator/main.swift @@ -14,6 +14,7 @@ import ArgumentParser import Foundation +import Logging import SotoCodeGeneratorLib struct Command: ParsableCommand, SotoCodeGenCommand { @@ -41,6 +42,9 @@ struct Command: ParsableCommand, SotoCodeGenCommand { @Flag(name: .long, help: "Load smithy") var smithy: Bool = false + @Option(name: .long, help: "Log Level (trace, debug, info, error)") + var logLevel: String? + static var rootPath: String { return #file .split(separator: "/", omittingEmptySubsequences: false) diff --git a/Sources/SotoCodeGeneratorLib/AwsService.swift b/Sources/SotoCodeGeneratorLib/AwsService.swift index 6528042..337181f 100644 --- a/Sources/SotoCodeGeneratorLib/AwsService.swift +++ b/Sources/SotoCodeGeneratorLib/AwsService.swift @@ -14,21 +14,23 @@ import Foundation import HummingbirdMustache +import Logging import SotoSmithy import SotoSmithyAWS struct AwsService { - var model: Model - var serviceName: String - var serviceEndpointPrefix: String - var serviceId: ShapeId - var service: ServiceShape - var serviceProtocolTrait: AwsServiceProtocol - var endpoints: Endpoints - var operations: [ShapeId: OperationShape] - var outputHTMLComments: Bool - - init(_ model: SotoSmithy.Model, endpoints: Endpoints, outputHTMLComments: Bool) throws { + let model: Model + let serviceName: String + let serviceEndpointPrefix: String + let serviceId: ShapeId + let service: ServiceShape + let serviceProtocolTrait: AwsServiceProtocol + let endpoints: Endpoints + let operations: [ShapeId: OperationShape] + let outputHTMLComments: Bool + let logger: Logger + + init(_ model: SotoSmithy.Model, endpoints: Endpoints, outputHTMLComments: Bool, logger: Logger) throws { guard let service = model.select(type: SotoSmithy.ServiceShape.self).first else { throw Error(reason: "No service object") } self.model = model @@ -45,6 +47,7 @@ struct AwsService { self.endpoints = endpoints self.outputHTMLComments = outputHTMLComments + self.logger = logger } /// Return service name from API @@ -566,15 +569,17 @@ struct AwsService { func getPartitionEndpoints() -> [String: (endpoint: String, region: Region)] { var partitionEndpoints: [String: (endpoint: String, region: Region)] = [:] self.endpoints.partitions.forEach { - if let partitionEndpoint = $0.services[self.serviceEndpointPrefix]?.partitionEndpoint { - guard let service = $0.services[self.serviceEndpointPrefix], - let endpoint = service.endpoints[partitionEndpoint], - let region = endpoint.credentialScope?.region - else { - preconditionFailure("Found partition endpoint without a credential scope region") - } - partitionEndpoints[$0.partition] = (endpoint: partitionEndpoint, region: region) + guard let service = $0.services[self.serviceEndpointPrefix] else { return } + guard let partitionEndpoint = service.partitionEndpoint else { return } + guard let endpoint = service.endpoints[partitionEndpoint] else { + self.logger.error("Partition endpoint \(partitionEndpoint) for service \(self.serviceEndpointPrefix) in \($0.partitionName) does not exist") + return + } + guard let region = endpoint.credentialScope?.region else { + self.logger.error("Partition endpoint \(partitionEndpoint) for service \(self.serviceEndpointPrefix) in \($0.partitionName) has no credential scope region") + return } + partitionEndpoints[$0.partition] = (endpoint: partitionEndpoint, region: region) } return partitionEndpoints } diff --git a/Sources/SotoCodeGeneratorLib/SotoCodeGen.swift b/Sources/SotoCodeGeneratorLib/SotoCodeGen.swift index d6f6343..4c64cfc 100644 --- a/Sources/SotoCodeGeneratorLib/SotoCodeGen.swift +++ b/Sources/SotoCodeGeneratorLib/SotoCodeGen.swift @@ -14,6 +14,7 @@ import Foundation import HummingbirdMustache +import Logging import SotoSmithy import SotoSmithyAWS import SwiftFormat @@ -27,6 +28,7 @@ public protocol SotoCodeGenCommand { var swiftFormat: Bool { get } var htmlComments: Bool { get } var smithy: Bool { get } + var logLevel: String? { get } } public struct SotoCodeGen { @@ -53,10 +55,14 @@ public struct SotoCodeGen { let command: SotoCodeGenCommand let library: HBMustacheLibrary + let logger: Logging.Logger public init(command: SotoCodeGenCommand) throws { self.command = command self.library = try Templates.createLibrary() + var logger = Logging.Logger(label: "") + logger.logLevel = self.command.logLevel.map { Logging.Logger.Level(rawValue: $0) ?? .info } ?? .info + self.logger = logger Smithy.registerAWSTraits() Smithy.registerTraitTypes( SotoInputShapeTrait.self, @@ -83,12 +89,17 @@ public struct SotoCodeGen { DispatchQueue.global().async { defer { group.leave() } do { - let service = try AwsService(model.value, endpoints: endpoints, outputHTMLComments: command.htmlComments) + let service = try AwsService( + model.value, + endpoints: endpoints, + outputHTMLComments: command.htmlComments, + logger: self.logger + ) if self.command.output { try self.generateFiles(with: service) } } catch { - print("\(model.key): \(error)") + self.logger.error("\(model.key): \(error)") exit(1) } } @@ -96,8 +107,8 @@ public struct SotoCodeGen { group.wait() - print("Code Generation took \(Int(-startTime.timeIntervalSinceNow)) seconds") - print("Done.") + self.logger.info("Code Generation took \(Int(-startTime.timeIntervalSinceNow)) seconds") + self.logger.info("Done.") } func getModelFiles() -> [String] { @@ -170,13 +181,13 @@ public struct SotoCodeGen { if try self.format(api) .writeIfChanged(toFile: "\(basePath)\(service.serviceName)_API.swift") { - print("Wrote: \(service.serviceName)_API.swift") + self.logger.info("Wrote \(service.serviceName)_API.swift") } let apiAsync = self.library.render(apiContext, withTemplate: "api+async")! if self.command.output, try self.format(apiAsync).writeIfChanged( toFile: "\(basePath)/\(service.serviceName)_API+async.swift" ) { - print("Wrote: \(service.serviceName)_API+async.swift") + self.logger.info("Wrote \(service.serviceName)_API+async.swift") } let shapesContext = try service.generateShapesContext() @@ -184,7 +195,7 @@ public struct SotoCodeGen { if self.command.output, try self.format(shapes).writeIfChanged( toFile: "\(basePath)/\(service.serviceName)_Shapes.swift" ) { - print("Wrote: \(service.serviceName)_Shapes.swift") + self.logger.info("Wrote \(service.serviceName)_Shapes.swift") } let errorContext = try service.generateErrorContext() @@ -193,7 +204,7 @@ public struct SotoCodeGen { if self.command.output, try self.format(errors).writeIfChanged( toFile: "\(basePath)/\(service.serviceName)_Error.swift" ) { - print("Wrote: \(service.serviceName)_Error.swift") + self.logger.info("Wrote \(service.serviceName)_Error.swift") } } @@ -203,13 +214,13 @@ public struct SotoCodeGen { if self.command.output, try self.format(paginators).writeIfChanged( toFile: "\(basePath)/\(service.serviceName)_Paginator.swift" ) { - print("Wrote: \(service.serviceName)_Paginator.swift") + self.logger.info("Wrote \(service.serviceName)_Paginator.swift") } let paginatorsAsync = self.library.render(paginatorContext, withTemplate: "paginator+async")! if self.command.output, try self.format(paginatorsAsync).writeIfChanged( toFile: "\(basePath)/\(service.serviceName)_Paginator+async.swift" ) { - print("Wrote: \(service.serviceName)_Paginator+async.swift") + self.logger.info("Wrote \(service.serviceName)_Paginator+async.swift") } } @@ -219,16 +230,16 @@ public struct SotoCodeGen { if self.command.output, try self.format(waiters).writeIfChanged( toFile: "\(basePath)/\(service.serviceName)_Waiter.swift" ) { - print("Wrote: \(service.serviceName)_Waiter.swift") + self.logger.info("Wrote \(service.serviceName)_Waiter.swift") } let waitersAsync = self.library.render(waiterContexts, withTemplate: "waiter+async")! if self.command.output, try self.format(waitersAsync).writeIfChanged( toFile: "\(basePath)/\(service.serviceName)_Waiter+async.swift" ) { - print("Wrote: \(service.serviceName)_Waiter+async.swift") + self.logger.info("Wrote \(service.serviceName)_Waiter+async.swift") } } - // print("Succesfully Generated \(service.serviceName)") + self.logger.debug("Succesfully Generated \(service.serviceName)") } }