-
Notifications
You must be signed in to change notification settings - Fork 745
Expand file tree
/
Copy pathContainerList.swift
More file actions
113 lines (96 loc) · 3.79 KB
/
ContainerList.swift
File metadata and controls
113 lines (96 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//===----------------------------------------------------------------------===//
// Copyright © 2025-2026 Apple Inc. and the container project authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
import ArgumentParser
import ContainerAPIClient
import ContainerResource
import ContainerizationExtras
import Foundation
import SwiftProtobuf
extension Application {
public struct ContainerList: AsyncLoggableCommand {
public static let configuration = CommandConfiguration(
commandName: "list",
abstract: "List running containers",
aliases: ["ls"])
@Flag(name: .shortAndLong, help: "Include containers that are not running")
var all = false
@Option(name: .long, help: "Format of the output")
var format: ListFormat = .table
@Flag(name: .shortAndLong, help: "Only output the container ID")
var quiet = false
@OptionGroup
public var logOptions: Flags.Logging
public init() {}
public func run() async throws {
let client = ContainerClient()
let filters = self.all ? ContainerListFilters.all : ContainerListFilters(status: .running)
let containers = try await client.list(filters: filters)
try printContainers(containers: containers, format: format)
}
private func createHeader() -> [[String]] {
[["ID", "IMAGE", "OS", "ARCH", "STATE", "ADDR", "CPUS", "MEMORY", "STARTED"]]
}
private func printContainers(containers: [ContainerSnapshot], format: ListFormat) throws {
if format == .json {
let printables = containers.map {
PrintableContainer($0)
}
let data = try JSONEncoder().encode(printables)
print(String(decoding: data, as: UTF8.self))
return
}
if self.quiet {
containers.forEach {
print($0.id)
}
return
}
var rows = createHeader()
for container in containers {
rows.append(container.asRow)
}
let formatter = TableOutput(rows: rows)
print(formatter.format())
}
}
}
extension ContainerSnapshot {
fileprivate var asRow: [String] {
[
self.id,
self.configuration.image.reference,
self.platform.os,
self.platform.architecture,
self.status.rawValue,
self.networks.compactMap { $0.ipv4Address.description }.joined(separator: ","),
"\(self.configuration.resources.cpus)",
"\(self.configuration.resources.memoryInBytes / (1024 * 1024)) MB",
self.startedDate.map { ISO8601DateFormatter().string(from: $0) } ?? "",
]
}
}
struct PrintableContainer: Codable {
let status: ContainerStatus
let configuration: ContainerConfiguration
let networks: [Attachment]
let startedDate: Date?
init(_ container: ContainerSnapshot) {
self.status = container.status
self.configuration = container.configuration
self.networks = container.networks
self.startedDate = container.startedDate
}
}