diff --git a/code/cli/munki/shared/admin/pkginfolib.swift b/code/cli/munki/shared/admin/pkginfolib.swift index a75f6b10..565ed5ed 100644 --- a/code/cli/munki/shared/admin/pkginfolib.swift +++ b/code/cli/munki/shared/admin/pkginfolib.swift @@ -294,11 +294,12 @@ func createPkgInfoFromDmg(_ dmgpath: String, /// Attempt to read a file with the same name as the input string and return its text, /// otherwise return the input string -func readFileOrString(_ fileNameOrString: String) -> String { - if !pathExists(fileNameOrString) { +func readFileOrString(_ fileNameOrString: String) throws -> String { + let expandedPath = (fileNameOrString as NSString).expandingTildeInPath + if !pathExists(expandedPath) { return fileNameOrString } - return (try? String(contentsOfFile: fileNameOrString, encoding: .utf8)) ?? fileNameOrString + return try fileContents(fileNameOrString) } /// If path appears to be inside the repo's pkgs directory, return a path relative to the pkgs dir @@ -343,6 +344,16 @@ func getMinimumOSVersionFromInstallsApps(_ pkginfo: PlistDict) -> String? { return minimumOSVersions.max()?.value } +/// return contents of file at path, expanding tilde as needed +func fileContents(_ path: String) throws -> String { + let expandedPath = (path as NSString).expandingTildeInPath + do { + return try String(contentsOfFile: expandedPath, encoding: .utf8) + } catch { + throw MunkiError("Failed to read file \(expandedPath): \(error)") + } +} + /// Return a pkginfo dictionary for installeritem func makepkginfo(_ filepath: String?, options: PkginfoOptions) throws -> PlistDict @@ -432,7 +443,7 @@ func makepkginfo(_ filepath: String?, pkginfo["catalogs"] = options.other.catalog } if let description = options.override.description { - pkginfo["description"] = readFileOrString(description) + pkginfo["description"] = try readFileOrString(description) } if let displayname = options.override.displayname { pkginfo["display_name"] = displayname @@ -478,62 +489,30 @@ func makepkginfo(_ filepath: String?, // add pkginfo scripts if specified // TODO: verify scripts start with a shebang line? if let installcheckScript = options.script.installcheckScript { - if let scriptText = try? String(contentsOfFile: installcheckScript, encoding: .utf8) { - pkginfo["installcheck_script"] = scriptText - } else { - throw MunkiError("Could not get contents of \(installcheckScript)") - } + pkginfo["installcheck_script"] = try fileContents(installcheckScript) } if let uninstallcheckScript = options.script.uninstallcheckScript { - if let scriptText = try? String(contentsOfFile: uninstallcheckScript, encoding: .utf8) { - pkginfo["uninstallcheck_script"] = scriptText - } else { - throw MunkiError("Could not get contents of \(uninstallcheckScript)") - } + pkginfo["uninstallcheck_script"] = try fileContents(uninstallcheckScript) } if let postinstallScript = options.script.postinstallScript { - if let scriptText = try? String(contentsOfFile: postinstallScript, encoding: .utf8) { - pkginfo["postinstall_script"] = scriptText - } else { - throw MunkiError("Could not get contents of \(postinstallScript)") - } + pkginfo["postinstall_script"] = try fileContents(postinstallScript) } if let preinstallScript = options.script.preinstallScript { - if let scriptText = try? String(contentsOfFile: preinstallScript, encoding: .utf8) { - pkginfo["preinstall_script"] = scriptText - } else { - throw MunkiError("Could not get contents of \(preinstallScript)") - } + pkginfo["preinstall_script"] = try fileContents(preinstallScript) } if let postuninstallScript = options.script.postuninstallScript { - if let scriptText = try? String(contentsOfFile: postuninstallScript, encoding: .utf8) { - pkginfo["postuninstall_script"] = scriptText - } else { - throw MunkiError("Could not get contents of \(postuninstallScript)") - } + pkginfo["postuninstall_script"] = try fileContents(postuninstallScript) } if let preuninstallScript = options.script.preuninstallScript { - if let scriptText = try? String(contentsOfFile: preuninstallScript, encoding: .utf8) { - pkginfo["preuninstall_script"] = scriptText - } else { - throw MunkiError("Could not get contents of \(preuninstallScript)") - } + pkginfo["preuninstall_script"] = try fileContents(preuninstallScript) } if let uninstallScript = options.script.uninstallScript { - if let scriptText = try? String(contentsOfFile: uninstallScript, encoding: .utf8) { - pkginfo["uninstall_script"] = scriptText - pkginfo["uninstall_method"] = "uninstall_script" - pkginfo["uninstallable"] = true - } else { - throw MunkiError("Could not get contents of \(uninstallScript)") - } + pkginfo["uninstall_script"] = try fileContents(uninstallScript) + pkginfo["uninstall_method"] = "uninstall_script" + pkginfo["uninstallable"] = true } if let versionScript = options.script.versionScript { - if let scriptText = try? String(contentsOfFile: versionScript, encoding: .utf8) { - pkginfo["version_script"] = scriptText - } else { - throw MunkiError("Could not get contents of \(versionScript)") - } + pkginfo["version_script"] = try fileContents(versionScript) } // more options and pkginfo bits if !installeritem.isEmpty || options.type.nopkg { @@ -588,7 +567,7 @@ func makepkginfo(_ filepath: String?, pkginfo["installer_environment"] = options.pkg.installerEnvironmentDict } if let notes = options.other.notes { - pkginfo["notes"] = readFileOrString(notes) + pkginfo["notes"] = try readFileOrString(notes) } return pkginfo