| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package coordinator | 5 package coordinator |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" |
| 9 |
| 10 "github.com/luci/gae/service/info" |
| 11 "github.com/luci/luci-go/common/config" |
| 12 log "github.com/luci/luci-go/common/logging" |
| 8 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
| 9 ) | 14 ) |
| 10 | 15 |
| 11 type servicesKeyType int | 16 type servicesKeyType int |
| 12 | 17 |
| 13 // WithServices installs the supplied Services instance into a Context. | 18 // WithServices installs the supplied Services instance into a Context. |
| 14 func WithServices(c context.Context, s Services) context.Context { | 19 func WithServices(c context.Context, s Services) context.Context { |
| 15 return context.WithValue(c, servicesKeyType(0), s) | 20 return context.WithValue(c, servicesKeyType(0), s) |
| 16 } | 21 } |
| 17 | 22 |
| 18 // GetServices gets the Services instance installed in the supplied Context. | 23 // GetServices gets the Services instance installed in the supplied Context. |
| 19 // | 24 // |
| 20 // If no Services has been installed, it will panic. | 25 // If no Services has been installed, it will panic. |
| 21 func GetServices(c context.Context) Services { | 26 func GetServices(c context.Context) Services { |
| 22 s, ok := c.Value(servicesKeyType(0)).(Services) | 27 s, ok := c.Value(servicesKeyType(0)).(Services) |
| 23 if !ok { | 28 if !ok { |
| 24 panic("no Services instance is installed") | 29 panic("no Services instance is installed") |
| 25 } | 30 } |
| 26 return s | 31 return s |
| 27 } | 32 } |
| 33 |
| 34 // WithProjectNamespace sets the current namespace to the project name. |
| 35 // |
| 36 // It will fail if either the project name or the project's namespace is |
| 37 // invalid. In the event of an error, the supplied Context will be not be |
| 38 // modified or invalidated. |
| 39 func WithProjectNamespace(c *context.Context, project config.ProjectName) error
{ |
| 40 // TODO(dnj): REQUIRE this to be non-empty once namespacing is mandatory
. |
| 41 if project == "" { |
| 42 log.Debugf(*c, "Using default namespace.") |
| 43 return nil |
| 44 } |
| 45 |
| 46 if err := project.Validate(); err != nil { |
| 47 log.Fields{ |
| 48 log.ErrorKey: err, |
| 49 "project": project, |
| 50 }.Errorf(*c, "Project name is invalid.") |
| 51 return err |
| 52 } |
| 53 |
| 54 pns := ProjectNamespace(project) |
| 55 nc, err := info.Get(*c).Namespace(pns) |
| 56 if err != nil { |
| 57 log.Fields{ |
| 58 log.ErrorKey: err, |
| 59 "project": project, |
| 60 "namespace": pns, |
| 61 }.Errorf(*c, "Failed to set namespace.") |
| 62 return err |
| 63 } |
| 64 |
| 65 log.Fields{ |
| 66 "project": project, |
| 67 }.Debugf(*c, "Using project namespace.") |
| 68 *c = nc |
| 69 return nil |
| 70 } |
| 71 |
| 72 // Project returns the current project installed in the supplied Context's |
| 73 // namespace. |
| 74 // |
| 75 // This function is called with the expectation that the Context is in a |
| 76 // namespace conforming to ProjectNamespace. If this is not the case, this |
| 77 // method will panic. |
| 78 func Project(c context.Context) config.ProjectName { |
| 79 ns, _ := info.Get(c).GetNamespace() |
| 80 |
| 81 // TODO(dnj): Remove the empty namespace/project exception once we no lo
nger |
| 82 // support that. |
| 83 if ns == "" { |
| 84 return "" |
| 85 } |
| 86 |
| 87 project := ProjectFromNamespace(ns) |
| 88 if project != "" { |
| 89 return project |
| 90 } |
| 91 panic(fmt.Errorf("current namespace %q does not begin with project names
pace prefix (%q)", ns, projectNamespacePrefix)) |
| 92 } |
| OLD | NEW |