diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..d2fc5a8 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,28 @@ +name: CI + +on: + push: + branches: [develop, production] + pull_request: + branches: [develop, production] + schedule: + - cron: 0 0 * * * # once a day + +jobs: + build: + name: Swift ${{ matrix.swift }} on ${{ matrix.os }} + strategy: + matrix: + os: [macos-latest] + swift: ["5.9", "5.10", "6.0"] + runs-on: ${{ matrix.os }} + + steps: + - uses: maxim-lobanov/setup-xcode@v1 + with: + xcode-version: latest-stable + - uses: actions/checkout@v4 + - name: Build + run: swift build -v + - name: Run tests + run: swift test -v diff --git a/.github/workflows/gitflow.yml b/.github/workflows/gitflow.yml new file mode 100644 index 0000000..1f5dfbc --- /dev/null +++ b/.github/workflows/gitflow.yml @@ -0,0 +1,28 @@ +name: Gitflow + +on: + workflow_dispatch: + inputs: + version_increment: + type: choice + required: true + description: "Version increment" + default: "patch" + options: + - "major" + - "minor" + - "patch" + +jobs: + gitflow-release: + name: Gitflow Release + runs-on: macos-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Gitflow Genius Action + uses: ./ + with: + version_increment: ${{ inputs.version_increment }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/Package.swift b/Package.swift index 6ca43cd..bd33041 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.9 +// swift-tools-version:6.0.0 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription diff --git a/README.md b/README.md index b9c8530..9ba6ce0 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,14 @@ # SwiftDotenv +[![CI](https://github.com/thebarndog/swift-dotenv/workflows/CI/badge.svg)](https://github.com/pointfreeco/swift-composable-architecture/actions?query=workflow%3ACI) +[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fthebarndog%2Fswift-dotenv%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/thebarndog/swift-dotenv) +[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fthebarndog%2Fswift-dotenv%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/thebarndog/swift-dotenv) + A one-stop shop for working with environment values in a Swift program. ## Overview -`SwiftDotenv` is a small and compact Swift package that allows you to load and save `.env` files at runtime and query for those values as well as system provided environemntal values via `ProcessInfo`. It's a single abstraction for dealing with environment variables at runtime in a local configuration file that doesn't get committed to version control, rather than hardcoding strings into your app or framework. +`SwiftDotenv` is a small and compact Swift scripting library that allows you to load and save `.env` files at runtime and query for those values as well as system provided environemntal values via `ProcessInfo`. It's a single abstraction for dealing with environment variables at runtime in a local configuration file that doesn't get committed to version control, rather than hardcoding strings into your app or framework. **IMPORTANT**: Please note that storing secrets or other sensitive information in the `.env` file does not necessarily make your app secure. For more information, see [this great article from NSHipster](https://nshipster.com/secrets/). @@ -12,6 +16,10 @@ A one-stop shop for working with environment values in a Swift program. `.env` files are used, most often in server-side applications, to inject environment variables into an application during active development. They can contain api keys, secrets, and other sensitive information and therefore **should not be committed to version control**. An environment file should only exist locally on a development machine; on a continuous integration system like TravisCI or CircleCI, environment variables are added via the respective UI in lieu of a `.env` file. +### When should I use this library? + +`SwiftDotenv` is primarily meant for _scripting use-cases only_, not within applications. This is because in order to access the `.env` file from your application, it would have to be included and bundled with your application which defeats the purpose of keeping them separate to start with. You can however still use `Dotenv` as a Swiftier way of interacting with the system environment variables which will just proxy to `processInfo.environment`. + ## Installation `SwiftDotenv` supports Swift Package Manager and can be added by adding this entry to your `Package.swift` manifest file: @@ -20,6 +28,8 @@ A one-stop shop for working with environment values in a Swift program. .package(url: "https://github.com/thebarndog/swift-dotenv.git", .upToNextMajor("2.0.0")) ``` +You can also use solutions such as [`swift-sh`](https://github.com/mxcl/swift-sh) for a less cumbersome scripting setup. + ## Usage ```swift diff --git a/Sources/Dotenv.swift b/Sources/Dotenv.swift index 1fc02a1..b8671b9 100644 --- a/Sources/Dotenv.swift +++ b/Sources/Dotenv.swift @@ -1,4 +1,8 @@ +#if os(Linux) +import Glibc +#else import Darwin +#endif import Foundation /// Structure used to load and save environment files. @@ -92,13 +96,13 @@ public enum Dotenv { // MARK: - Configuration /// `FileManager` instance used to load and save configuration files. Can be replaced with a custom instance. - public static var fileManager = FileManager.default + nonisolated(unsafe) public static var fileManager = FileManager.default /// Delimeter for key value pairs, defaults to `=`. - public static var delimeter: Character = "=" + nonisolated(unsafe) public static var delimeter: Character = "=" /// Process info instance. - public static var processInfo: ProcessInfo = ProcessInfo.processInfo + nonisolated(unsafe) public static var processInfo: ProcessInfo = ProcessInfo.processInfo /// Configure the environment with environment values loaded from the environment file. /// - Parameters: @@ -164,7 +168,7 @@ public enum Dotenv { /// - key: Key to set the value with. /// - overwrite: Flag that indicates if any existing value should be overwritten, defaults to `true`. public static func set(value: String?, forKey key: String, overwrite: Bool = true) { - setenv(key, value, overwrite ? 1 : 0) + setenv(key, value ?? "", overwrite ? 1 : 0) } // MARK: - Subscripting