| 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 "strings" | 8 "strings" |
| 9 | 9 |
| 10 "github.com/luci/gae/service/datastore/meta" | 10 "github.com/luci/gae/service/datastore/meta" |
| 11 "github.com/luci/gae/service/info" |
| 11 "github.com/luci/luci-go/common/config" | 12 "github.com/luci/luci-go/common/config" |
| 13 "github.com/luci/luci-go/common/proto/logdog/svcconfig" |
| 12 "golang.org/x/net/context" | 14 "golang.org/x/net/context" |
| 13 ) | 15 ) |
| 14 | 16 |
| 15 // projectNamespacePrefix is the datastore namespace prefix for project | 17 // projectNamespacePrefix is the datastore namespace prefix for project |
| 16 // namespaces. | 18 // namespaces. |
| 17 const projectNamespacePrefix = "luci." | 19 const projectNamespacePrefix = "luci." |
| 18 | 20 |
| 19 // ProjectNamespace returns the AppEngine namespace for a given luci-config | 21 // ProjectNamespace returns the AppEngine namespace for a given luci-config |
| 20 // project name. | 22 // project name. |
| 21 func ProjectNamespace(project config.ProjectName) string { | 23 func ProjectNamespace(project config.ProjectName) string { |
| 22 return projectNamespacePrefix + string(project) | 24 return projectNamespacePrefix + string(project) |
| 23 } | 25 } |
| 24 | 26 |
| 25 // ProjectFromNamespace returns the current project installed in the supplied | 27 // ProjectFromNamespace returns the current project installed in the supplied |
| 26 // Context's namespace. | 28 // Context's namespace. |
| 27 // | 29 // |
| 28 // If the namespace does not have a project namespace prefix, this function | 30 // If the namespace does not have a project namespace prefix, this function |
| 29 // will return an empty string. | 31 // will return an empty string. |
| 30 func ProjectFromNamespace(ns string) config.ProjectName { | 32 func ProjectFromNamespace(ns string) config.ProjectName { |
| 31 if !strings.HasPrefix(ns, projectNamespacePrefix) { | 33 if !strings.HasPrefix(ns, projectNamespacePrefix) { |
| 32 return "" | 34 return "" |
| 33 } | 35 } |
| 34 return config.ProjectName(ns[len(projectNamespacePrefix):]) | 36 return config.ProjectName(ns[len(projectNamespacePrefix):]) |
| 35 } | 37 } |
| 36 | 38 |
| 39 // CurrentProject returns the current project based on the currently-loaded |
| 40 // namespace. |
| 41 // |
| 42 // If there is no current namespace, or if the current namespace is not a valid |
| 43 // project namespace, an empty string will be returned. |
| 44 func CurrentProject(c context.Context) config.ProjectName { |
| 45 if ns, ok := info.Get(c).GetNamespace(); ok { |
| 46 return ProjectFromNamespace(ns) |
| 47 } |
| 48 return "" |
| 49 } |
| 50 |
| 51 // CurrentProjectConfig returns the project-specific configuration for the |
| 52 // current project. |
| 53 // |
| 54 // If there is no current project namespace, or if the current project has no |
| 55 // configuration, config.ErrInvalidConfig will be returned. |
| 56 func CurrentProjectConfig(c context.Context) (*svcconfig.ProjectConfig, error) { |
| 57 return GetServices(c).ProjectConfig(c, CurrentProject(c)) |
| 58 } |
| 59 |
| 37 // AllProjectsWithNamespaces scans current namespaces and returns those that | 60 // AllProjectsWithNamespaces scans current namespaces and returns those that |
| 38 // belong to LUCI projects. | 61 // belong to LUCI projects. |
| 39 func AllProjectsWithNamespaces(c context.Context) ([]config.ProjectName, error)
{ | 62 func AllProjectsWithNamespaces(c context.Context) ([]config.ProjectName, error)
{ |
| 40 var projects []config.ProjectName | 63 var projects []config.ProjectName |
| 41 err := meta.NamespacesWithPrefix(c, projectNamespacePrefix, func(ns stri
ng) error { | 64 err := meta.NamespacesWithPrefix(c, projectNamespacePrefix, func(ns stri
ng) error { |
| 42 if proj := ProjectFromNamespace(ns); proj != "" { | 65 if proj := ProjectFromNamespace(ns); proj != "" { |
| 43 projects = append(projects, proj) | 66 projects = append(projects, proj) |
| 44 } | 67 } |
| 45 return nil | 68 return nil |
| 46 }) | 69 }) |
| 47 if err != nil { | 70 if err != nil { |
| 48 return nil, err | 71 return nil, err |
| 49 } | 72 } |
| 50 return projects, nil | 73 return projects, nil |
| 51 } | 74 } |
| OLD | NEW |