44 lines
881 B
Go
44 lines
881 B
Go
package util
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/config"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
type CmdContextVal uint8
|
|
|
|
const (
|
|
CTX_CONFIG CmdContextVal = iota
|
|
CTX_LOGGER
|
|
)
|
|
|
|
func ContextWithConfig(baseCtx context.Context, config *config.ClientsConfig) context.Context {
|
|
return context.WithValue(baseCtx, CTX_CONFIG, config)
|
|
}
|
|
|
|
func ConfigFromContext(ctx context.Context) *config.ClientsConfig {
|
|
val := ctx.Value(CTX_CONFIG)
|
|
conf, ok := val.(*config.ClientsConfig)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
return conf
|
|
}
|
|
|
|
func ContextWithLogger(baseCtx context.Context, logger *zerolog.Logger) context.Context {
|
|
return context.WithValue(baseCtx, CTX_LOGGER, logger)
|
|
}
|
|
|
|
func LoggerFromContext(ctx context.Context) *zerolog.Logger {
|
|
val := ctx.Value(CTX_LOGGER)
|
|
logger, ok := val.(*zerolog.Logger)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
return logger
|
|
}
|