Allow reading input from stdin
This commit is contained in:
parent
01b564adb5
commit
abc0ef910d
2 changed files with 40 additions and 7 deletions
|
@ -6,7 +6,7 @@ import PackageDescription
|
||||||
let package = Package(
|
let package = Package(
|
||||||
name: "MakeColors",
|
name: "MakeColors",
|
||||||
platforms: [
|
platforms: [
|
||||||
.macOS(.v10_15),
|
.macOS("10.15.4"),
|
||||||
],
|
],
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMinor(from: "0.3.1")),
|
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMinor(from: "0.3.1")),
|
||||||
|
|
|
@ -24,6 +24,8 @@ enum Errors: Error {
|
||||||
case duplicateColor(String)
|
case duplicateColor(String)
|
||||||
case missingReference(String)
|
case missingReference(String)
|
||||||
case cyclicReference(String)
|
case cyclicReference(String)
|
||||||
|
case cannotWriteWrapperToStdout
|
||||||
|
case cannotReadStdin
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class MakeColors: ParsableCommand, Context {
|
public final class MakeColors: ParsableCommand, Context {
|
||||||
|
@ -42,10 +44,7 @@ public final class MakeColors: ParsableCommand, Context {
|
||||||
public init() {}
|
public init() {}
|
||||||
|
|
||||||
public func run() throws {
|
public func run() throws {
|
||||||
let url = URL(fileURLWithPath: input)
|
let scanner = Scanner(string: try readInput())
|
||||||
let string = try String(contentsOf: url)
|
|
||||||
|
|
||||||
let scanner = Scanner(string: string)
|
|
||||||
scanner.charactersToBeSkipped = .whitespaces
|
scanner.charactersToBeSkipped = .whitespaces
|
||||||
|
|
||||||
let data = try scanner.colorList()
|
let data = try scanner.colorList()
|
||||||
|
@ -68,10 +67,44 @@ public final class MakeColors: ParsableCommand, Context {
|
||||||
let generator = formatter.type.init(context: self)
|
let generator = formatter.type.init(context: self)
|
||||||
let fileWrapper = try generator.generate(data: data)
|
let fileWrapper = try generator.generate(data: data)
|
||||||
|
|
||||||
let writeURL = outputURL(extension: formatter.type.defaultExtension)
|
try writeOutput(fileWrapper)
|
||||||
try fileWrapper.write(to: writeURL, options: .atomic, originalContentsURL: nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
func outputURL(extension: String) -> URL {
|
||||||
if let output = output {
|
if let output = output {
|
||||||
return URL(fileURLWithPath: output)
|
return URL(fileURLWithPath: output)
|
||||||
|
|
Loading…
Add table
Reference in a new issue