-
Notifications
You must be signed in to change notification settings - Fork 776
Centralize utilities for configuration loading and path parsing #1448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
katiewasnothere
merged 2 commits into
apple:main
from
noah-thor:user-config-utility-no-toml
May 8, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // Copyright © 2026 Apple Inc. and the container project authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import ContainerizationError | ||
| import Foundation | ||
| import SystemPackage | ||
| import TOML | ||
|
|
||
| public protocol Initable { | ||
| init() | ||
| } | ||
|
|
||
| public typealias LoadableConfiguration = Codable & Sendable & Initable | ||
|
|
||
| public enum ConfigurationLoader { | ||
| private static let configFilename = "runtime-config.toml" | ||
| private static let configDirectory = "config" | ||
| private static let READ_ONLY: Int = 0o444 | ||
| private static let READ_AND_WRITE: Int = 0o644 | ||
|
|
||
| /// Returns the canonical configuration file path under an appRoot base directory: | ||
| /// `<base>/config/runtime-config.toml`. | ||
| public static func configurationFile(in base: FilePath) -> FilePath { | ||
| base.appending(configDirectory).appending(configFilename) | ||
| } | ||
|
|
||
| /// Loads and decodes a TOML configuration file as type `T`. | ||
| /// | ||
| /// - Parameter configurationFile: Absolute path to the configuration file. | ||
| /// When `nil`, falls back to | ||
| /// `configurationFile(in: PathUtils.BaseConfigPath.appRoot.basePath())`. | ||
| /// - Returns: A decoded value of type `T`, or a default-initialized `T` if the | ||
| /// configuration file does not exist. | ||
| public static func load<T: LoadableConfiguration>(configurationFile: FilePath? = nil) throws -> T { | ||
| let path = configurationFile ?? Self.configurationFile(in: PathUtils.BaseConfigPath.appRoot.basePath()) | ||
| guard FileManager.default.fileExists(atPath: path.string) else { | ||
| return T() | ||
| } | ||
| do { | ||
| let data = try Data(contentsOf: URL(filePath: path.string)) | ||
| return try TOMLDecoder().decode(T.self, from: data) | ||
| } catch { | ||
| throw ContainerizationError( | ||
| .invalidArgument, | ||
| message: "failed to load configuration from '\(path)': \(error)" | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /// Copies a TOML configuration file into a read-only destination under an appRoot base. | ||
| /// | ||
| /// - Parameters: | ||
| /// - source: The file to copy. When `nil`, defaults to | ||
| /// `<home>/container/runtime-config.toml`. If the source does not exist, | ||
| /// this is a no-op. | ||
| /// - destination: Base directory under which the file is written at | ||
| /// `<destination>/config/runtime-config.toml`. When `nil`, falls back to | ||
| /// `PathUtils.BaseConfigPath.appRoot.basePath()`. The destination file is written | ||
| /// with `READ_ONLY` (`0o444`) permissions. | ||
| public static func copyConfigurationToReadOnly( | ||
| from source: FilePath? = nil, | ||
| to destination: FilePath? = nil | ||
| ) throws { | ||
| let source = | ||
| source | ||
| ?? PathUtils.BaseConfigPath.home.basePath() | ||
| .appending(configFilename) | ||
| let destinationFile = Self.configurationFile(in: destination ?? PathUtils.BaseConfigPath.appRoot.basePath()) | ||
| do { | ||
| let fm = FileManager.default | ||
| guard fm.fileExists(atPath: source.string) else { return } | ||
|
|
||
| let destDir = destinationFile.removingLastComponent() | ||
| try fm.createDirectory( | ||
| atPath: destDir.string, | ||
| withIntermediateDirectories: true | ||
| ) | ||
| if fm.fileExists(atPath: destinationFile.string) { | ||
| try fm.setAttributes( | ||
| [.posixPermissions: READ_AND_WRITE], | ||
| ofItemAtPath: destinationFile.string | ||
| ) | ||
| try fm.removeItem(at: URL(filePath: destinationFile.string)) | ||
| } | ||
| try fm.copyItem( | ||
| at: URL(filePath: source.string), | ||
| to: URL(filePath: destinationFile.string) | ||
| ) | ||
| try fm.setAttributes( | ||
| [.posixPermissions: READ_ONLY], | ||
| ofItemAtPath: destinationFile.string | ||
| ) | ||
| } catch { | ||
| throw ContainerizationError( | ||
| .invalidState, message: "Failed to copy user TOML to AppRoot `\(error)`") | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // Copyright © 2026 Apple Inc. and the container project authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import Foundation | ||
| import SystemPackage | ||
|
|
||
| public enum PathUtils { | ||
| public enum BaseConfigPath { | ||
| case home | ||
| case appRoot | ||
|
|
||
| public func basePath(env: [String: String] = ProcessInfo.processInfo.environment) -> FilePath { | ||
| switch self { | ||
| case .home: | ||
| let configHome: String | ||
| if let xdg = env["XDG_CONFIG_HOME"], !xdg.isEmpty { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need a follow up PR to scrub ContainerPersistence for string and URL path manipulation. I took care of this for the entity store type. e.g. we could start here with something like: |
||
| configHome = xdg | ||
| } else { | ||
| configHome = NSHomeDirectory() + "/.config" | ||
| } | ||
| return FilePath(configHome).appending("container") | ||
| case .appRoot: | ||
| if let envPath = env["CONTAINER_APP_ROOT"], !envPath.isEmpty { | ||
| return FilePath(envPath) | ||
| } | ||
| let appSupportURL = FileManager.default.urls( | ||
| for: .applicationSupportDirectory, | ||
| in: .userDomainMask | ||
| ).first!.appendingPathComponent("com.apple.container") | ||
| return FilePath(appSupportURL.path(percentEncoded: false)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit for the next commit: we can use type inference here and shorten the line