LEFT | RIGHT |
1 package environs | 1 package environs |
2 | 2 |
3 import "fmt" | 3 import "fmt" |
4 | 4 |
5 // New creates a new Environ using the | 5 // Open creates a new Environ using the environment configuration with the |
6 // environment configuration with the given name. | 6 // given name. If name is empty, the default environment will be used. |
7 // If name is empty, the default environment will be used. | |
8 func (envs *Environs) Open(name string) (Environ, error) { | 7 func (envs *Environs) Open(name string) (Environ, error) { |
9 if name == "" { | 8 if name == "" { |
10 name = envs.Default | 9 name = envs.Default |
| 10 if name == "" { |
| 11 return nil, fmt.Errorf("no default environment found") |
| 12 } |
| 13 } |
| 14 e, ok := envs.environs[name] |
| 15 if !ok { |
| 16 return nil, fmt.Errorf("unknown environment %q", name) |
| 17 } |
| 18 if e.err != nil { |
| 19 return nil, e.err |
| 20 } |
| 21 env, err := providers[e.kind].Open(name, e.config) |
| 22 if err != nil { |
| 23 return nil, fmt.Errorf("cannot initialize environment %q: %v", n
ame, err) |
| 24 } |
| 25 |
| 26 return env, nil |
| 27 } |
| 28 |
| 29 // NewEnviron creates a new Environ of the registered kind using the configurati
on |
| 30 // supplied. |
| 31 func NewEnviron(kind string, config map[string]interface{}) (Environ, error) { |
| 32 p, ok := providers[kind] |
| 33 if !ok { |
| 34 return nil, fmt.Errorf("no registered provider for kind: %q", ki
nd) |
| 35 } |
| 36 cfg, err := p.ConfigChecker().Coerce(config, nil) |
| 37 if err != nil { |
| 38 return nil, fmt.Errorf("error validating environment: %v", err) |
| 39 } |
| 40 |
| 41 // TODO(dfc) the name of the environment needs to be injected when it is······ |
| 42 // created from environments.yaml |
| 43 return p.Open("default", cfg) |
| 44 } |
LEFT | RIGHT |