|
| 1 | +import ArgumentParser |
| 2 | +import DotEnvy |
| 3 | +import Foundation |
| 4 | + |
| 5 | +@main |
| 6 | +struct Tool: ParsableCommand { |
| 7 | + static var configuration = CommandConfiguration( |
| 8 | + commandName: "dotenvy-tool", |
| 9 | + abstract: "Tool for working with dotenv files", |
| 10 | + subcommands: [Check.self, JSON.self] |
| 11 | + ) |
| 12 | +} |
| 13 | + |
| 14 | +struct Check: ParsableCommand { |
| 15 | + static var configuration |
| 16 | + = CommandConfiguration( |
| 17 | + abstract: "Check syntax of input.", |
| 18 | + discussion: """ |
| 19 | + In case of a syntax error, the error is printed to standard error |
| 20 | + and the command exits with failure code \(ExitCode.failure.rawValue). |
| 21 | +
|
| 22 | + If there are no problems reading the input, nothing is printed |
| 23 | + and the command exits with \(ExitCode.success.rawValue). |
| 24 | + """ |
| 25 | + ) |
| 26 | + |
| 27 | + @Option( |
| 28 | + name: [.customShort("i"), .long], |
| 29 | + help: "Input. Standard input is used with -. If omitted, try to use .env in cwd" |
| 30 | + ) |
| 31 | + var input: Input? |
| 32 | + |
| 33 | + func run() throws { |
| 34 | + _ = try loadInput(self.input) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +struct JSON: ParsableCommand { |
| 39 | + static var configuration |
| 40 | + = CommandConfiguration( |
| 41 | + abstract: "Convert input to JSON.", |
| 42 | + discussion: """ |
| 43 | + The input is converted to a JSON object. |
| 44 | +
|
| 45 | + In case of a syntax error, the error is printed to standard error and the |
| 46 | + command exits with failure code \(ExitCode.failure.rawValue). |
| 47 | +
|
| 48 | + If there are no problems reading the input, the JSON value is printed to |
| 49 | + standard output and the command exits with \(ExitCode.success.rawValue). |
| 50 | + """ |
| 51 | + ) |
| 52 | + |
| 53 | + @Option( |
| 54 | + name: [.customShort("i"), .long], |
| 55 | + help: "Input. Standard input is used with -. If omitted, try to use .env in cwd" |
| 56 | + ) |
| 57 | + var input: Input? |
| 58 | + |
| 59 | + @Flag(help: "Pretty print JSON") |
| 60 | + var pretty: Bool = false |
| 61 | + |
| 62 | + func run() throws { |
| 63 | + let values = try loadInput(self.input) |
| 64 | + let json = try JSONSerialization.data( |
| 65 | + withJSONObject: values, |
| 66 | + options: self.pretty ? [.prettyPrinted, .sortedKeys] : [] |
| 67 | + ) |
| 68 | + FileHandle.standardOutput.write(json) |
| 69 | + FileHandle.standardOutput.write(Data("\n".utf8)) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +enum Input: ExpressibleByArgument { |
| 74 | + case stdin |
| 75 | + case fileURL(FileURL) |
| 76 | + |
| 77 | + init?(argument: String) { |
| 78 | + if argument == "-" { |
| 79 | + self = .stdin |
| 80 | + } else if let fileURL = FileURL(argument: argument) { |
| 81 | + self = .fileURL(fileURL) |
| 82 | + } else { |
| 83 | + return nil |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +struct FileURL: ExpressibleByArgument { |
| 89 | + var url: URL |
| 90 | + |
| 91 | + init?(argument: String) { |
| 92 | + // the new URL(filePath:directoryHint:) is not available on Linux |
| 93 | + let url = URL(fileURLWithPath: argument, isDirectory: false) |
| 94 | + guard url.isFileURL else { |
| 95 | + return nil |
| 96 | + } |
| 97 | + self.url = url |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +private func loadInput(_ input: Input?) throws -> [String: String] { |
| 102 | + if let input = input { |
| 103 | + let string = try readInput(input) |
| 104 | + do { |
| 105 | + return try DotEnvironment.parse(string: string) |
| 106 | + } catch let error as ParseErrorWithLocation { |
| 107 | + FileHandle.standardError.write(Data(error.formatError(source: string).utf8)) |
| 108 | + FileHandle.standardError.write(Data("\n".utf8)) |
| 109 | + throw ExitCode.failure |
| 110 | + } |
| 111 | + } else { |
| 112 | + do { |
| 113 | + return try DotEnvironment.loadValues() |
| 114 | + } catch let error as LoadError { |
| 115 | + FileHandle.standardError.write(Data(error.description.utf8)) |
| 116 | + FileHandle.standardError.write(Data("\n".utf8)) |
| 117 | + throw ExitCode.failure |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +private func readInput(_ input: Input) throws -> String { |
| 123 | + let data: Data |
| 124 | + switch input { |
| 125 | + case .stdin: |
| 126 | + data = FileHandle.standardInput.readDataToEndOfFile() |
| 127 | + case let .fileURL(fileURL): |
| 128 | + data = try Data(contentsOf: fileURL.url) |
| 129 | + } |
| 130 | + guard let string = String(data: data, encoding: .utf8) else { |
| 131 | + throw ValidationError("Input could not be decoded as UTF-8") |
| 132 | + } |
| 133 | + return string |
| 134 | +} |
0 commit comments