gopass/internal/out/context.go
Dominik Schulz 16c071a780
Enable golangci-lint on push and pr (#2158)
Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
2022-03-24 21:58:53 +01:00

56 lines
1.1 KiB
Go

package out
import "context"
type contextKey int
const (
ctxKeyPrefix contextKey = iota
ctxKeyNewline
)
// WithPrefix returns a context with the given prefix set.
func WithPrefix(ctx context.Context, prefix string) context.Context {
return context.WithValue(ctx, ctxKeyPrefix, prefix)
}
// AddPrefix returns a context with the given prefix added to end of the
// existing prefix.
func AddPrefix(ctx context.Context, prefix string) context.Context {
if prefix == "" {
return ctx
}
pfx := Prefix(ctx)
if pfx == "" {
return WithPrefix(ctx, prefix)
}
return WithPrefix(ctx, pfx+prefix)
}
// Prefix returns the prefix or an empty string.
func Prefix(ctx context.Context) string {
sv, ok := ctx.Value(ctxKeyPrefix).(string)
if !ok {
return ""
}
return sv
}
// WithNewline returns a context with the flag value for newline set.
func WithNewline(ctx context.Context, nl bool) context.Context {
return context.WithValue(ctx, ctxKeyNewline, nl)
}
// HasNewline returns the value of newline or the default (true).
func HasNewline(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyNewline).(bool)
if !ok {
return true
}
return bv
}