From 842052f5bdca8cf29cc4afd99bdb537f8d88b7cb Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Tue, 20 Feb 2024 00:19:24 +0400 Subject: [PATCH] tart pull: experimental --json-digest option --- Sources/tart/Commands/Pull.swift | 9 ++++++++- Sources/tart/Logging/Logger.swift | 19 +++++++++++-------- Sources/tart/VMStorageOCI.swift | 5 ++++- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Sources/tart/Commands/Pull.swift b/Sources/tart/Commands/Pull.swift index 489439f..0735828 100644 --- a/Sources/tart/Commands/Pull.swift +++ b/Sources/tart/Commands/Pull.swift @@ -23,6 +23,9 @@ struct Pull: AsyncParsableCommand { @Option(help: "network concurrency to use when pulling a remote VM from the OCI-compatible registry") var concurrency: UInt = 4 + @Flag(help: .hidden) + var jsonDigest: Bool = false + func validate() throws { if concurrency < 1 { throw ValidationError("network concurrency cannot be less than 1") @@ -30,10 +33,14 @@ struct Pull: AsyncParsableCommand { } func run() async throws { + if jsonDigest { + jsonLogger = SimpleConsoleLogger() + } + // Be more liberal when accepting local image as argument, // see https://github.com/cirruslabs/tart/issues/36 if VMStorageLocal().exists(remoteName) { - print("\"\(remoteName)\" is a local image, nothing to pull here!") + defaultLogger.appendNewLine("\"\(remoteName)\" is a local image, nothing to pull here!") return } diff --git a/Sources/tart/Logging/Logger.swift b/Sources/tart/Logging/Logger.swift index 5ac3138..ef5608a 100644 --- a/Sources/tart/Logging/Logger.swift +++ b/Sources/tart/Logging/Logger.swift @@ -12,16 +12,13 @@ var defaultLogger: Logger = { return InteractiveConsoleLogger() } }() +var jsonLogger: Logger = NopLogger() public class InteractiveConsoleLogger: Logger { private let eraseCursorDown = "\u{001B}[J" // clear entire line private let moveUp = "\u{001B}[1A" // move one line up private let moveBeginningOfLine = "\r" // - public init() { - - } - public func appendNewLine(_ line: String) { print(line, terminator: "\n") } @@ -32,10 +29,6 @@ public class InteractiveConsoleLogger: Logger { } public class SimpleConsoleLogger: Logger { - public init() { - - } - public func appendNewLine(_ line: String) { print(line, terminator: "\n") } @@ -44,3 +37,13 @@ public class SimpleConsoleLogger: Logger { print(line, terminator: "\n") } } + +public class NopLogger: Logger { + public func appendNewLine(_ line: String) { + // do nothing + } + + public func updateLastLine(_ line: String) { + // do nothing + } +} diff --git a/Sources/tart/VMStorageOCI.swift b/Sources/tart/VMStorageOCI.swift index a2e5831..0cce9bf 100644 --- a/Sources/tart/VMStorageOCI.swift +++ b/Sources/tart/VMStorageOCI.swift @@ -147,6 +147,7 @@ class VMStorageOCI: PrunableStorage { if exists(name) && exists(digestName) && linked(from: name, to: digestName) { // optimistically check if we need to do anything at all before locking defaultLogger.appendNewLine("\(digestName) image is already cached and linked!") + jsonLogger.appendNewLine("{\"digest\": \"\(digestName)\"}") return } @@ -159,7 +160,7 @@ class VMStorageOCI: PrunableStorage { let sucessfullyLocked = try lock.trylock() if !sucessfullyLocked { - print("waiting for lock...") + defaultLogger.appendNewLine("waiting for lock...") try lock.lock() } defer { try! lock.unlock() } @@ -199,6 +200,8 @@ class VMStorageOCI: PrunableStorage { defaultLogger.appendNewLine("\(digestName) image is already cached! creating a symlink...") } + jsonLogger.appendNewLine("{\"digest\": \"\(digestName)\"}") + if name != digestName { // Create new or overwrite the old symbolic link try link(from: name, to: digestName)