Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1402)

Side by Side Diff: environs/open.go

Issue 13421050: environs: add Environs.Config method
Patch Set: environs: add Environs.Config method Created 11 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « environs/config_test.go ('k') | environs/open_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011, 2012, 2013 Canonical Ltd. 1 // Copyright 2011, 2012, 2013 Canonical Ltd.
2 // Licensed under the AGPLv3, see LICENCE file for details. 2 // Licensed under the AGPLv3, see LICENCE file for details.
3 3
4 package environs 4 package environs
5 5
6 import ( 6 import (
7 "fmt" 7 "fmt"
8 "io/ioutil" 8 "io/ioutil"
9 9
10 "launchpad.net/juju-core/environs/config" 10 "launchpad.net/juju-core/environs/config"
11 "launchpad.net/juju-core/errors" 11 "launchpad.net/juju-core/errors"
12 ) 12 )
13 13
14 var InvalidEnvironmentError error = fmt.Errorf( 14 var InvalidEnvironmentError = fmt.Errorf("environment is not a juju-core environ ment")
15 » "environment is not a juju-core environment")
16 15
17 // Open creates a new Environ using the environment configuration with the 16 // ConfigForName returns the configuration for the environment with the
18 // given name. If name is empty, the default environment will be used. 17 // given name from the default environments file. If the name is blank,
19 func (envs *Environs) Open(name string) (Environ, error) { 18 // the default environment will be used. If the configuration is not
20 » if name == "" { 19 // found, an errors.NotFoundError is returned.
21 » » name = envs.Default
22 » » if name == "" {
23 » » » return nil, fmt.Errorf("no default environment found")
24 » » }
25 » }
26 » e, ok := envs.environs[name]
27 » if !ok {
28 » » return nil, fmt.Errorf("unknown environment %q", name)
29 » }
30 » if e.err != nil {
31 » » return nil, e.err
32 » }
33 » return New(e.config)
34 }
35
36 // ConfigForName returns the configuration for the
37 // environment with the given name from the default
38 // environments file. If the name is blank, the default
39 // environment will be used.
40 func ConfigForName(name string) (*config.Config, error) { 20 func ConfigForName(name string) (*config.Config, error) {
41 envs, err := ReadEnvirons("") 21 envs, err := ReadEnvirons("")
42 if err != nil { 22 if err != nil {
43 return nil, err 23 return nil, err
44 } 24 }
45 » if name == "" { 25 » return envs.Config(name)
46 » » name = envs.Default
47 » » if name == "" {
48 » » » return nil, fmt.Errorf("no default environment found")
49 » » }
50 » }
51 » e, ok := envs.environs[name]
52 » if !ok {
53 » » return nil, fmt.Errorf("unknown environment %q", name)
54 » }
55 » if e.err != nil {
56 » » return nil, e.err
57 » }
58 » return e.config, nil
59 } 26 }
60 27
61 // NewFromName opens the environment with the given 28 // NewFromName opens the environment with the given
62 // name from the default environments file. If the 29 // name from the default environments file. If the
63 // name is blank, the default environment will be used. 30 // name is blank, the default environment will be used.
64 func NewFromName(name string) (Environ, error) { 31 func NewFromName(name string) (Environ, error) {
65 cfg, err := ConfigForName(name) 32 cfg, err := ConfigForName(name)
66 if err != nil { 33 if err != nil {
67 return nil, err 34 return nil, err
68 } 35 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 return nil 92 return nil
126 } else if err != nil { 93 } else if err != nil {
127 return err 94 return err
128 } else if content, err := ioutil.ReadAll(reader); err != nil { 95 } else if content, err := ioutil.ReadAll(reader); err != nil {
129 return err 96 return err
130 } else if string(content) != verificationContent { 97 } else if string(content) != verificationContent {
131 return InvalidEnvironmentError 98 return InvalidEnvironmentError
132 } 99 }
133 return nil 100 return nil
134 } 101 }
OLDNEW
« no previous file with comments | « environs/config_test.go ('k') | environs/open_test.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b