Skip to content

Commit 94e28eb

Browse files
committed
Simplify parameter merge
1 parent 4fb3564 commit 94e28eb

File tree

2 files changed

+57
-55
lines changed

2 files changed

+57
-55
lines changed

Sources/xcodeinstall/CLI-driver/CLIMain.swift

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -79,74 +79,44 @@ struct MainCommand: AsyncParsableCommand {
7979
) async throws -> XCodeInstall {
8080

8181
var logger = Logger(label: "xcodeinstall")
82-
if verbose {
83-
logger.logLevel = .debug
84-
} else {
85-
logger.logLevel = .error
86-
}
82+
logger.logLevel = verbose ? .debug : .error
8783

8884
if let deps {
8985
return await XCodeInstall(log: logger, deps: deps)
9086
}
9187

92-
// Load saved config
88+
// Resolve config: merge CLI args with saved settings, display info, persist
9389
let baseDirectory = FileManager.default.homeDirectoryForCurrentUser
9490
.appendingPathComponent(".xcodeinstall")
9591
let configHandler = await ConfigHandler(log: logger, baseDirectory: baseDirectory)
96-
let savedConfig = await configHandler.loadConfig()
97-
98-
// Merge CLI args with saved config (CLI takes precedence)
99-
let effectiveRegion = region ?? savedConfig?.secretManagerRegion
100-
let effectiveProfile = profileName ?? savedConfig?.profileName
101-
102-
// Display info message for loaded (non-overridden) settings
10392
let display = NooraDisplay()
104-
if effectiveRegion != nil || effectiveProfile != nil {
105-
var savedParts: [String] = []
106-
if let r = effectiveRegion, region == nil {
107-
savedParts.append("-s \(r)")
108-
}
109-
if let p = effectiveProfile, profileName == nil {
110-
savedParts.append("-p \(p)")
111-
}
112-
if !savedParts.isEmpty {
113-
await display.display(
114-
"Using saved settings: \(savedParts.joined(separator: " "))",
115-
style: .info
116-
)
117-
}
118-
}
119-
120-
// Save config if CLI args provided (merge with existing)
121-
if region != nil || profileName != nil {
122-
let newConfig = PersistentConfig(
123-
secretManagerRegion: region ?? savedConfig?.secretManagerRegion,
124-
profileName: profileName ?? savedConfig?.profileName
125-
)
126-
try? await configHandler.saveConfig(newConfig)
127-
logger.debug("Saved config")
128-
}
93+
let resolved = try await configHandler.resolvedConfig(
94+
cliRegion: region,
95+
cliProfile: profileName,
96+
display: display
97+
)
12998

99+
// Wire dependencies using resolved values
130100
let fileHandler = await FileHandler(log: logger)
131101
let urlSession = URLSession.shared
132102

133-
var secrets: SecretsHandlerProtocol
134-
var authenticator: AppleAuthenticatorProtocol
135-
var downloader: AppleDownloaderProtocol
136-
137-
if let effectiveRegion {
138-
let awsSecrets = try await SecretsStorageAWS(
103+
let secrets: SecretsHandlerProtocol
104+
if let effectiveRegion = resolved.secretManagerRegion {
105+
secrets = try await SecretsStorageAWS(
139106
region: effectiveRegion,
140-
profileName: effectiveProfile,
107+
profileName: resolved.profileName,
141108
log: logger
142109
)
143-
secrets = awsSecrets
144110
} else {
145111
secrets = await SecretsStorageFile(log: logger)
146112
}
147113

148-
authenticator = await AppleAuthenticator(secrets: secrets, urlSession: urlSession, log: logger)
149-
downloader = await AppleDownloader(
114+
let authenticator: AppleAuthenticatorProtocol = await AppleAuthenticator(
115+
secrets: secrets,
116+
urlSession: urlSession,
117+
log: logger
118+
)
119+
let downloader: AppleDownloaderProtocol = await AppleDownloader(
150120
secrets: secrets,
151121
urlSession: urlSession,
152122
fileHandler: fileHandler,

Sources/xcodeinstall/Utilities/ConfigHandler.swift

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ protocol ConfigHandlerProtocol: Sendable {
1818
func saveConfig(_ config: PersistentConfig) throws
1919
func loadConfig() -> PersistentConfig?
2020
nonisolated func configPath() -> URL
21+
func resolvedConfig(cliRegion: String?, cliProfile: String?, display: DisplayProtocol) async throws -> PersistentConfig
2122
}
2223

2324
// Config data model
@@ -41,7 +42,6 @@ struct ConfigHandler: ConfigHandlerProtocol {
4142
}
4243

4344
func saveConfig(_ config: PersistentConfig) throws {
44-
// Create directory if needed
4545
let fm = FileManager.default
4646
if !fm.fileExists(atPath: baseDirectory.path) {
4747
do {
@@ -51,23 +51,17 @@ struct ConfigHandler: ConfigHandlerProtocol {
5151
throw error
5252
}
5353
}
54-
55-
// Encode and save
5654
let data = try JSONEncoder().encode(config)
5755
try data.write(to: configPath())
5856
log.debug("Saved config to \(configPath().path)")
5957
}
6058

6159
func loadConfig() -> PersistentConfig? {
6260
let path = configPath()
63-
64-
// Return nil if file doesn't exist (not an error)
6561
guard FileManager.default.fileExists(atPath: path.path) else {
6662
log.debug("No config file found at \(path.path)")
6763
return nil
6864
}
69-
70-
// Try to load and decode
7165
do {
7266
let data = try Data(contentsOf: path)
7367
let config = try JSONDecoder().decode(PersistentConfig.self, from: data)
@@ -78,4 +72,42 @@ struct ConfigHandler: ConfigHandlerProtocol {
7872
return nil
7973
}
8074
}
75+
76+
/// Merges CLI arguments with saved config, displays an info message for
77+
/// values that were loaded from disk (not provided on the CLI), and
78+
/// saves back when CLI arguments were provided.
79+
/// Returns the effective config to use for this invocation.
80+
func resolvedConfig(
81+
cliRegion: String?,
82+
cliProfile: String?,
83+
display: DisplayProtocol
84+
) async throws -> PersistentConfig {
85+
let saved = loadConfig()
86+
87+
let effectiveRegion = cliRegion ?? saved?.secretManagerRegion
88+
let effectiveProfile = cliProfile ?? saved?.profileName
89+
90+
// Show info message for values coming from saved config
91+
var savedParts: [String] = []
92+
if let r = effectiveRegion, cliRegion == nil { savedParts.append("-s \(r)") }
93+
if let p = effectiveProfile, cliProfile == nil { savedParts.append("-p \(p)") }
94+
if !savedParts.isEmpty {
95+
display.display(
96+
"Using saved settings: \(savedParts.joined(separator: " "))",
97+
style: .info
98+
)
99+
}
100+
101+
// Persist when CLI provided new values, merging with existing
102+
if cliRegion != nil || cliProfile != nil {
103+
let updated = PersistentConfig(
104+
secretManagerRegion: effectiveRegion,
105+
profileName: effectiveProfile
106+
)
107+
try? saveConfig(updated)
108+
log.debug("Saved config")
109+
}
110+
111+
return PersistentConfig(secretManagerRegion: effectiveRegion, profileName: effectiveProfile)
112+
}
81113
}

0 commit comments

Comments
 (0)