harness-drone/handler/api/request/context.go

55 lines
1.5 KiB
Go
Raw Normal View History

2019-02-19 23:56:41 +00:00
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
package request
// https://github.com/kubernetes/apiserver/blob/master/pkg/endpoints/request/context.go
import (
"context"
"github.com/drone/drone/core"
)
type key int
const (
userKey key = iota
permKey
repoKey
)
// WithUser returns a copy of parent in which the user value is set
func WithUser(parent context.Context, user *core.User) context.Context {
return context.WithValue(parent, userKey, user)
}
// UserFrom returns the value of the user key on the ctx
func UserFrom(ctx context.Context) (*core.User, bool) {
user, ok := ctx.Value(userKey).(*core.User)
return user, ok
}
// WithPerm returns a copy of parent in which the perm value is set
func WithPerm(parent context.Context, perm *core.Perm) context.Context {
return context.WithValue(parent, permKey, perm)
}
// PermFrom returns the value of the perm key on the ctx
func PermFrom(ctx context.Context) (*core.Perm, bool) {
perm, ok := ctx.Value(permKey).(*core.Perm)
return perm, ok
}
// WithRepo returns a copy of parent in which the repo value is set
func WithRepo(parent context.Context, repo *core.Repository) context.Context {
return context.WithValue(parent, repoKey, repo)
}
// RepoFrom returns the value of the repo key on the ctx
func RepoFrom(ctx context.Context) (*core.Repository, bool) {
repo, ok := ctx.Value(repoKey).(*core.Repository)
return repo, ok
}