Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Give the password advice only when sudo asked for one

Țugui Dragoș2c8a7c478411
diff --git a/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift b/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
index 4b898ca1..246afece 100644
--- a/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
@@ -477,6 +477,12 @@ indirect enum CaskValue: Decodable, Sendable {
public enum Homebrew {
static let notInstalled = "Homebrew isn’t installed."
+
+ /// Whether `sudo` stopped for a password it had no terminal to ask in, in its own words, which it writes in English
+ /// in the environment Peel runs Homebrew in.
+ public static func askedForAPassword(in transcript: String) -> Bool {
+ ["a terminal is required to read the password", "a password is required"].contains(where: transcript.contains)
+ }
/// The command brew.sh gives for installing Homebrew. Its script says what it will do and waits before doing it.
public static let installCommand = "/bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
public static let website = URL(string: "https://brew.sh")!
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
index 55abab0d..fb8e166e 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
@@ -19,6 +19,28 @@ struct HomebrewTests {
/// `man brew`: unless `HOMEBREW_NO_INSTALL_CLEANUP` is set, `brew upgrade` runs `brew cleanup` by itself, for
/// the upgraded packages and every 30 days for all of them, which deletes old versions and downloads for good.
/// Peel deletes those only through Clean Up, which asks first, so no call it makes may clean up on its own.
+ @Test func knowsWhenSudoAskedForAPassword() {
+ let sudo = """
+ ==> Running installer for zoom with sudo; the password may be necessary.
+ sudo: a terminal is required to read the password; either use the -S option to read from standard input \
+ or configure an askpass helper
+ sudo: a password is required
+ Error: Failure while executing; `/usr/bin/sudo -u root -E -- /usr/sbin/installer -pkg zoom.pkg` exited with 1.
+ """
+ let network = """
+ curl: (6) Could not resolve host: formulae.brew.sh
+ Error: Failure while executing; `/usr/bin/curl --disable --globoff` exited with 6.
+ """
+ let build = """
+ Error: An exception occurred within a child process:
+ BuildError: Failed executing: make install
+ """
+
+ #expect(Homebrew.askedForAPassword(in: sudo))
+ #expect(!Homebrew.askedForAPassword(in: network))
+ #expect(!Homebrew.askedForAPassword(in: build))
+ }
+
@Test func anUpgradeNeverCleansUpOnItsOwn() {
for autoUpdate in [true, false] {
#expect(Homebrew.environment(autoUpdate: autoUpdate)["HOMEBREW_NO_INSTALL_CLEANUP"] == "1")
diff --git a/Peel/Views/Homebrew/HomebrewList.swift b/Peel/Views/Homebrew/HomebrewList.swift
index 06c85410..9d7bff23 100644
--- a/Peel/Views/Homebrew/HomebrewList.swift
+++ b/Peel/Views/Homebrew/HomebrewList.swift
@@ -654,7 +654,7 @@ struct HomebrewOutputView: View {
Image(systemName: result.succeeded ? "checkmark.circle.fill" : "exclamationmark.triangle.fill")
.foregroundStyle(result.succeeded ? .green : .red)
}
- if !result.succeeded {
+ if !result.succeeded, Homebrew.askedForAPassword(in: result.output) {
Text("If the command needs your password, run it in Terminal.")
.font(.callout)
.foregroundStyle(.secondary)