11package config
22
33import (
4+ "fmt"
45 "os"
56 "path/filepath"
67 "strings"
@@ -12,7 +13,7 @@ import (
1213)
1314
1415var (
15- DefaultConfig = `timestamps:
16+ defaultConfig = `timestamps:
1617
1718 # Example: {"level":"error","error":"context deadline exceeded","time":1744570895480541,"caller":"server.go:462"}
1819 - format: epochany
@@ -164,6 +165,8 @@ var (
164165 pattern: |
165166 /Date\((\d+)\)
166167`
168+
169+ windowConfig = `window: %s`
167170)
168171
169172type NotificationWebhook struct {
@@ -193,7 +196,39 @@ type Regex struct {
193196 Format string `yaml:"format"`
194197}
195198
196- func LoadConfig (dir , file string ) (* Config , error ) {
199+ type OptT func (* optsT )
200+
201+ type optsT struct {
202+ window time.Duration
203+ }
204+
205+ func WithWindow (window time.Duration ) func (* optsT ) {
206+ return func (o * optsT ) {
207+ o .window = window
208+ }
209+ }
210+
211+ func parseOpts (opts ... OptT ) * optsT {
212+ o := & optsT {}
213+ for _ , opt := range opts {
214+ opt (o )
215+ }
216+ return o
217+ }
218+
219+ func Marshal (opts ... OptT ) string {
220+ o := parseOpts (opts ... )
221+
222+ if o .window > 0 {
223+ wcfg := fmt .Sprintf (windowConfig , o .window )
224+ return fmt .Sprintf ("%s\n %s\n " , defaultConfig , wcfg )
225+ }
226+
227+ return defaultConfig
228+ }
229+
230+ func LoadConfig (dir , file string , opts ... OptT ) (* Config , error ) {
231+
197232 var config Config
198233
199234 if _ , err := os .Stat (dir ); os .IsNotExist (err ) {
@@ -203,7 +238,7 @@ func LoadConfig(dir, file string) (*Config, error) {
203238 }
204239
205240 if _ , err := os .Stat (filepath .Join (dir , file )); os .IsNotExist (err ) {
206- if err := WriteDefaultConfig (filepath .Join (dir , file )); err != nil {
241+ if err := WriteDefaultConfig (filepath .Join (dir , file ), opts ... ); err != nil {
207242 log .Error ().Err (err ).Msg ("Failed to write default config" )
208243 return nil , err
209244 }
@@ -221,8 +256,9 @@ func LoadConfig(dir, file string) (*Config, error) {
221256 return & config , nil
222257}
223258
224- func WriteDefaultConfig (path string ) error {
225- return os .WriteFile (path , []byte (DefaultConfig ), 0644 )
259+ func WriteDefaultConfig (path string , opts ... OptT ) error {
260+ cfg := Marshal (opts ... )
261+ return os .WriteFile (path , []byte (cfg ), 0644 )
226262}
227263
228264func LoadConfigFromBytes (data string ) (* Config , error ) {
0 commit comments