123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package got
- import (
- "text/template"
- . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
- G "github.com/dave/jennifer/jen"
- )
- var (
- environmentStmtsTmpl *template.Template
- environmentStmtsErr error
- )
- func init() {
- environmentStmtsTmpl, environmentStmtsErr = ParseTemplate(`
- import (
- "os"
- "fmt"
- common "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
- )
-
- var (
- {{range .variables}} {{.CamelID}} = "{{.Default}}"
- {{end}}
- value string
- variables = []common.EnvironmentVariable{
- {{range .variables}}{
- ID: "{{.ID}}",
- Description: "{{.Description}}",
- Required: {{.Required}},
- Reference: & {{.CamelID}},
- Default: "{{.Default}}",
- },{{end}}
- }
- )
-
- func init(){
- for _, variable := range variables {
- value = os.Getenv(variable.ID)
- if variable.Required && value == "" && *variable.Reference == "" {
- panic(fmt.Errorf("Environment Variable '%s' not defined! %s", variable.ID, variable.Description))
- }
-
- if value != "" {
- // remove in future
- common.Setenv(variable.ID, value)
- *variable.Reference = value
- }
- }
- }
- func InitEnvironment() error {
- fmt.Println("Environment...")
- // for _, pair := range os.Environ() {
- // fmt.Printf("\t├ %s\n",pair)
- // }
- for _, variable := range variables {
- fmt.Printf("\t├ %s: %s\n",variable.ID, *variable.Reference)
- }
- return nil
- }`)
- }
- func CreateEnvironment(p *Project) error {
- for id, environment := range p.Environment {
- environment.CamelID = UnderToCamel(id)
- }
- var (
- context = map[string]interface{}{
- "variables": p.Environment,
- }
- )
- out, _ := TemplateToString(environmentStmtsTmpl, context)
- file := G.NewFile("env")
- file.Id(out)
- return Write(p.Paths.Build("/%s/env/variables.go", p.Package), file)
- }
|