2016-09-26 17:14:39 +00:00
|
|
|
// Package host enables setting and reading
|
|
|
|
|
// the current host from context
|
|
|
|
|
package host
|
|
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2016-09-26 17:14:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type key int
|
|
|
|
|
|
|
|
|
|
const hostKey key = 0
|
|
|
|
|
|
|
|
|
|
// NewContext returns a new context carrying the current osquery host.
|
2022-01-18 01:52:09 +00:00
|
|
|
func NewContext(ctx context.Context, host *fleet.Host) context.Context {
|
2016-09-26 17:14:39 +00:00
|
|
|
return context.WithValue(ctx, hostKey, host)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FromContext extracts the osquery host from context if present.
|
2022-01-18 01:52:09 +00:00
|
|
|
func FromContext(ctx context.Context) (*fleet.Host, bool) {
|
|
|
|
|
host, ok := ctx.Value(hostKey).(*fleet.Host)
|
2016-09-26 17:14:39 +00:00
|
|
|
return host, ok
|
|
|
|
|
}
|