Files
cunicu/pkg/config/types.go
Steffen Vogel a8f67be889 ci: use golangci-lint for linting
Signed-off-by: Steffen Vogel <post@steffenvogel.de>
2022-12-07 23:12:05 +01:00

112 lines
1.7 KiB
Go

package config
import (
"errors"
"fmt"
"net/url"
"strings"
"go.uber.org/zap/zapcore"
)
type BackendURL struct {
url.URL
}
func (u *BackendURL) UnmarshalText(text []byte) error {
str := string(text)
if !strings.Contains(str, ":") {
str += ":"
}
up, err := url.Parse(str)
if err != nil {
return err
}
u.URL = *up
return nil
}
func (u BackendURL) MarshalText() ([]byte, error) {
s := u.String()
if s[len(s)-1] == ':' {
s = s[:len(s)-1]
}
return []byte(s), nil
}
type URL struct {
url.URL
}
func (u *URL) UnmarshalText(text []byte) error {
up, err := url.Parse(string(text))
if err != nil {
return err
}
u.URL = *up
return nil
}
func (u URL) MarshalText() ([]byte, error) {
s := u.String()
return []byte(s), nil
}
type OutputFormat string
const (
OutputFormatJSON OutputFormat = "json"
OutputFormatLogger OutputFormat = "logger"
OutputFormatHuman OutputFormat = "human"
)
//nolint:gochecknoglobals
var OutputFormats = []OutputFormat{
OutputFormatJSON,
OutputFormatLogger,
OutputFormatHuman,
}
var errUnknownFormat = errors.New("unknown output format")
func (f *OutputFormat) UnmarshalText(text []byte) error {
*f = OutputFormat(text)
switch *f {
case OutputFormatJSON, OutputFormatLogger, OutputFormatHuman:
return nil
}
return fmt.Errorf("%w: %s", errUnknownFormat, string(text))
}
func (f OutputFormat) MarshalText() ([]byte, error) {
return []byte(f), nil
}
func (f OutputFormat) String() string {
return string(f)
}
func (f *OutputFormat) Set(str string) error {
return f.UnmarshalText([]byte(str))
}
func (f *OutputFormat) Type() string {
return "string"
}
type Level struct {
zapcore.Level
}
func (l *Level) Type() string {
return "string"
}