frame out cli tool
This commit is contained in:
43
cmd/internal/util/context.go
Normal file
43
cmd/internal/util/context.go
Normal file
@@ -0,0 +1,43 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user