Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 19 additions & 1 deletion Sources/ACKLocalizationCore/ACKLocalization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions Sources/ACKLocalizationCore/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}