Some checks failed
Docker image builds / build (push) Waiting to run
Asset Build Verification / asset_verification (push) Has been cancelled
Docs deploy / build (push) Has been cancelled
Go Mod Tidy Check / go_mod_tidy_check (push) Has been cancelled
Go / go_tests (push) Has been cancelled
Package builds (unstable) / package_builds (push) Has been cancelled
Smoke tests / smoke-test (default-config-macro) (push) Has been cancelled
Smoke tests / smoke-test (docker-registry) (push) Has been cancelled
Smoke tests / smoke-test (double_slash) (push) Has been cancelled
Smoke tests / smoke-test (forced-language) (push) Has been cancelled
Smoke tests / smoke-test (git-clone) (push) Has been cancelled
Smoke tests / smoke-test (git-push) (push) Has been cancelled
Smoke tests / smoke-test (healthcheck) (push) Has been cancelled
Smoke tests / smoke-test (i18n) (push) Has been cancelled
Smoke tests / smoke-test (log-file) (push) Has been cancelled
Smoke tests / smoke-test (nginx) (push) Has been cancelled
Smoke tests / smoke-test (palemoon/amd64) (push) Has been cancelled
Smoke tests / smoke-test (robots_txt) (push) Has been cancelled
Check Spelling / Check Spelling (push) Has been cancelled
SSH CI / ssh (aarch64-16k) (push) Has been cancelled
SSH CI / ssh (aarch64-4k) (push) Has been cancelled
SSH CI / ssh (ppc64le) (push) Has been cancelled
SSH CI / ssh (riscv64) (push) Has been cancelled
zizmor / zizmor latest via PyPI (push) Has been cancelled
124 lines
2.8 KiB
Go
124 lines
2.8 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
)
|
|
|
|
var (
|
|
ErrMissingLoggingFileConfig = errors.New("config.Logging: missing value parameters in logging block")
|
|
ErrInvalidLoggingSink = errors.New("config.Logging: invalid sink")
|
|
ErrInvalidLoggingFileConfig = errors.New("config.LoggingFileConfig: invalid parameters")
|
|
ErrOutOfRange = errors.New("config: error out of range")
|
|
)
|
|
|
|
type Logging struct {
|
|
Sink string `json:"sink"` // Logging sink, either "stdio" or "file"
|
|
Level *slog.Level `json:"level"` // Log level, if set supersedes the level in flags
|
|
Parameters *LoggingFileConfig `json:"parameters"` // Logging parameters, to be dynamic in the future
|
|
}
|
|
|
|
const (
|
|
LogSinkStdio = "stdio"
|
|
LogSinkFile = "file"
|
|
)
|
|
|
|
func (l *Logging) Valid() error {
|
|
var errs []error
|
|
|
|
switch l.Sink {
|
|
case LogSinkStdio:
|
|
// no validation needed
|
|
case LogSinkFile:
|
|
if l.Parameters == nil {
|
|
errs = append(errs, ErrMissingLoggingFileConfig)
|
|
}
|
|
|
|
if err := l.Parameters.Valid(); err != nil {
|
|
errs = append(errs, err)
|
|
}
|
|
default:
|
|
errs = append(errs, fmt.Errorf("%w: sink %s is unknown to me", ErrInvalidLoggingSink, l.Sink))
|
|
}
|
|
|
|
if len(errs) != 0 {
|
|
return errors.Join(errs...)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (Logging) Default() *Logging {
|
|
return &Logging{
|
|
Sink: "stdio",
|
|
}
|
|
}
|
|
|
|
type LoggingFileConfig struct {
|
|
Filename string `json:"file"`
|
|
MaxBackups int `json:"maxBackups"`
|
|
MaxBytes int64 `json:"maxBytes"`
|
|
MaxAge int `json:"maxAge"`
|
|
Compress bool `json:"compress"`
|
|
UseLocalTime bool `json:"useLocalTime"`
|
|
}
|
|
|
|
func (lfc *LoggingFileConfig) Valid() error {
|
|
if lfc == nil {
|
|
return fmt.Errorf("logging file config is nil, why are you calling this?")
|
|
}
|
|
|
|
var errs []error
|
|
|
|
if lfc.Zero() {
|
|
errs = append(errs, ErrMissingValue)
|
|
}
|
|
|
|
if lfc.Filename == "" {
|
|
errs = append(errs, fmt.Errorf("%w: filename", ErrMissingValue))
|
|
}
|
|
|
|
if lfc.MaxBackups < 0 {
|
|
errs = append(errs, fmt.Errorf("%w: max backup count %d is not greater than or equal to zero", ErrOutOfRange, lfc.MaxBackups))
|
|
}
|
|
|
|
if lfc.MaxAge < 0 {
|
|
errs = append(errs, fmt.Errorf("%w: max backup count %d is not greater than or equal to zero", ErrOutOfRange, lfc.MaxAge))
|
|
}
|
|
|
|
if len(errs) != 0 {
|
|
errs = append([]error{ErrInvalidLoggingFileConfig}, errs...)
|
|
return errors.Join(errs...)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (lfc LoggingFileConfig) Zero() bool {
|
|
for _, cond := range []bool{
|
|
lfc.Filename != "",
|
|
lfc.MaxBackups != 0,
|
|
lfc.MaxBytes != 0,
|
|
lfc.MaxAge != 0,
|
|
lfc.Compress,
|
|
lfc.UseLocalTime,
|
|
} {
|
|
if cond {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (LoggingFileConfig) Default() *LoggingFileConfig {
|
|
return &LoggingFileConfig{
|
|
Filename: "./var/nuke.log",
|
|
MaxBackups: 3,
|
|
MaxBytes: 104857600, // 100 Mi
|
|
MaxAge: 7, // 7 days
|
|
Compress: true,
|
|
UseLocalTime: false,
|
|
}
|
|
}
|