Skip to content

Commit 99743c7

Browse files
committed
Follow swiftlint suggestions
1 parent 34b9961 commit 99743c7

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

Sources/DotEnv/DotEnv.swift

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
import Foundation
88
import NIO
99

10+
public enum DotEnvError: Error {
11+
case fileCouldNotBeRead(String, String.Encoding)
12+
}
13+
1014
/// Either provides an EventLoopGroup or indicate to create a new one
1115
public enum EventLoopGroupSource {
1216
/// Provided EventLoopGroup
1317
case provided(EventLoopGroup)
1418
/// Create a EventLoopGroup
1519
case createNew
1620
}
17-
1821
/// Represents a `KEY=VALUE` pair in a dotenv file.
1922
public struct Line: CustomStringConvertible, Equatable {
2023
/// The key.
@@ -28,7 +31,6 @@ public struct Line: CustomStringConvertible, Equatable {
2831
return "\(self.key)=\(self.value)"
2932
}
3033
}
31-
3234
/// An environment variable loader.
3335
///
3436
/// You can either read the file and then load it or load in one step.
@@ -236,11 +238,11 @@ public struct DotEnv {
236238
public static func load(path: String,
237239
suffix: String,
238240
encoding: String.Encoding = .utf8,
239-
overwrite: Bool = true) {
240-
load(path: "\(path).\(suffix)",
241+
overwrite: Bool = true) throws {
242+
try load(path: "\(path).\(suffix)",
241243
encoding: encoding,
242244
overwrite: overwrite)
243-
load(path: path,
245+
try load(path: path,
244246
encoding: encoding,
245247
overwrite: overwrite)
246248
}
@@ -258,11 +260,15 @@ public struct DotEnv {
258260
/// - overwrite: Set to false to prevent overwiting current environment variables
259261
public static func load(path: String,
260262
encoding: String.Encoding = .utf8,
261-
overwrite: Bool = true) {
262-
let file = try! String(contentsOfFile: path, encoding: encoding)
263-
var parser = StringParser(source: file)
264-
let dotenv = Self.init(lines: parser.parse())
265-
dotenv.load(overwrite: overwrite)
263+
overwrite: Bool = true) throws {
264+
do {
265+
let file = try String(contentsOfFile: path, encoding: encoding)
266+
var parser = StringParser(source: file)
267+
let dotenv = Self.init(lines: parser.parse())
268+
dotenv.load(overwrite: overwrite)
269+
} catch {
270+
throw DotEnvError.fileCouldNotBeRead(path, encoding)
271+
}
266272
}
267273

268274
/// Reads a `DotEnv` file from the supplied path.
@@ -282,10 +288,14 @@ public struct DotEnv {
282288
/// - path: Absolute or relative path of the dotenv file.
283289
/// - encoding: Encoding of the file
284290
/// - returns: `DotEnv`
285-
public static func read(path: String, encoding: String.Encoding = .utf8) -> DotEnv {
286-
let file = try! String(contentsOfFile: path, encoding: encoding)
287-
var parser = StringParser(source: file)
288-
return .init(lines: parser.parse())
291+
public static func read(path: String, encoding: String.Encoding = .utf8) throws -> DotEnv {
292+
do {
293+
let file = try String(contentsOfFile: path, encoding: encoding)
294+
var parser = StringParser(source: file)
295+
return .init(lines: parser.parse())
296+
} catch {
297+
throw DotEnvError.fileCouldNotBeRead(path, encoding)
298+
}
289299
}
290300

291301
/// All `KEY=VALUE` pairs found in the file.

Sources/DotEnv/Parser.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ internal extension UInt8 {
3333
internal protocol Parser {
3434
/// Enables adopters to define their own source type
3535
/// e.g. `String`, `[UInt8]`, etc.
36-
associatedtype sourceType
36+
associatedtype SourceType
3737
/// Enables adopters to define their own input source type
3838
/// e.g. `String`, `[UInt8]`, etc.
39-
associatedtype initSourceType
39+
associatedtype InitSourceType
4040
/// The raw source to parse
41-
var source: sourceType { get set }
41+
var source: SourceType { get set }
4242
/// Initializer accepting source
43-
init(source: initSourceType)
43+
init(source: InitSourceType)
4444
/// Parse the source
4545
mutating func parse() -> [Line]
4646
}

Tests/DotEnvTests/DotEnvTests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import XCTest
22
import NIO
33
@testable import DotEnv
4-
4+
// swiftlint:disable all
55
final class DotEnvTests: XCTestCase {
66
func testByteArrayParser() {
77
var parser = ByteArrayParser(source: testBytes)
@@ -24,7 +24,7 @@ final class DotEnvTests: XCTestCase {
2424
}
2525

2626
func testStringDotEnv() {
27-
let env = DotEnv.read(path: filePath)
27+
let env = try! DotEnv.read(path: filePath)
2828
env.load(overwrite: true)
2929
for line in testValues {
3030
XCTAssertEqual(ProcessInfo.processInfo.environment[line.key], line.value )
@@ -102,8 +102,9 @@ final class DotEnvTests: XCTestCase {
102102

103103
static var allTests = [
104104
("testByteArrayParser", testByteArrayParser),
105-
("testStringDotEnv", testStringDotEnv),
105+
("testStringParser", testStringParser),
106106
("testByteBufferParser", testByteBufferParser),
107+
("testStringDotEnv", testStringDotEnv),
107108
("testByteBufferDotEnv", testByteBufferDotEnv),
108109
("testNoTrailingNewlineByteBuffer", testNoTrailingNewlineByteBuffer),
109110
("testNoTrailingNewlineString", testNoTrailingNewlineString),

0 commit comments

Comments
 (0)