Allow reading input from stdin

This commit is contained in:
Sven Weidauer 2020-12-31 19:31:42 +01:00
parent 01b564adb5
commit abc0ef910d
2 changed files with 40 additions and 7 deletions

View file

@ -6,7 +6,7 @@ import PackageDescription
let package = Package(
name: "MakeColors",
platforms: [
.macOS(.v10_15),
.macOS("10.15.4"),
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMinor(from: "0.3.1")),

View file

@ -24,6 +24,8 @@ enum Errors: Error {
case duplicateColor(String)
case missingReference(String)
case cyclicReference(String)
case cannotWriteWrapperToStdout
case cannotReadStdin
}
public final class MakeColors: ParsableCommand, Context {
@ -42,10 +44,7 @@ public final class MakeColors: ParsableCommand, Context {
public init() {}
public func run() throws {
let url = URL(fileURLWithPath: input)
let string = try String(contentsOf: url)
let scanner = Scanner(string: string)
let scanner = Scanner(string: try readInput())
scanner.charactersToBeSkipped = .whitespaces
let data = try scanner.colorList()
@ -68,10 +67,44 @@ public final class MakeColors: ParsableCommand, Context {
let generator = formatter.type.init(context: self)
let fileWrapper = try generator.generate(data: data)
let writeURL = outputURL(extension: formatter.type.defaultExtension)
try fileWrapper.write(to: writeURL, options: .atomic, originalContentsURL: nil)
try writeOutput(fileWrapper)
}
func readInput() throws -> String {
if input == "-" {
return try readStdin()
}
let url = URL(fileURLWithPath: input)
return try String(contentsOf: url)
}
func readStdin() throws -> String {
guard
let data = try FileHandle.standardInput.readToEnd(),
let input = String(data: data, encoding: .utf8)
else {
throw Errors.cannotReadStdin
}
return input
}
func writeOutput(_ wrapper: FileWrapper) throws {
if shouldWriteToStdout {
guard wrapper.isRegularFile, let contents = wrapper.regularFileContents else {
throw Errors.cannotWriteWrapperToStdout
}
FileHandle.standardOutput.write(contents)
} else {
let writeURL = outputURL(extension: formatter.type.defaultExtension)
try wrapper.write(to: writeURL, options: .atomic, originalContentsURL: nil)
}
}
var shouldWriteToStdout: Bool { input == "-" && output == nil }
func outputURL(extension: String) -> URL {
if let output = output {
return URL(fileURLWithPath: output)