get.porter.sh/porter@v1.3.0/pkg/grpc/portergrpc/context.go (about)

     1  package portergrpc
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"get.porter.sh/porter/pkg/porter"
     8  )
     9  
    10  type ctxKey int
    11  
    12  const porterConnCtxKey ctxKey = 0
    13  
    14  // GetPorterConnectionFromContext returns the porter connection from the give context.
    15  // Use AddPorterConnectionToContext to add one.
    16  func GetPorterConnectionFromContext(ctx context.Context) (*porter.Porter, error) {
    17  	p, ok := ctx.Value(porterConnCtxKey).(*porter.Porter)
    18  	if !ok {
    19  		return nil, errors.New("Unable to find porter connection in context")
    20  	}
    21  	return p, nil
    22  }
    23  
    24  // AddPorterConnectionToContext adds the porter connection to the given context
    25  // use GetPorterConnectionFromContext to read it back out
    26  func AddPorterConnectionToContext(p *porter.Porter, ctx context.Context) context.Context {
    27  	return context.WithValue(ctx, porterConnCtxKey, p)
    28  }