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 commands that add Peel's lines to ~/.zshrc and ~/.ssh/config

Țugui Dragoș6b7693a98575
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/PathBelowHome.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/PathBelowHome.swift
index 94827987..6c7565a5 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/PathBelowHome.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/PathBelowHome.swift
@@ -9,4 +9,21 @@ extension URL {
guard names.count > homeNames.count, names.starts(with: homeNames) else { return nil }
return names.dropFirst(homeNames.count).joined(separator: "/")
}
+
+ /// The path as a command in Terminal takes it: from `~` when it is in the home folder and needs no quoting,
+ /// and otherwise whole, in single quotes.
+ func pathForTheShell(home: URL) -> String {
+ let plain: (Character) -> Bool = { $0.isASCII && ($0.isLetter || $0.isNumber || "._/-".contains($0)) }
+ if let below = path(below: home), below.allSatisfy(plain) {
+ return "~/" + below
+ }
+ return path(percentEncoded: false).quotedForTheShell
+ }
+}
+
+extension String {
+ /// The text in single quotes, as the shell reads it back unchanged.
+ var quotedForTheShell: String {
+ "'" + replacing("'", with: "'\\''") + "'"
+ }
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/SSHFile.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/SSHFile.swift
index 4caca9a8..e79b0bbf 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/SSHFile.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/SSHFile.swift
@@ -46,6 +46,14 @@ public enum SSHFile {
return ["Match all", " Include \"\(path.replacing("\\", with: "\\\\").replacing("\"", with: "\\\""))\""]
}
+ /// The command that adds `lines` at the end of `config`, after an empty line, making the folder it is in, closed
+ /// to everyone else as ssh wants it, and the file itself when they are not there.
+ public static func command(adding lines: [String], to config: URL, home: URL) -> String {
+ let folder = config.deletingLastPathComponent().pathForTheShell(home: home)
+ let arguments = ([""] + lines).map(\.quotedForTheShell).joined(separator: " ")
+ return "mkdir -p -m 700 \(folder) && printf '%s\\n' \(arguments) >> \(config.pathForTheShell(home: home))"
+ }
+
/// Whether an `Include` line of `config` names the file, however its spaces are written.
public static func isIncluded(_ url: URL, from config: URL, home: URL) -> Bool {
guard let data = BoundedRead.data(at: config) else { return false }
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/ShellFile.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/ShellFile.swift
index 9c68c5ba..f5460d24 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/ShellFile.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/ShellFile.swift
@@ -65,6 +65,11 @@ public enum ShellFile {
return "[[ -r \(path) ]] && source \(path)"
}
+ /// The command that adds `line` at the end of `startupFile`, which it makes when there is none.
+ public static func command(adding line: String, to startupFile: URL, home: URL) -> String {
+ "printf '%s\\n' \(line.quotedForTheShell) >> \(startupFile.pathForTheShell(home: home))"
+ }
+
/// Whether a line of `startupFile` that is not a comment names the file, however its spaces are written.
public static func isSourced(_ url: URL, from startupFile: URL, home: URL) -> Bool {
guard let data = BoundedRead.data(at: startupFile) else { return false }
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift
index 5133f511..c2a9262b 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift
@@ -21,6 +21,12 @@ public enum ShellSessions: Sendable, Equatable {
}
}
+ /// The `.zshrc` beside the `.zshenv` zsh reads, where the line that sources Peel's file goes.
+ public var zshrc: URL? {
+ guard case .zsh(let zshenv) = self else { return nil }
+ return zshenv.deletingLastPathComponent().appending(path: ".zshrc", directoryHint: .notDirectory)
+ }
+
public var name: String {
switch self {
case .zsh: "zsh"
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ShellFileTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ShellFileTests.swift
index 76708108..eb9e7b8e 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ShellFileTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ShellFileTests.swift
@@ -1,6 +1,7 @@
import Darwin
import Foundation
@testable import PeelCore
+import PeelPrivileged
import Testing
struct ShellFileTests {
@@ -154,3 +155,54 @@ struct ZshRequirementsTests {
#expect(PromptStyle.macOS.isKnown(by: []))
}
}
+
+struct PeelLinesCommandTests {
+ private func run(_ command: String, home: URL) async throws -> Int32 {
+ let answer = await Subprocess.run("/bin/zsh", ["-f", "-c", command], environment: ["HOME": home.path(percentEncoded: false), "PATH": "/usr/bin:/bin"], timeout: 10)
+ guard case .success(let output) = answer else { throw POSIXError(.EIO) }
+ #expect(output.errorText.isEmpty, "\(output.errorText)")
+ return output.status
+ }
+
+ private func madeUpHome() throws -> URL {
+ let home = FileManager.default.temporaryDirectory.appending(path: "PeelLines-\(UUID().uuidString)", directoryHint: .isDirectory)
+ try FileManager.default.createDirectory(at: home, withIntermediateDirectories: true)
+ return home
+ }
+
+ @Test func theShellsCommandAddsTheLineAtTheEndOfZshrcAndPeelFindsIt() async throws {
+ let home = try madeUpHome()
+ let file = ShellFile.url(in: home.appending(path: "Library/Application Support/Peel", directoryHint: .isDirectory))
+ let zshrc = home.appending(path: ".zshrc")
+ let line = ShellFile.line(sourcing: file, home: home)
+ let command = ShellFile.command(adding: line, to: zshrc, home: home)
+ #expect(command.hasSuffix(" >> ~/.zshrc"))
+ #expect(try await run(command, home: home) == 0)
+ #expect(try String(contentsOf: zshrc, encoding: .utf8) == line + "\n")
+ #expect(ShellFile.isSourced(file, from: zshrc, home: home))
+ try "setopt AUTO_CD\n".write(to: zshrc, atomically: true, encoding: .utf8)
+ #expect(try await run(command, home: home) == 0)
+ #expect(try String(contentsOf: zshrc, encoding: .utf8) == "setopt AUTO_CD\n" + line + "\n")
+ }
+
+ @Test func theSSHCommandMakesAClosedFolderAndAddsTheLinesPeelFinds() async throws {
+ let home = try madeUpHome()
+ let file = SSHFile.url(in: home.appending(path: "Library/Application Support/Peel", directoryHint: .isDirectory))
+ let config = home.appending(path: ".ssh/config")
+ let lines = SSHFile.lines(including: file, home: home)
+ #expect(try await run(SSHFile.command(adding: lines, to: config, home: home), home: home) == 0)
+ let folder = try FileManager.default.attributesOfItem(atPath: config.deletingLastPathComponent().path(percentEncoded: false))
+ #expect((folder[.posixPermissions] as? Int) == 0o700)
+ #expect(try String(contentsOf: config, encoding: .utf8) == "\n" + lines.joined(separator: "\n") + "\n")
+ #expect(SSHFile.isIncluded(file, from: config, home: home))
+ }
+
+ @Test func aPathOutsideTheHomeOrWithASpaceIsQuotedForTheShell() {
+ let home = URL(filePath: "/Users/example", directoryHint: .isDirectory)
+ #expect(home.appending(path: ".zshrc").pathForTheShell(home: home) == "~/.zshrc")
+ #expect(home.appending(path: "My Files/.zshrc").pathForTheShell(home: home) == "'/Users/example/My Files/.zshrc'")
+ #expect(URL(filePath: "/etc/it's").pathForTheShell(home: home) == "'/etc/it'\\''s'")
+ #expect(ShellSessions.zsh(startupFile: home.appending(path: ".zshenv")).zshrc == home.appending(path: ".zshrc"))
+ #expect(ShellSessions.bash.zshrc == nil)
+ }
+}