From 3d264675bfd7b4b5add195d462c12d23a4979346 Mon Sep 17 00:00:00 2001 From: Ross Goldberg <484615+rgoldberg@users.noreply.github.com> Date: Tue, 29 Oct 2024 00:16:35 -0400 Subject: [PATCH] Move code to more appropriate files. Partial #533 Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com> --- Sources/mas/Commands/Uninstall.swift | 4 +-- .../Controllers/SoftwareMapAppLibrary.swift | 22 ++------------ Sources/mas/MAS.swift | 9 ------ Sources/mas/Models/AppID.swift | 17 +++++++++++ .../{Controllers => Utilities}/Finder.swift | 0 Sources/mas/Utilities/ProcessInfo.swift | 29 +++++++++++++++++++ 6 files changed, 50 insertions(+), 31 deletions(-) create mode 100644 Sources/mas/Models/AppID.swift rename Sources/mas/{Controllers => Utilities}/Finder.swift (100%) create mode 100644 Sources/mas/Utilities/ProcessInfo.swift diff --git a/Sources/mas/Commands/Uninstall.swift b/Sources/mas/Commands/Uninstall.swift index 0cada84..46ea06e 100644 --- a/Sources/mas/Commands/Uninstall.swift +++ b/Sources/mas/Commands/Uninstall.swift @@ -32,12 +32,12 @@ extension MAS { throw MASError.macOSUserMustBeRoot } - guard let username = getSudoUsername() else { + guard let username = ProcessInfo.processInfo.sudoUsername else { throw MASError.runtimeError("Could not determine the original username") } guard - let uid = getSudoUID(), + let uid = ProcessInfo.processInfo.sudoUID, seteuid(uid) == 0 else { throw MASError.runtimeError("Failed to switch effective user from 'root' to '\(username)'") diff --git a/Sources/mas/Controllers/SoftwareMapAppLibrary.swift b/Sources/mas/Controllers/SoftwareMapAppLibrary.swift index 7fc48e7..77e47e5 100644 --- a/Sources/mas/Controllers/SoftwareMapAppLibrary.swift +++ b/Sources/mas/Controllers/SoftwareMapAppLibrary.swift @@ -43,24 +43,6 @@ class SoftwareMapAppLibrary: AppLibrary { } } -func getSudoUsername() -> String? { - ProcessInfo.processInfo.environment["SUDO_USER"] -} - -func getSudoUID() -> uid_t? { - guard let uid = ProcessInfo.processInfo.environment["SUDO_UID"] else { - return nil - } - return uid_t(uid) -} - -func getSudoGID() -> gid_t? { - guard let gid = ProcessInfo.processInfo.environment["SUDO_GID"] else { - return nil - } - return gid_t(gid) -} - private func getOwnerAndGroupOfItem(atPath path: String) throws -> (uid_t, gid_t) { do { let attributes = try FileManager.default.attributesOfItem(atPath: path) @@ -75,11 +57,11 @@ private func getOwnerAndGroupOfItem(atPath path: String) throws -> (uid_t, gid_t } private func chown(paths: [String]) throws -> [String: (uid_t, gid_t)] { - guard let sudoUID = getSudoUID() else { + guard let sudoUID = ProcessInfo.processInfo.sudoUID else { throw MASError.runtimeError("Failed to get original uid") } - guard let sudoGID = getSudoGID() else { + guard let sudoGID = ProcessInfo.processInfo.sudoGID else { throw MASError.runtimeError("Failed to get original gid") } diff --git a/Sources/mas/MAS.swift b/Sources/mas/MAS.swift index aee1262..56e814f 100644 --- a/Sources/mas/MAS.swift +++ b/Sources/mas/MAS.swift @@ -7,7 +7,6 @@ // import ArgumentParser -import Foundation import PromiseKit @main @@ -55,11 +54,3 @@ struct MAS: ParsableCommand { Self.initialize() } } - -typealias AppID = UInt64 - -extension NSNumber { - var appIDValue: AppID { - uint64Value - } -} diff --git a/Sources/mas/Models/AppID.swift b/Sources/mas/Models/AppID.swift new file mode 100644 index 0000000..0b8689f --- /dev/null +++ b/Sources/mas/Models/AppID.swift @@ -0,0 +1,17 @@ +// +// AppID.swift +// mas +// +// Created by Ross Goldberg on 2024-10-29. +// Copyright © 2024 mas-cli. All rights reserved. +// + +import Foundation + +typealias AppID = UInt64 + +extension NSNumber { + var appIDValue: AppID { + uint64Value + } +} diff --git a/Sources/mas/Controllers/Finder.swift b/Sources/mas/Utilities/Finder.swift similarity index 100% rename from Sources/mas/Controllers/Finder.swift rename to Sources/mas/Utilities/Finder.swift diff --git a/Sources/mas/Utilities/ProcessInfo.swift b/Sources/mas/Utilities/ProcessInfo.swift new file mode 100644 index 0000000..a7e59b1 --- /dev/null +++ b/Sources/mas/Utilities/ProcessInfo.swift @@ -0,0 +1,29 @@ +// +// ProcessInfo.swift +// mas +// +// Created by Ross Goldberg on 2024-10-29. +// Copyright © 2024 mas-cli. All rights reserved. +// + +import Foundation + +extension ProcessInfo { + var sudoUsername: String? { + environment["SUDO_USER"] + } + + var sudoUID: uid_t? { + guard let uid = environment["SUDO_UID"] else { + return nil + } + return uid_t(uid) + } + + var sudoGID: gid_t? { + guard let gid = environment["SUDO_GID"] else { + return nil + } + return gid_t(gid) + } +}