Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce "tart fqn" command #735

Merged
merged 3 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Sources/tart/Commands/Pull.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@ 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")
}
}

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
}
Expand Down
17 changes: 10 additions & 7 deletions Sources/tart/Logging/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ var defaultLogger: Logger = {
return InteractiveConsoleLogger()
}
}()
var jsonLogger: Logger = NopLogger()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it's a NopLogger?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we don't output JSON by default.


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")
}
Expand All @@ -32,15 +29,21 @@ public class InteractiveConsoleLogger: Logger {
}

public class SimpleConsoleLogger: Logger {
public init() {
public func appendNewLine(_ line: String) {
print(line, terminator: "\n")
}

public func updateLastLine(_ line: String) {
print(line, terminator: "\n")
}
}

public class NopLogger: Logger {
public func appendNewLine(_ line: String) {
print(line, terminator: "\n")
// do nothing
}

public func updateLastLine(_ line: String) {
print(line, terminator: "\n")
// do nothing
}
}
5 changes: 4 additions & 1 deletion Sources/tart/VMStorageOCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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() }
Expand Down Expand Up @@ -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)
Expand Down
Loading