Skip to content

Commit 09b4602

Browse files
committed
Output JSON for config.
Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
1 parent fd2f2a2 commit 09b4602

3 files changed

Lines changed: 60 additions & 19 deletions

File tree

Scripts/mas

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ try (
9797
] |
9898
map(select(length > 0)) | if length > 0 then join("\n\n") else empty end
9999
) catch ("\u001B[4;31mError:\u001B[0m Invalid data from mas: \(.)\n" | halt_error(1))
100+
'
101+
} 4>&1 5>&2
102+
;;
103+
config)
104+
{
105+
{ exec "${mas}" "${@}" 3>&1 1>&4 2>&5 } | "${jq}" -r '
106+
try (
107+
(keys_unsorted | map(length) | max) as $max_key_length |
108+
to_entries[] |
109+
"\(.key) \("▁" * ($max_key_length - (.key | length) + 1)) \(.value)"
110+
) catch ("\u001B[4;31mError:\u001B[0m Invalid data from mas: \(.)\n" | halt_error(1))
100111
'
101112
} 4>&1 5>&2
102113
;;

Sources/mas/Commands/Config.swift

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
internal import ArgumentParser
99
private import Darwin
1010
private import Foundation
11+
private import JSON
12+
private import JSONAST
13+
private import JSONEncoding
1114

1215
extension MAS {
1316
/// Outputs mas config & related system info.
@@ -16,24 +19,30 @@ extension MAS {
1619
abstract: "Output mas config & related system info",
1720
)
1821

22+
@OptionGroup
23+
private var outputFormatOptionGroup: OutputFormatOptionGroup
24+
1925
func run() {
20-
printer.info(
21-
"""
22-
mas ▁▁▁▁ \(version)
23-
slice ▁▁ \(runningSliceArchitecture)
24-
slices ▁ \(supportedSliceArchitectures.joined(separator: " "))
25-
dist ▁▁▁ \(distribution)
26-
origin ▁ \(gitOrigin)
27-
rev ▁▁▁▁ \(gitRevision)
28-
swift ▁▁ \(swiftVersion)
29-
driver ▁ \(swiftDriverVersion)
30-
store ▁▁ \(appStoreRegion)
31-
region ▁ \(macRegion)
32-
macos ▁▁ \(macOSVersion)
33-
mac ▁▁▁▁ \(configStringValue("hw.product"))
34-
cpu ▁▁▁▁ \(configStringValue("machdep.cpu.brand_string"))
35-
arch ▁▁▁ \(configStringValue("hw.machine"))
36-
""",
26+
outputFormatOptionGroup.info(
27+
JSON.encode(
28+
KeyValuePairs(
29+
dictionaryLiteral:
30+
("mas", version),
31+
("slice", runningSliceArchitecture),
32+
("slices", supportedSliceArchitectures.joined(separator: " ")),
33+
("dist", distribution),
34+
("origin", gitOrigin),
35+
("rev", gitRevision),
36+
("swift", swiftVersion),
37+
("driver", swiftDriverVersion),
38+
("store", appStoreRegion),
39+
("region", macRegion),
40+
("macos", macOSVersion),
41+
("mac", configStringValue("hw.product")),
42+
("cpu", configStringValue("machdep.cpu.brand_string")),
43+
("arch", configStringValue("hw.machine")),
44+
),
45+
),
3746
)
3847
}
3948
}
@@ -78,8 +87,10 @@ private var supportedSliceArchitectures: [String] {
7887
?? [] // swiftformat:disable:this indent
7988
}
8089

81-
private var macOSVersion: Substring {
82-
ProcessInfo.processInfo.operatingSystemVersionString.dropFirst(8).replacing("Build ", with: "", maxReplacements: 1)
90+
private var macOSVersion: String {
91+
String(
92+
ProcessInfo.processInfo.operatingSystemVersionString.dropFirst(8).replacing("Build ", with: "", maxReplacements: 1),
93+
)
8394
}
8495

8596
private func configStringValue(_ name: String) -> String {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// KeyValuePairs+JSONObjectEncodable.swift
3+
// mas
4+
//
5+
// Copyright © 2026 mas-cli. All rights reserved.
6+
//
7+
8+
public import JSONAST
9+
public import JSONEncoding
10+
11+
extension KeyValuePairs: @retroactive JSONEncodable where Key: CustomStringConvertible, Value: JSONEncodable {}
12+
13+
extension KeyValuePairs: @retroactive JSONObjectEncodable where Key: CustomStringConvertible, Value: JSONEncodable {
14+
public func encode(to json: inout JSON.ObjectEncoder<JSON.Key>) {
15+
for (key, value) in self {
16+
json[JSON.Key(rawValue: String(describing: key))] = value
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)