github.com/theliebeskind/genfig@v0.1.5-alpha/writers/envs.go (about)

     1  package writers
     2  
     3  import (
     4  	"io"
     5  	"text/template"
     6  )
     7  
     8  var (
     9  	envsTpl = template.Must(template.New("envs").Parse(`// Envs holds the environment-specific configurations so that
    10  // they can easily be accessed by e.g. Envs.Default
    11  var Envs = struct{ 
    12  {{range $_, $k := .Envs}}	{{$k}} Config
    13  {{end}}}{}
    14  
    15  var envMap = map[string]*Config{
    16  {{range $k, $v  := .Envs}}	"{{$k}}": &Envs.{{$v}},
    17  {{end}}}
    18  
    19  // Get returns the config matching 'env' if found, otherwie the default config.
    20  // The bool return value indicates, if a match was found (true) or the default config
    21  // is returned
    22  func Get(env string) (*Config, bool) {
    23  	if c, ok := envMap[env]; ok {
    24  		return c, true
    25  	}
    26  	return &Envs.Default, false
    27  }
    28  `))
    29  )
    30  
    31  //WriteEnvs writes
    32  func WriteEnvs(w io.Writer, envs map[string]string) error {
    33  	return envsTpl.Execute(w, struct {
    34  		Envs map[string]string
    35  	}{Envs: envs})
    36  }