Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Sources/ContainerPlugin/PluginConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

//
import Foundation
import SystemPackage
import TOML

/// PluginConfig details all of the fields to describe and register a plugin.
Expand Down Expand Up @@ -109,17 +110,17 @@ extension PluginConfig {
extension PluginConfig {
/// Initialize from a config file, selecting the decoder based on file extension.
/// Supports `.toml` (via TOMLDecoder) and `.json` (via JSONDecoder).
public init?(configURL: URL) throws {
public init?(configPath: FilePath) throws {
let fm = FileManager.default
if !fm.fileExists(atPath: configURL.path) {
if !fm.fileExists(atPath: configPath.string) {
return nil
}

guard let data = fm.contents(atPath: configURL.path) else {
guard let data = fm.contents(atPath: configPath.string) else {
return nil
}

switch configURL.pathExtension {
switch configPath.extension {
case "toml":
guard let content = String(data: data, encoding: .utf8) else {
return nil
Expand Down
15 changes: 8 additions & 7 deletions Sources/ContainerPlugin/PluginFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import Foundation
import Logging
import SystemPackage

/// Describes the configuration and binary file locations for a plugin.
public protocol PluginFactory: Sendable {
Expand All @@ -35,8 +36,8 @@ public struct DefaultPluginFactory: PluginFactory {
self.logger = logger
}

/// Returns the URL of the first config file found in `directory`, preferring TOML over JSON.
static func findConfigURL(in directory: URL, logger: Logger) -> URL? {
/// Returns the path of the first config file found in `directory`, preferring TOML over JSON.
static func findConfigPath(in directory: URL, logger: Logger) -> FilePath? {
let fm = FileManager.default
for filename in configFilenames {
let url = directory.appending(path: filename)
Expand All @@ -47,7 +48,7 @@ public struct DefaultPluginFactory: PluginFactory {
metadata: ["path": "\(url.path)"]
)
}
return url
return FilePath(url.path)
}
}
return nil
Expand All @@ -56,11 +57,11 @@ public struct DefaultPluginFactory: PluginFactory {
public func create(installURL: URL) throws -> Plugin? {
let fm = FileManager.default

guard let configURL = Self.findConfigURL(in: installURL, logger: logger) else {
guard let configPath = Self.findConfigPath(in: installURL, logger: logger) else {
return nil
}

guard let config = try PluginConfig(configURL: configURL) else {
guard let config = try PluginConfig(configPath: configPath) else {
return nil
}

Expand Down Expand Up @@ -99,11 +100,11 @@ public struct AppBundlePluginFactory: PluginFactory {
.appending(path: "Contents")
.appending(path: "Resources")

guard let configURL = DefaultPluginFactory.findConfigURL(in: contentResources, logger: logger) else {
guard let configPath = DefaultPluginFactory.findConfigPath(in: contentResources, logger: logger) else {
return nil
}

guard let config = try PluginConfig(configURL: configURL) else {
guard let config = try PluginConfig(configPath: configPath) else {
return nil
}

Expand Down
13 changes: 9 additions & 4 deletions Tests/ContainerPluginTests/PluginConfigTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//===----------------------------------------------------------------------===//

import Foundation
import SystemPackage
import Testing

@testable import ContainerPlugin
Expand All @@ -35,7 +36,8 @@ struct PluginConfigTest {
author = "Apple"
"""
try configToml.write(to: configURL, atomically: true, encoding: .utf8)
let config = try #require(try PluginConfig(configURL: configURL))
let configPath = FilePath(configURL.path)
let config = try #require(try PluginConfig(configPath: configPath))

#expect(config.isCLI)
#expect(config.abstract == "Default network management service")
Expand Down Expand Up @@ -67,7 +69,8 @@ struct PluginConfigTest {
description = "foo"
"""
try configToml.write(to: configURL, atomically: true, encoding: .utf8)
let config = try #require(try PluginConfig(configURL: configURL))
let configPath = FilePath(configURL.path)
let config = try #require(try PluginConfig(configPath: configPath))

#expect(!config.isCLI)
#expect(config.abstract == "Default network management service")
Expand Down Expand Up @@ -97,8 +100,9 @@ struct PluginConfigTest {
[invalid
"""
try malformedToml.write(to: configURL, atomically: true, encoding: .utf8)
let configPath = FilePath(configURL.path)
#expect(throws: (any Error).self) {
try PluginConfig(configURL: configURL)
try PluginConfig(configPath: configPath)
}
}

Expand All @@ -117,7 +121,8 @@ struct PluginConfigTest {
author: "Apple"
"""
try content.write(to: configURL, atomically: true, encoding: .utf8)
let config = try PluginConfig(configURL: configURL)
let configPath = FilePath(configURL.path)
let config = try PluginConfig(configPath: configPath)
#expect(config == nil)
}
}