Skip to content

Commit 0f9068c

Browse files
committed
Extract version output logic so --version is consistent.
1 parent e616143 commit 0f9068c

File tree

8 files changed

+74
-63
lines changed

8 files changed

+74
-63
lines changed

Package.swift

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import PackageDescription
2222

2323
let releaseVersion = ProcessInfo.processInfo.environment["RELEASE_VERSION"] ?? "0.0.0"
2424
let gitCommit = ProcessInfo.processInfo.environment["GIT_COMMIT"] ?? "unspecified"
25-
let scVersion = "0.5.0"
2625
let builderShimVersion = "0.6.0"
26+
let scVersion = "0.5.0"
2727

2828
let package = Package(
2929
name: "container",
@@ -37,6 +37,7 @@ let package = Package(
3737
.library(name: "ContainerLog", targets: ["ContainerLog"]),
3838
.library(name: "ContainerPersistence", targets: ["ContainerPersistence"]),
3939
.library(name: "ContainerPlugin", targets: ["ContainerPlugin"]),
40+
.library(name: "ContainerVersion", targets: ["ContainerVersion"]),
4041
.library(name: "ContainerXPC", targets: ["ContainerXPC"]),
4142
.library(name: "SocketForwarder", targets: ["SocketForwarder"]),
4243
.library(name: "ContainerBuildReporting", targets: ["ContainerBuildReporting"]),
@@ -70,13 +71,13 @@ let package = Package(
7071
.product(name: "Containerization", package: "containerization"),
7172
.product(name: "ContainerizationOCI", package: "containerization"),
7273
.product(name: "ContainerizationOS", package: "containerization"),
73-
"CVersion",
74-
"TerminalProgress",
7574
"ContainerBuild",
7675
"ContainerClient",
76+
"ContainerLog",
7777
"ContainerPersistence",
7878
"ContainerPlugin",
79-
"ContainerLog",
79+
"ContainerVersion",
80+
"TerminalProgress",
8081
],
8182
path: "Sources/CLI"
8283
),
@@ -90,14 +91,14 @@ let package = Package(
9091
.product(name: "Containerization", package: "containerization"),
9192
.product(name: "ContainerizationExtras", package: "containerization"),
9293
.product(name: "ContainerizationOS", package: "containerization"),
93-
"CVersion",
94-
"DNSServer",
95-
"ContainerNetworkService",
96-
"ContainerSandboxService",
9794
"ContainerClient",
9895
"ContainerLog",
96+
"ContainerNetworkService",
9997
"ContainerPersistence",
10098
"ContainerPlugin",
99+
"ContainerSandboxService",
100+
"ContainerVersion",
101+
"DNSServer",
101102
],
102103
path: "Sources/APIServer"
103104
),
@@ -108,10 +109,10 @@ let package = Package(
108109
.product(name: "Logging", package: "swift-log"),
109110
.product(name: "GRPC", package: "grpc-swift"),
110111
.product(name: "Containerization", package: "containerization"),
111-
"CVersion",
112+
"ContainerLog",
112113
"ContainerNetworkService",
113114
"ContainerSandboxService",
114-
"ContainerLog",
115+
"ContainerVersion",
115116
"ContainerXPC",
116117
],
117118
path: "Sources/Helpers/RuntimeLinux"
@@ -140,9 +141,9 @@ let package = Package(
140141
.product(name: "ContainerizationExtras", package: "containerization"),
141142
.product(name: "ContainerizationIO", package: "containerization"),
142143
.product(name: "ContainerizationOS", package: "containerization"),
143-
"CVersion",
144-
"ContainerNetworkService",
145144
"ContainerLog",
145+
"ContainerNetworkService",
146+
"ContainerVersion",
146147
"ContainerXPC",
147148
],
148149
path: "Sources/Helpers/NetworkVmnet"
@@ -164,11 +165,11 @@ let package = Package(
164165
.product(name: "ArgumentParser", package: "swift-argument-parser"),
165166
.product(name: "Logging", package: "swift-log"),
166167
.product(name: "Containerization", package: "containerization"),
167-
"CVersion",
168+
"ContainerImagesService",
168169
"ContainerLog",
169170
"ContainerPlugin",
171+
"ContainerVersion",
170172
"ContainerXPC",
171-
"ContainerImagesService",
172173
],
173174
path: "Sources/Helpers/Images"
174175
),
@@ -414,6 +415,12 @@ let package = Package(
414415
],
415416
path: "Tests/CLITests"
416417
),
418+
.target(
419+
name: "ContainerVersion",
420+
dependencies: [
421+
"CVersion"
422+
],
423+
),
417424
.target(
418425
name: "CVersion",
419426
dependencies: [],

Sources/APIServer/APIServer.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
import ArgumentParser
18-
import CVersion
1918
import ContainerClient
2019
import ContainerLog
2120
import ContainerNetworkService
2221
import ContainerPlugin
22+
import ContainerVersion
2323
import ContainerXPC
2424
import ContainerizationError
2525
import ContainerizationExtras
@@ -37,7 +37,7 @@ struct APIServer: AsyncParsableCommand {
3737
static let configuration = CommandConfiguration(
3838
commandName: "container-apiserver",
3939
abstract: "Container management API server",
40-
version: releaseVersion()
40+
version: ReleaseVersion.singleLine(appName: "container-apiserver")
4141
)
4242

4343
@Flag(name: .long, help: "Enable debug logging")
@@ -47,10 +47,6 @@ struct APIServer: AsyncParsableCommand {
4747

4848
var installRoot = InstallRoot.url
4949

50-
static func releaseVersion() -> String {
51-
(Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? get_release_version().map { String(cString: $0) } ?? "0.0.0"
52-
}
53-
5450
func run() async throws {
5551
let commandName = Self.configuration.commandName ?? "container-apiserver"
5652
let log = setupLogger()

Sources/APIServer/HealthCheck/HealthCheckHarness.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import CVersion
1818
import ContainerClient
19+
import ContainerVersion
1920
import ContainerXPC
2021
import Containerization
2122
import Foundation
@@ -37,8 +38,8 @@ actor HealthCheckHarness {
3738
let reply = message.reply()
3839
reply.set(key: .appRoot, value: appRoot.absoluteString)
3940
reply.set(key: .installRoot, value: installRoot.absoluteString)
40-
reply.set(key: .apiServerVersion, value: APIServer.releaseVersion())
41-
reply.set(key: .apiServerCommit, value: get_git_commit().map { String(cString: $0) } ?? "unknown")
41+
reply.set(key: .apiServerVersion, value: ReleaseVersion.singleLine(appName: "container-apiserver"))
42+
reply.set(key: .apiServerCommit, value: get_git_commit().map { String(cString: $0) } ?? "unspecified")
4243
return reply
4344
}
4445
}

Sources/CLI/Application.swift

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
//
1818

1919
import ArgumentParser
20-
import CVersion
2120
import ContainerClient
2221
import ContainerLog
2322
import ContainerPlugin
23+
import ContainerVersion
2424
import ContainerizationError
2525
import ContainerizationOS
2626
import Foundation
@@ -48,7 +48,7 @@ struct Application: AsyncParsableCommand {
4848
static let configuration = CommandConfiguration(
4949
commandName: "container",
5050
abstract: "A container platform for macOS",
51-
version: releaseVersion(),
51+
version: ReleaseVersion.singleLine(appName: "container CLI"),
5252
subcommands: [
5353
DefaultCommand.self
5454
],
@@ -336,25 +336,4 @@ extension Application {
336336
throw posixErr
337337
}
338338
}
339-
340-
private static func releaseVersion() -> String {
341-
var versionDetails: [String: String] = ["build": "release"]
342-
#if DEBUG
343-
versionDetails["build"] = "debug"
344-
#endif
345-
let gitCommit = {
346-
let sha = get_git_commit().map { String(cString: $0) }
347-
guard let sha else {
348-
return "unspecified"
349-
}
350-
return String(sha.prefix(7))
351-
}()
352-
versionDetails["commit"] = gitCommit
353-
let extras: String = versionDetails.map { "\($0): \($1)" }.sorted().joined(separator: ", ")
354-
355-
let bundleVersion = (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String)
356-
let releaseVersion = bundleVersion ?? get_release_version().map { String(cString: $0) } ?? "0.0.0"
357-
358-
return "container CLI version \(releaseVersion) (\(extras))"
359-
}
360339
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===----------------------------------------------------------------------===//
2+
// Copyright © 2025 Apple Inc. and the container project authors. All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//===----------------------------------------------------------------------===//
16+
17+
import CVersion
18+
import Foundation
19+
20+
public struct ReleaseVersion {
21+
public static func singleLine(appName: String) -> String {
22+
var versionDetails: [String: String] = ["build": "release"]
23+
#if DEBUG
24+
versionDetails["build"] = "debug"
25+
#endif
26+
versionDetails["commit"] = gitCommit().map { String($0.prefix(7)) } ?? "unspecified"
27+
let extras: String = versionDetails.map { "\($0): \($1)" }.sorted().joined(separator: ", ")
28+
29+
return "\(appName) version \(version()) (\(extras))"
30+
}
31+
32+
public static func version() -> String {
33+
let bundleVersion = (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String)
34+
return bundleVersion ?? get_release_version().map { String(cString: $0) } ?? "0.0.0"
35+
}
36+
37+
public static func gitCommit() -> String? {
38+
get_git_commit().map { String(cString: $0) }
39+
}
40+
}

Sources/Helpers/Images/ImagesHelper.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
import ArgumentParser
18-
import CVersion
1918
import ContainerImagesService
2019
import ContainerImagesServiceClient
2120
import ContainerLog
2221
import ContainerPlugin
22+
import ContainerVersion
2323
import ContainerXPC
2424
import Containerization
2525
import Foundation
@@ -30,7 +30,7 @@ struct ImagesHelper: AsyncParsableCommand {
3030
static let configuration = CommandConfiguration(
3131
commandName: "container-core-images",
3232
abstract: "XPC service for managing OCI images",
33-
version: releaseVersion(),
33+
version: ReleaseVersion.singleLine(appName: "container-core-images"),
3434
subcommands: [
3535
Start.self
3636
]
@@ -129,8 +129,4 @@ extension ImagesHelper {
129129
return log
130130
}
131131
}
132-
133-
private static func releaseVersion() -> String {
134-
(Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? get_release_version().map { String(cString: $0) } ?? "0.0.0"
135-
}
136132
}

Sources/Helpers/NetworkVmnet/NetworkVmnetHelper.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
import ArgumentParser
18-
import CVersion
1918
import ContainerLog
2019
import ContainerNetworkService
20+
import ContainerVersion
2121
import ContainerXPC
2222
import ContainerizationExtras
2323
import Foundation
@@ -28,7 +28,7 @@ struct NetworkVmnetHelper: AsyncParsableCommand {
2828
static let configuration = CommandConfiguration(
2929
commandName: "container-network-vmnet",
3030
abstract: "XPC service for managing a vmnet network",
31-
version: releaseVersion(),
31+
version: ReleaseVersion.singleLine(appName: "container-network-vmnet"),
3232
subcommands: [
3333
Start.self
3434
]
@@ -112,8 +112,4 @@ extension NetworkVmnetHelper {
112112
return try ReservedVmnetNetwork(configuration: configuration, log: log)
113113
}
114114
}
115-
116-
private static func releaseVersion() -> String {
117-
(Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? get_release_version().map { String(cString: $0) } ?? "0.0.0"
118-
}
119115
}

Sources/Helpers/RuntimeLinux/RuntimeLinuxHelper.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
import ArgumentParser
18-
import CVersion
1918
import ContainerClient
2019
import ContainerLog
2120
import ContainerNetworkService
2221
import ContainerSandboxService
22+
import ContainerVersion
2323
import ContainerXPC
2424
import Containerization
2525
import ContainerizationError
@@ -34,7 +34,7 @@ struct RuntimeLinuxHelper: AsyncParsableCommand {
3434
static let configuration = CommandConfiguration(
3535
commandName: "container-runtime-linux",
3636
abstract: "XPC Service for managing a Linux sandbox",
37-
version: releaseVersion()
37+
version: ReleaseVersion.singleLine(appName: "container-runtime-linux")
3838
)
3939

4040
@Flag(name: .long, help: "Enable debug logging")
@@ -122,8 +122,4 @@ struct RuntimeLinuxHelper: AsyncParsableCommand {
122122
throw POSIXError(.init(rawValue: errno)!)
123123
}
124124
}
125-
126-
private static func releaseVersion() -> String {
127-
(Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? get_release_version().map { String(cString: $0) } ?? "0.0.0"
128-
}
129125
}

0 commit comments

Comments
 (0)