diff --git a/core/core.go b/core/core.go index e0dc9ad..9d98817 100644 --- a/core/core.go +++ b/core/core.go @@ -1,6 +1,7 @@ package core import ( + "fmt" "io/ioutil" "log" "os" @@ -225,8 +226,14 @@ func buildDocumentation(branches []string, versionsInfo types.VersionsInformatio return err } + args := []string{"run", "--rm", "-v", versionsInfo.CurrentPath + ":/mkdocs"} + if _, err = os.Stat(filepath.Join(versionsInfo.CurrentPath, ".env")); err == nil { + args = append(args, fmt.Sprintf("--env-file=%s", filepath.Join(versionsInfo.CurrentPath, ".env"))) + } + args = append(args, dockerImageFullName, "mkdocs", "build") + // Run image - output, err := docker.Exec(config.Debug, "run", "--rm", "-v", versionsInfo.CurrentPath+":/mkdocs", dockerImageFullName, "mkdocs", "build") + output, err := docker.Exec(config.Debug, args...) if err != nil { log.Println(output) return err diff --git a/manifest/manifest.go b/manifest/manifest.go index e723f6b..18726bf 100644 --- a/manifest/manifest.go +++ b/manifest/manifest.go @@ -1,6 +1,7 @@ package manifest import ( + "fmt" "io/ioutil" "os" "path" @@ -12,8 +13,12 @@ import ( "gopkg.in/yaml.v2" ) -// FileName file name of the mkdocs manifest file. -const FileName = "mkdocs.yml" +const ( + // FileName file name of the mkdocs manifest file. + FileName = "mkdocs.yml" + // TempPrefixEnvName temp prefix for environment variable. + TempPrefixEnvName = "STRUCTOR_TEMP_" +) // Read Reads the manifest. func Read(manifestFilePath string) (map[string]interface{}, error) { @@ -38,7 +43,19 @@ func replaceEnvVariables(bytes []byte) []byte { var re = regexp.MustCompile(`!!python\/object\/apply:os\.getenv\s\[[\'|\"]?([A-Z0-9_-]+)[\'|\"]?\]`) result := re.FindAllStringSubmatch(data, -1) for _, value := range result { - data = strings.ReplaceAll(data, value[0], os.Getenv(value[1])) + data = strings.ReplaceAll(data, value[0], TempPrefixEnvName+value[1]) + } + + return []byte(data) +} + +func rewriteEnvVariables(bytes []byte) []byte { + data := string(bytes) + var re = regexp.MustCompile(TempPrefixEnvName + `([A-Z0-9_-]+)`) + result := re.FindAllStringSubmatch(data, -1) + + for _, value := range result { + data = strings.ReplaceAll(data, value[0], fmt.Sprintf(`!!python/object/apply:os.getenv ["%s"]`, value[1])) } return []byte(data) @@ -51,6 +68,8 @@ func Write(manifestFilePath string, manif map[string]interface{}) error { return errors.Wrap(err, "error when marshal MkDocs Manifest.") } + out = rewriteEnvVariables(out) + return ioutil.WriteFile(manifestFilePath, out, os.ModePerm) } diff --git a/manifest/manifest_test.go b/manifest/manifest_test.go index 6039e43..579ec44 100644 --- a/manifest/manifest_test.go +++ b/manifest/manifest_test.go @@ -1,7 +1,6 @@ package manifest import ( - "os" "path/filepath" "testing" @@ -38,7 +37,7 @@ func TestRead(t *testing.T) { "site_url": "https://docs.traefik.io", "copyright": "Copyright © 2016-2019 Containous", "extra": map[interface{}]interface{}{ - "traefikVersion": "powpow", + "traefikVersion": TempPrefixEnvName + "TRAEFIK_VERSION", }, "theme": map[interface{}]interface{}{ "include_sidebar": true, @@ -113,8 +112,6 @@ func TestRead(t *testing.T) { t.Run(test.desc, func(t *testing.T) { t.Parallel() - err := os.Setenv("TRAEFIK_VERSION", "powpow") - require.NoError(t, err) content, err := Read(filepath.Join(".", "fixtures", test.filename)) require.NoError(t, err)