Retrieve API key from crowdin.yml
Before, you had to set an environmental variable with the API key for
the generate-i18n-credits.go script. As this API key is already set in
the crowdin.yml file, this change adds code so it is retrieved from
there if the environemntal variable is not set.
Change-Id: I57c8af1c50863efd21e01cfce2745d86db6b8774
diff --git a/tools/i18n/generate-i18n-credits.go b/tools/i18n/generate-i18n-credits.go
index 93fa702..0d75842 100644
--- a/tools/i18n/generate-i18n-credits.go
+++ b/tools/i18n/generate-i18n-credits.go
@@ -11,9 +11,12 @@
"os"
"strings"
"time"
+
+ "gopkg.in/yaml.v2"
)
const blockFileName = "blocked-users.txt"
+const crowdinConfigFileName = "crowdin.yml"
const projectId = "191707"
const checkAttempts = 5
const checkWaitTime = 2 * time.Second
@@ -119,10 +122,38 @@
return responseJSON, nil
}
+func getTokenFromCrowdinConfig(fileName string) (string, error) {
+ crowdinFile, err := os.Open(fileName)
+ if err != nil {
+ return "", err
+ }
+ defer crowdinFile.Close()
+
+ configRaw, err := ioutil.ReadAll(crowdinFile)
+ if err != nil {
+ return "", err
+ }
+
+ var configYaml map[interface{}]interface{}
+ if err := yaml.Unmarshal([]byte(configRaw), &configYaml); err != nil {
+ return "", err
+ }
+
+ if val, ok := configYaml["api_token"]; ok {
+ return val.(string), nil
+ }
+
+ return "", fmt.Errorf("api_token value isn't set")
+}
+
func apiCall(path string, method string, body string) (*http.Response, error) {
token, isTokenSet := os.LookupEnv("GTRANSLATE_CROWDIN_API_KEY")
if !isTokenSet {
- return nil, fmt.Errorf("Environmental variable GTRANSLATE_CROWDIN_API_KEY is not set.")
+ var err error
+ token, err = getTokenFromCrowdinConfig(crowdinConfigFileName)
+ if err != nil {
+ return nil, fmt.Errorf("Environmental variable GTRANSLATE_CROWDIN_API_KEY is not set and couldn't find the API key in %s (%v).", crowdinConfigFileName, err)
+ }
}
if body == "" {