Skip to content

Commit 057605d

Browse files
committed
added argument-parser and a human-readable flag
1 parent 5fbb1e1 commit 057605d

File tree

3 files changed

+74
-14
lines changed

3 files changed

+74
-14
lines changed

diskspace.xcodeproj/project.pbxproj

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
C6F43F032731718900747000 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6F43F022731718900747000 /* main.swift */; };
11+
C6F43F0B2731762C00747000 /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = C6F43F0A2731762C00747000 /* ArgumentParser */; };
1112
/* End PBXBuildFile section */
1213

1314
/* Begin PBXCopyFilesBuildPhase section */
@@ -32,6 +33,7 @@
3233
isa = PBXFrameworksBuildPhase;
3334
buildActionMask = 2147483647;
3435
files = (
36+
C6F43F0B2731762C00747000 /* ArgumentParser in Frameworks */,
3537
);
3638
runOnlyForDeploymentPostprocessing = 0;
3739
};
@@ -78,6 +80,9 @@
7880
dependencies = (
7981
);
8082
name = diskspace;
83+
packageProductDependencies = (
84+
C6F43F0A2731762C00747000 /* ArgumentParser */,
85+
);
8186
productName = diskspace;
8287
productReference = C6F43EFF2731718900747000 /* diskspace */;
8388
productType = "com.apple.product-type.tool";
@@ -106,6 +111,9 @@
106111
Base,
107112
);
108113
mainGroup = C6F43EF62731718900747000;
114+
packageReferences = (
115+
C6F43F092731762C00747000 /* XCRemoteSwiftPackageReference "swift-argument-parser" */,
116+
);
109117
productRefGroup = C6F43F002731718900747000 /* Products */;
110118
projectDirPath = "";
111119
projectRoot = "";
@@ -286,6 +294,25 @@
286294
defaultConfigurationName = Release;
287295
};
288296
/* End XCConfigurationList section */
297+
298+
/* Begin XCRemoteSwiftPackageReference section */
299+
C6F43F092731762C00747000 /* XCRemoteSwiftPackageReference "swift-argument-parser" */ = {
300+
isa = XCRemoteSwiftPackageReference;
301+
repositoryURL = "https://github.com/apple/swift-argument-parser.git";
302+
requirement = {
303+
kind = upToNextMajorVersion;
304+
minimumVersion = 1.0.0;
305+
};
306+
};
307+
/* End XCRemoteSwiftPackageReference section */
308+
309+
/* Begin XCSwiftPackageProductDependency section */
310+
C6F43F0A2731762C00747000 /* ArgumentParser */ = {
311+
isa = XCSwiftPackageProductDependency;
312+
package = C6F43F092731762C00747000 /* XCRemoteSwiftPackageReference "swift-argument-parser" */;
313+
productName = ArgumentParser;
314+
};
315+
/* End XCSwiftPackageProductDependency section */
289316
};
290317
rootObject = C6F43EF72731718900747000 /* Project object */;
291318
}

diskspace.xcodeproj/xcuserdata/armin.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,13 @@
1010
<integer>0</integer>
1111
</dict>
1212
</dict>
13+
<key>SuppressBuildableAutocreation</key>
14+
<dict>
15+
<key>C6F43EFE2731718900747000</key>
16+
<dict>
17+
<key>primary</key>
18+
<true/>
19+
</dict>
20+
</dict>
1321
</dict>
1422
</plist>

diskspace/main.swift

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,47 @@
66
//
77

88
import Foundation
9+
import ArgumentParser
910

10-
let fileURL = URL(fileURLWithPath:"/")
11-
do {
12-
let values = try fileURL.resourceValues(forKeys: [.volumeAvailableCapacityKey,.volumeAvailableCapacityForImportantUsageKey, .volumeAvailableCapacityForOpportunisticUsageKey, .volumeTotalCapacityKey])
13-
if let importantCapacity = values.volumeAvailableCapacity {
14-
print("Available: \(importantCapacity)")
15-
}
16-
if let importantCapacity = values.volumeAvailableCapacityForImportantUsage {
17-
print("Important: \(importantCapacity)")
11+
// Code based on sample from here:
12+
// https://developer.apple.com/documentation/foundation/urlresourcekey/checking_volume_storage_capacity
13+
14+
struct DiskSpace : ParsableCommand {
15+
@Flag var humanReadable: Bool = false
16+
17+
18+
func format(_ int: Int) -> String {
19+
return format(Int64(int))
1820
}
19-
if let opportunisticCapacity = values.volumeAvailableCapacityForOpportunisticUsage {
20-
print("Opportunistic: \(opportunisticCapacity)")
21+
22+
func format(_ int: Int64) -> String {
23+
if humanReadable {
24+
return ByteCountFormatter().string(fromByteCount: Int64(int))
25+
} else {
26+
return String(int)
27+
}
2128
}
22-
if let totalCapacity = values.volumeTotalCapacity {
23-
print("Total: \(totalCapacity)")
29+
30+
func run() {
31+
let systemVolume = URL(fileURLWithPath:"/")
32+
do {
33+
let values = try systemVolume.resourceValues(forKeys: [.volumeAvailableCapacityKey,.volumeAvailableCapacityForImportantUsageKey, .volumeAvailableCapacityForOpportunisticUsageKey, .volumeTotalCapacityKey])
34+
if let totalCapacity = values.volumeAvailableCapacity {
35+
print("Available: \(format(totalCapacity))")
36+
}
37+
if let importantCapacity = values.volumeAvailableCapacityForImportantUsage {
38+
print("Important: \(format(importantCapacity))")
39+
}
40+
if let opportunisticCapacity = values.volumeAvailableCapacityForOpportunisticUsage {
41+
print("Opportunistic: \(format(opportunisticCapacity))")
42+
}
43+
if let totalCapacity = values.volumeTotalCapacity {
44+
print("Total: \(format(totalCapacity))")
45+
}
46+
} catch {
47+
print("Error retrieving capacity: \(error.localizedDescription)")
48+
}
2449
}
25-
} catch {
26-
print("Error retrieving capacity: \(error.localizedDescription)")
2750
}
51+
52+
DiskSpace.main()

0 commit comments

Comments
 (0)