-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restructuring code, initial changes for rotate functionality, unit tests
- Loading branch information
Showing
9 changed files
with
475 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type LogEntry struct { | ||
Path string `yaml:"path"` | ||
Type string `yaml:"type"` | ||
Schedule string `yaml:"schedule,omitempty"` | ||
Size string `yaml:"size,omitempty"` | ||
MaxKeep int `yaml:"max_keep,omitempty"` | ||
Condition *Condition `yaml:"condition,omitempty"` | ||
} | ||
|
||
type Condition struct { | ||
Age string `yaml:"age,omitempty"` | ||
Age *string `yaml:"age,omitempty"` | ||
MaxKeep *int `yaml:"max_keep,omitempty"` | ||
Size *string `yaml:"size,omitempty"` | ||
Compress *bool `yaml:"compress,omitempty"` | ||
} | ||
|
||
type Config struct { | ||
Logs []LogEntry `yaml:"logs"` | ||
Schedule string `yaml:"schedule"` | ||
} | ||
|
||
func (entry *LogEntry) setDefaults() { | ||
if entry.Type == "rotate" && entry.Condition == nil { | ||
entry.Condition = &Condition{} | ||
|
||
if entry.Condition.Compress == nil { | ||
defaultCompress := true | ||
entry.Condition.Compress = &defaultCompress | ||
} | ||
} | ||
} | ||
|
||
func loadConfig(filePath string) (Config, error) { | ||
yamlFile, err := os.ReadFile(filepath.Clean(filePath)) | ||
if err != nil { | ||
return Config{}, fmt.Errorf("failed to read YAML file: %v", err) | ||
} | ||
|
||
var config Config | ||
err = yaml.Unmarshal(yamlFile, &config) | ||
if err != nil { | ||
return Config{}, fmt.Errorf("failed to parse YAML file: %v", err) | ||
} | ||
|
||
for i := range config.Logs { | ||
config.Logs[i].setDefaults() | ||
} | ||
|
||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.