86 lines
3.0 KiB
Swift
86 lines
3.0 KiB
Swift
import ConfigurationClient
|
|
import Dependencies
|
|
import Foundation
|
|
|
|
// NOTE: We're not using the `Coders` client because we generally do not
|
|
// want the output to be `prettyPrinted` or anything, unless we're running
|
|
// tests, so we use a supplied json encoder.
|
|
|
|
// TODO: Remove.
|
|
func createJSONData(
|
|
_ options: CliClient.GenerateJsonOptions,
|
|
logging loggingOptions: CliClient.LoggingOptions,
|
|
encoder: JSONEncoder = .init()
|
|
) async throws -> Data {
|
|
try await CliClient.withLogger(loggingOptions) {
|
|
@Dependency(\.logger) var logger
|
|
@Dependency(\.configurationClient) var configurationClient
|
|
|
|
let configuration = try await configurationClient.findAndLoad()
|
|
|
|
let templateDir = options.templateDirectory ?? configuration.template.directory
|
|
let templateRepo = options.templateRepo ?? configuration.template.url
|
|
let version = options.version ?? configuration.template.version
|
|
|
|
logger.debug("""
|
|
(\(options.useLocalTemplateDirectory), \(String(describing: templateDir)), \(String(describing: templateRepo)))
|
|
""")
|
|
|
|
switch (options.useLocalTemplateDirectory, templateDir, templateRepo) {
|
|
case (true, .none, _):
|
|
// User supplied they wanted to use a local template directory, but we could not find
|
|
// the path set from command line or in configuration.
|
|
throw CliClientError.templateDirectoryNotFound
|
|
case let (false, _, .some(repo)):
|
|
// User did not supply they wanted to use a local template directory, and we found a repo url that was
|
|
// either set by the command line or found in the configuration.
|
|
logger.debug("Using repo.")
|
|
return try encoder.encode(TemplateRepo(repo: repo, version: version))
|
|
case let (true, .some(templateDir), _):
|
|
// User supplied they wanted to use a local template directory, and we found the template directory
|
|
// either set by the command line or in the configuration.
|
|
logger.debug("Using template directory.")
|
|
return try encoder.encode(TemplateDirJson(path: templateDir))
|
|
case let (false, .some(templateDir), _):
|
|
// User supplied they did not wanted to use a local template directory, and we found the template directory
|
|
// either set by the command line or in the configuration, and no repo was found / handled previously.
|
|
logger.debug("Using template directory.")
|
|
return try encoder.encode(TemplateDirJson(path: templateDir))
|
|
case (_, .none, .none):
|
|
// We could not find a repo or template directory.
|
|
throw CliClientError.templateDirectoryOrRepoNotSpecified
|
|
}
|
|
}
|
|
}
|
|
|
|
private struct TemplateDirJson: Encodable {
|
|
|
|
let template: Template
|
|
|
|
init(path: String) {
|
|
self.template = .init(path: path)
|
|
}
|
|
|
|
struct Template: Encodable {
|
|
let path: String
|
|
}
|
|
}
|
|
|
|
private struct TemplateRepo: Encodable {
|
|
|
|
let template: Template
|
|
|
|
init(repo: String, version: String?) {
|
|
self.template = .init(repo: .init(url: repo, version: version ?? "main"))
|
|
}
|
|
|
|
struct Template: Encodable {
|
|
let repo: Repo
|
|
}
|
|
|
|
struct Repo: Encodable {
|
|
let url: String
|
|
let version: String
|
|
}
|
|
}
|