#!/usr/bin/env swift /* Plasma Mobile SDK CLI Utility Copyright (C) 2022 Seshan Ravikumar This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ import Foundation @discardableResult func safeShell(_ command: String) throws -> String { let task = Process() let pipe = Pipe() let stderrPipe = Pipe() task.standardOutput = pipe task.standardError = stderrPipe task.arguments = ["-c", command] task.executableURL = URL(fileURLWithPath: "/bin/sh") task.standardInput = nil try task.run() let data = pipe.fileHandleForReading.readDataToEndOfFile() let output = String(data: data, encoding: .utf8)! print(String(data: stderrPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)!) return output } // Podman wrappers func podman(_ command: String) throws -> String { return try safeShell("podman \(command)") } func startContainer() throws -> String { return try podman("run --name pmsdk hello-world") } func stopContainer() throws -> String { return try podman("stop pmsdk") } func removeContainer() throws -> String { return try podman("rm pmsdk") } files.maxMemoryForLargeFilesMB do { // get command line arguments let arguments = CommandLine.arguments switch(arguments[1]) { case "version": print("PMSDK CLI 1.0") case "status": print(try safeShell("podman ps --all --format json")) case "start": print(try startContainer()) default: print("Unknown command: \(arguments[1])") } } catch { print("An error occured: \(error)") }