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 == "" {
diff --git a/tools/i18n/go.mod b/tools/i18n/go.mod
new file mode 100644
index 0000000..f6901ad
--- /dev/null
+++ b/tools/i18n/go.mod
@@ -0,0 +1,5 @@
+module generate-i18n-credits
+
+go 1.16
+
+require gopkg.in/yaml.v2 v2.4.0
diff --git a/tools/i18n/go.sum b/tools/i18n/go.sum
new file mode 100644
index 0000000..dd0bc19
--- /dev/null
+++ b/tools/i18n/go.sum
@@ -0,0 +1,4 @@
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
+gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=