Skip to content

Commit

Permalink
tart list: show if the VM is running or not (#456)
Browse files Browse the repository at this point in the history
* tart list: show if the VM is running or not

* Boolean "running" field instead of "state", similarly to "tart get"

* Re-use VMDirectory.running() in "tart get"
  • Loading branch information
edigaryev authored Mar 29, 2023
1 parent 33a51b7 commit e72fcd9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Sources/tart/Commands/Get.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ struct Get: AsyncParsableCommand {
let vmConfig = try VMConfig(fromURL: vmDir.configURL)
let diskSizeInGb = try vmDir.sizeGB()
let memorySizeInMb = vmConfig.memorySize / 1024 / 1024
let running = try PIDLock(lockURL: vmDir.configURL).pid() > 0

let info = VMInfo(CPU: vmConfig.cpuCount, Memory: memorySizeInMb, Disk: diskSizeInGb, Display: vmConfig.display.description, Running: running)
let info = VMInfo(CPU: vmConfig.cpuCount, Memory: memorySizeInMb, Disk: diskSizeInGb,
Display: vmConfig.display.description, Running: try vmDir.running())
print(format.renderSingle(info))
}
}
7 changes: 5 additions & 2 deletions Sources/tart/Commands/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fileprivate struct VMInfo: Encodable {
let Source: String
let Name: String
let Size: Int
let Running: Bool
}

struct List: AsyncParsableCommand {
Expand All @@ -32,17 +33,19 @@ struct List: AsyncParsableCommand {

func run() async throws {
var infos: [VMInfo] = []

if source == nil || source == "local" {
infos += sortedInfos(try VMStorageLocal().list().map { (name, vmDir) in
try VMInfo(Source: "local", Name: name, Size: vmDir.sizeGB())
try VMInfo(Source: "local", Name: name, Size: vmDir.sizeGB(), Running: vmDir.running())
})
}

if source == nil || source == "oci" {
infos += sortedInfos(try VMStorageOCI().list().map { (name, vmDir, _) in
try VMInfo(Source: "oci", Name: name, Size: vmDir.sizeGB())
try VMInfo(Source: "oci", Name: name, Size: vmDir.sizeGB(), Running: vmDir.running())
})
}

if (quiet) {
for info in infos {
print(info.Name)
Expand Down
4 changes: 4 additions & 0 deletions Sources/tart/VMDirectory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ struct VMDirectory: Prunable {
baseURL
}

func running() throws -> Bool {
try PIDLock(lockURL: configURL).pid() != 0
}

static func temporary() throws -> VMDirectory {
let tmpDir = try Config().tartTmpDir.appendingPathComponent(UUID().uuidString)
try FileManager.default.createDirectory(at: tmpDir, withIntermediateDirectories: false)
Expand Down

0 comments on commit e72fcd9

Please sign in to comment.