blob: 2e67a827eb46e31c149731a6126cd78af8ab4916 [file]
// Copyright 2015 The LUCI Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package coordinator
import (
"strings"
"github.com/luci/gae/service/info"
"github.com/luci/luci-go/logdog/api/config/svcconfig"
"github.com/luci/luci-go/luci_config/common/cfgtypes"
"golang.org/x/net/context"
)
const (
// projectNamespacePrefix is the datastore namespace prefix for project
// namespaces.
projectNamespacePrefix = "luci."
)
// ProjectNamespace returns the AppEngine namespace for a given luci-config
// project name.
func ProjectNamespace(project cfgtypes.ProjectName) string {
return projectNamespacePrefix + string(project)
}
// ProjectFromNamespace returns the current project installed in the supplied
// Context's namespace.
//
// If the namespace does not have a project namespace prefix, this function
// will return an empty string.
func ProjectFromNamespace(ns string) cfgtypes.ProjectName {
if !strings.HasPrefix(ns, projectNamespacePrefix) {
return ""
}
return cfgtypes.ProjectName(ns[len(projectNamespacePrefix):])
}
// CurrentProject returns the current project based on the currently-loaded
// namespace.
//
// If there is no current namespace, or if the current namespace is not a valid
// project namespace, an empty string will be returned.
func CurrentProject(c context.Context) cfgtypes.ProjectName {
if ns := info.GetNamespace(c); ns != "" {
return ProjectFromNamespace(ns)
}
return ""
}
// CurrentProjectConfig returns the project-specific configuration for the
// current project.
//
// If there is no current project namespace, or if the current project has no
// configuration, config.ErrInvalidConfig will be returned.
func CurrentProjectConfig(c context.Context) (*svcconfig.ProjectConfig, error) {
return GetServices(c).ProjectConfig(c, CurrentProject(c))
}