|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// Copyright © 2025 Apple Inc. and the container project authors. |
| 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 ArgumentParser |
| 18 | +import ContainerClient |
| 19 | +import ContainerizationError |
| 20 | +import Foundation |
| 21 | + |
| 22 | +extension Application { |
| 23 | + public struct SystemDF: AsyncParsableCommand { |
| 24 | + public static let configuration = CommandConfiguration( |
| 25 | + commandName: "df", |
| 26 | + abstract: "Show disk usage for images, containers, and volumes" |
| 27 | + ) |
| 28 | + |
| 29 | + @Option(name: .long, help: "Format of the output") |
| 30 | + var format: ListFormat = .table |
| 31 | + |
| 32 | + @OptionGroup |
| 33 | + var global: Flags.Global |
| 34 | + |
| 35 | + public init() {} |
| 36 | + |
| 37 | + public func run() async throws { |
| 38 | + let stats = try await ClientDiskUsage.get() |
| 39 | + |
| 40 | + if format == .json { |
| 41 | + let encoder = JSONEncoder() |
| 42 | + encoder.outputFormatting = [.prettyPrinted, .sortedKeys] |
| 43 | + let data = try encoder.encode(stats) |
| 44 | + guard let jsonString = String(data: data, encoding: .utf8) else { |
| 45 | + throw ContainerizationError( |
| 46 | + .internalError, |
| 47 | + message: "Failed to encode JSON output" |
| 48 | + ) |
| 49 | + } |
| 50 | + print(jsonString) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + printTable(stats: stats) |
| 55 | + } |
| 56 | + |
| 57 | + private func printTable(stats: DiskUsageStats) { |
| 58 | + var rows: [[String]] = [] |
| 59 | + |
| 60 | + // Header row |
| 61 | + rows.append(["TYPE", "TOTAL", "ACTIVE", "SIZE", "RECLAIMABLE"]) |
| 62 | + |
| 63 | + // Images row |
| 64 | + rows.append([ |
| 65 | + "Images", |
| 66 | + "\(stats.images.total)", |
| 67 | + "\(stats.images.active)", |
| 68 | + formatSize(stats.images.sizeInBytes), |
| 69 | + formatReclaimable(stats.images.reclaimable, total: stats.images.sizeInBytes), |
| 70 | + ]) |
| 71 | + |
| 72 | + // Containers row |
| 73 | + rows.append([ |
| 74 | + "Containers", |
| 75 | + "\(stats.containers.total)", |
| 76 | + "\(stats.containers.active)", |
| 77 | + formatSize(stats.containers.sizeInBytes), |
| 78 | + formatReclaimable(stats.containers.reclaimable, total: stats.containers.sizeInBytes), |
| 79 | + ]) |
| 80 | + |
| 81 | + // Volumes row |
| 82 | + rows.append([ |
| 83 | + "Local Volumes", |
| 84 | + "\(stats.volumes.total)", |
| 85 | + "\(stats.volumes.active)", |
| 86 | + formatSize(stats.volumes.sizeInBytes), |
| 87 | + formatReclaimable(stats.volumes.reclaimable, total: stats.volumes.sizeInBytes), |
| 88 | + ]) |
| 89 | + |
| 90 | + let tableFormatter = TableOutput(rows: rows) |
| 91 | + print(tableFormatter.format()) |
| 92 | + } |
| 93 | + |
| 94 | + private func formatSize(_ bytes: UInt64) -> String { |
| 95 | + if bytes == 0 { |
| 96 | + return "0 B" |
| 97 | + } |
| 98 | + let formatter = ByteCountFormatter() |
| 99 | + formatter.countStyle = .file |
| 100 | + return formatter.string(fromByteCount: Int64(bytes)) |
| 101 | + } |
| 102 | + |
| 103 | + private func formatReclaimable(_ reclaimable: UInt64, total: UInt64) -> String { |
| 104 | + let sizeStr = formatSize(reclaimable) |
| 105 | + |
| 106 | + if total == 0 { |
| 107 | + return "\(sizeStr) (0%)" |
| 108 | + } |
| 109 | + |
| 110 | + // Cap at 100% in case reclaimable > total (shouldn't happen but be defensive) |
| 111 | + let percentage = min(100, Int(round(Double(reclaimable) / Double(total) * 100.0))) |
| 112 | + return "\(sizeStr) (\(percentage)%)" |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments