diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e8b7bd..cc6b8c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ ## master +### Added +- Add support for loading secrets from environment variables ([#29](https://github.com/AckeeCZ/ACKLocalization/pull/29), kudos to @leinhauplk) + ## 1.2.0 ### Added diff --git a/README.md b/README.md index 443123a..584377a 100644 --- a/README.md +++ b/README.md @@ -84,12 +84,22 @@ Attributes documentation: | `apiKey` | ❌ | API key that will be used to communicate with Google Sheets API, `apiKey` or `serviceAccount` has to be provided | | `serviceAccount` | ❌ | Path to service account file that will be used to access spreadsheet, `apiKey` or `serviceAccount` has to be provided | | `spreadsheetID` | ✅ | Identifier of spreadsheet that should be downloaded | -| `spreadsheetTabName` | ❌ | Name of spreadsheet tab to be fetched, ff nothing is specified, we will use the first tab in spreadsheet | +| `spreadsheetTabName` | ❌ | Name of spreadsheet tab to be fetched, if nothing is specified, we will use the first tab in spreadsheet | | `stringsFileName` | ❌ | Name of strings file that should be generated | The file has to be in the same directory where you call ACKLocalization. -To be able to communicate with Google Sheets API, you need to provide either `apiKey` or `serviceAccount` parameter. If both are provided, then `serviceAccount` will be used. +To be able to communicate with Google Sheets API, you need to provide either `apiKey` or `serviceAccount` parameter or [use environment variable](#environment-variables). If both are provided, then `serviceAccount` will be used. + +### Environment variables + +Do you want to share secrets across multiple projects, or do you not want to keep secrets in the project repository? We have the solution for you. Just set one of the environment variables below. + +`ACKLOCALIZATION_SERVICE_ACCOUNT_PATH` - Path to service account file that will be used to access spreadsheet +`ACKLOCALIZATION_API_KEY` - API key that will be used to communicate with Google Sheets API + +`apiKey` or `serviceAccount` defined in `localization.json` have higher priority than environment values. +If both are provided then `ACKLOCALIZATION_SERVICE_ACCOUNT_PATH` will be used. ### Calling ACKLocalization diff --git a/Sources/ACKLocalizationCore/ACKLocalization.swift b/Sources/ACKLocalizationCore/ACKLocalization.swift index 340b62b..e158d42 100644 --- a/Sources/ACKLocalizationCore/ACKLocalization.swift +++ b/Sources/ACKLocalizationCore/ACKLocalization.swift @@ -277,8 +277,26 @@ public final class ACKLocalization { return fetchSheetValues(config.spreadsheetTabName, spreadsheetId: config.spreadsheetID, serviceAccount: serviceAccount) } else if let apiKey = config.apiKey { return fetchSheetValues(config.spreadsheetTabName, spreadsheetId: config.spreadsheetID, apiKey: apiKey) + } else if let serviceAccountPath = ProcessInfo.processInfo.environment[Constants.serviceAccountPath] { + let serviceAccount = try loadServiceAccount(from: serviceAccountPath) + return fetchSheetValues( + config.spreadsheetTabName, + spreadsheetId: config.spreadsheetID, + serviceAccount: serviceAccount + ) + } else if let apiKey = ProcessInfo.processInfo.environment[Constants.apiKey] { + let apiKey = APIKey(value: apiKey) + return fetchSheetValues(config.spreadsheetTabName, spreadsheetId: config.spreadsheetID, apiKey: apiKey) } else { - throw LocalizationError(message: "Either `apiKey` or `serviceAccount` must be provided in `localization.json` file") + let errorMessage = """ + Unable to load API key or service account path. Please check if: + + - `apiKey` or `serviceAccount` attribute is provided in `localization.json` file + or + - `\(Constants.apiKey)` or `\(Constants.serviceAccountPath)` environment variable is set + """ + + throw LocalizationError(message: errorMessage) } } catch { switch error { diff --git a/Sources/ACKLocalizationCore/Constants.swift b/Sources/ACKLocalizationCore/Constants.swift index 20ffc04..bbbc01c 100644 --- a/Sources/ACKLocalizationCore/Constants.swift +++ b/Sources/ACKLocalizationCore/Constants.swift @@ -14,4 +14,10 @@ public enum Constants { /// Regex pattern for suffix of the plural translations public static let pluralPattern = #"^([\w.]+)?##\{([\w]+)?\}$"# + + /// Environment variable key for providing service account path + public static let serviceAccountPath = "ACKLOCALIZATION_SERVICE_ACCOUNT_PATH" + + /// Environment variable key for providing API key + public static let apiKey = "ACKLOCALIZATION_API_KEY" }