Skip to content

Commit

Permalink
Merge pull request #44 from jamf/specify-output-filename
Browse files Browse the repository at this point in the history
Specify output filename
  • Loading branch information
mattbenyo committed Dec 8, 2022
2 parents 623aa77 + fb6c19c commit 067a182
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/weichsel/ZIPFoundation",
"state" : {
"revision" : "1b662e2e7a091710ad8a963263939984e2ebf527",
"revision" : "7254c74b49cec2cb81520523ba993c671f71b066",
"version" : "0.9.14"
}
}
Expand Down
34 changes: 24 additions & 10 deletions aftermath/CaseFiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,34 @@ struct CaseFiles {
}
}

static func MoveTemporaryCaseDir(outputDir: String, isAnalysis: Bool) {
static func MoveTemporaryCaseDir(outputLocation: String, isAnalysis: Bool) {
print("Checking for existence of output location")

let fm = FileManager.default
let isDir = fm.isDirectoryThatExists(path: outputLocation)
guard isDir || fm.fileExists(atPath: outputLocation) else {
print("Output path is not a valid file or directory that exists")
return
}

print("Moving the aftermath directory from its temporary location. This may take some time. Please wait...")

var localCaseDir: URL

if isAnalysis {
localCaseDir = analysisCaseDir

// Determine if we should look in /tmp or in the Aftermath case directory within /tmp
let localCaseDir = isAnalysis ? analysisCaseDir : caseDir

let endPath: String
if isDir {
endPath = "\(outputLocation)/\(localCaseDir.lastPathComponent)"
} else {
localCaseDir = caseDir
// Ensure that we end up with the correct (.zip) path extension
endPath = fm.deletingPathExtension(path: outputLocation)
}

// The zipped case directory should end up in the specified output location
let endURL = URL(fileURLWithPath: endPath)
let zippedURL = endURL.appendingPathExtension("zip")

do {
let endURL = URL(fileURLWithPath: "\(outputDir)/\(localCaseDir.lastPathComponent)")
let zippedURL = endURL.appendingPathExtension("zip")

try fm.zipItem(at: localCaseDir, to: endURL, shouldKeepParent: true, compressionMethod: .deflate)
try fm.moveItem(at: endURL, to: zippedURL)
print("Aftermath archive moved to \(zippedURL.path)")
Expand Down
35 changes: 7 additions & 28 deletions aftermath/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class Command {
static var options: Options = []
static var analysisDir: String? = nil
static var outputDir: String = "/tmp"
static var outputLocation: String = "/tmp"
static var collectDirs: [String] = []
static let version: String = "1.2.0"

Expand Down Expand Up @@ -50,7 +50,7 @@ class Command {
case "-o", "--output":
if let index = args.firstIndex(of: arg) {
Self.options.insert(.output)
Self.outputDir = args[index + 1]
Self.outputLocation = args[index + 1]
}
case "--analyze":
if let index = args.firstIndex(of: arg) {
Expand Down Expand Up @@ -97,7 +97,7 @@ class Command {
mainModule.log("Analysis directory not provided")
return
}
guard isFileThatExists(path: dir) else {
guard FileManager.default.isFileThatExists(path: dir) else {
mainModule.log("Analysis directory is not a valid directory that exists")
return
}
Expand All @@ -113,14 +113,9 @@ class Command {
}

mainModule.log("Finished analysis module")

guard isDirectoryThatExists(path: Self.outputDir) else {
mainModule.log("Output directory is not a valid directory that exists")
return
}

// Move analysis directory to output direcotry
CaseFiles.MoveTemporaryCaseDir(outputDir: self.outputDir, isAnalysis: true)
CaseFiles.MoveTemporaryCaseDir(outputLocation: self.outputLocation, isAnalysis: true)

// End Aftermath
mainModule.log("Aftermath Finished")
Expand Down Expand Up @@ -182,15 +177,9 @@ class Command {
mainModule.log("Finished logging unified logs")

mainModule.log("Finished running Aftermath collection")


guard isDirectoryThatExists(path: Self.outputDir) else {
mainModule.log("Output directory is not a valid directory that exists")
return
}

// Copy from cache to output
CaseFiles.MoveTemporaryCaseDir(outputDir: self.outputDir, isAnalysis: false)
CaseFiles.MoveTemporaryCaseDir(outputLocation: self.outputLocation, isAnalysis: false)

// End Aftermath
mainModule.log("Aftermath Finished")
Expand Down Expand Up @@ -218,20 +207,10 @@ class Command {
exit(1)
}

static func isDirectoryThatExists(path: String) -> Bool {
var isDir : ObjCBool = false
let pathExists = FileManager.default.fileExists(atPath: path, isDirectory:&isDir)
return pathExists && isDir.boolValue
}

static func isFileThatExists(path: String) -> Bool {
let fileExists = FileManager.default.fileExists(atPath: path)
return fileExists
}

static func printHelp() {
print("-o -> specify an output location for Aftermath results (defaults to /tmp)")
print(" usage: -o Users/user/Desktop")
print(" usage: -o Users/user/Desktop ")
print(" -o Users/user/Desktop/outputFile.zip ")
print("--analyze -> Analyze the results of the Aftermath results")
print(" usage: --analyze <path_to_file>")
print("--collect-dirs -> specify locations of (space-separated) directories to dump those raw files")
Expand Down
18 changes: 18 additions & 0 deletions extensions/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ import Foundation

public extension FileManager {

func isDirectoryThatExists(path: String) -> Bool {
var isDir : ObjCBool = false
let pathExists = self.fileExists(atPath: path, isDirectory:&isDir)
return pathExists && isDir.boolValue
}

func isFileThatExists(path: String) -> Bool {
self.fileExists(atPath: path)
}

func deletingPathExtension(path: String) -> String {
if #available(macOS 13.0, *) {
return URL(filePath: path).deletingPathExtension().absoluteString
} else {
return URL(fileURLWithPath: path).deletingPathExtension().absoluteString
}
}

@discardableResult
class func delete(path: String) -> Error? {
if (FileManager.default.fileExists(atPath: path)) {
Expand Down

0 comments on commit 067a182

Please sign in to comment.