Giter VIP home page Giter VIP logo

diskutil's Introduction

DiskUtil

DiskUtil is a simple SwiftPM Package for creating / ejecting / formatting disk images. Build using Swift 5.1, XCode 11.3, supports macOS only.

Usage

The framework provide its functionality through the DiskUtil struct. The struct has 3 main static functions:

///  Creates a disk image from a ram region with a given size in megabytes.
///
///  - Parameter  size: The size of the ram region you want in MB.
///  - Parameter  completion: Completion block containing the result of the process.
///  Returns: The "hardware" `id` of the disk.
public static func createDiskImage(withSize size: Int, completion: @escaping (Response) -> ())

///  Erases an allocated RAM region with a given id to a specific filesystem and mounts it.
///
///  - Parameters:
///  - devicePath: The path that points to the device.
///  - name: The name you want set to your new RAM Disk
///  - fileSystem: the file system in which you want your ram region formatted.
///  - Returns: `true` if erase was successful, `false` if error had occured.
public static func eraseDisk(withDevicePath devicePath: String, name: String, fileSystem: FileSystem, completion: @escaping (Response) -> ())

///  Ejects a ram disk from Finder, destroying all things stored in it.
///
///  - Parameter  devicePath: The id of the disk that you want ejected.
///  - Returns: `true` if eject was successful, `false` otherwise.
public static func ejectDisk(withDevicePath devicePath: String, completion: @escaping (Response) -> ())

Each of these 3 functions give back asynchronously object of type Response that look like this:

// MARK: - Response

extension  DiskUtil {

	///  Container holding the output of a terminal task.
	public  struct  Response {

		///  The content of the output pipe after the task has been executed.
		public let output: String

		///  The content of the error pipe after the task has been executed
		public let error: String
		
		///  The task's exit code.
		public  let  terminationStatus: Int32
	}
}

Example

Creating, formatting and ejecting disk image using DiskUtil:

DiskUtil.createDiskImage(withSize: 10) { (responce) in
	guard responce.error.isEmpty else { /* Fail */ return }
	let devicePath = responce.output
	
	DiskUtil.eraseDisk(withDevicePath: devicePath, name: "Test Disk", fileSystem: .apfs) { (responce) in
		guard responce.error.isEmpty else { /* Fail */ return }
		DiskUtil.ejectDisk(withDevicePath: devicePath) { (responce) in
			guard responce.error.isEmpty else { /* Fail */ return }
			/* Success! */
		}
	}
}

Installation

Swift Package Manager

  1. Go to your project settings
  2. Tap on your Project name above listed targets
  3. Open tab swift packages
  4. Add DiskUtil's repo as a package.

Further Information

If you find DiskUtil Package helpful, you might as well like DiskMonitor.

Licence

The framework is licensed under MIT licence. For more information see file LICENCE

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.