Skip to content

Commit

Permalink
restructuring code, initial changes for rotate functionality, unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PAKalucki committed Sep 11, 2024
1 parent 6f6939b commit e4c89b5
Show file tree
Hide file tree
Showing 9 changed files with 475 additions and 240 deletions.
46 changes: 42 additions & 4 deletions config.go
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
}
10 changes: 6 additions & 4 deletions configs/wingologrotate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ logs:
# condition:
# size: "30MB"

# - path: "/tmp/test/logs/compress/*.log"
# type: compress
# condition:
# max_keep: 5
- path: "/tmp/test/logs/compress/*.log"
type: rotate
condition:
max_keep: 5 // if missing and type rotate it should be default 3
size: "30MB" // required
compress: true // if missing and type rotate it should be default true

# - path: "/tmp/test/logs/rotate/*.log"
# type: compress
Expand Down
86 changes: 0 additions & 86 deletions install.go

This file was deleted.

45 changes: 45 additions & 0 deletions logrotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"log"
"os"
"path/filepath"
"time"

"github.com/robfig/cron/v3"
)
Expand Down Expand Up @@ -45,3 +47,46 @@ func runLogRotation() {
select {}

}

func createTask(logEntry LogEntry) func() {
return func() {
log.Printf("Running task for path: %s", logEntry.Path)
matchingFiles, err := filepath.Glob(filepath.Clean(logEntry.Path))
if err != nil {
log.Printf("Failed to expand wildcard for path %s: %v", logEntry.Path, err)
return
}

if logEntry.Type == "delete" {
for _, file := range matchingFiles {
if logEntry.Condition != nil && logEntry.Condition.Age != nil {
ageDuration, err := parseDuration(*logEntry.Condition.Age)
if err != nil {
log.Printf("Invalid age format for file %s: %v", file, err)
continue
}

fileInfo, err := os.Stat(file)
if err != nil {
log.Printf("Failed to get file info for %s: %v", file, err)
continue
}
fileAge := time.Since(fileInfo.ModTime())

if fileAge < ageDuration {
// log.Printf("Skipping file %s, does not meet age condition (%s)", file, logEntry.Condition.Age)
continue
}
}

log.Printf("Deleting file: %s", file)
err := os.Remove(file)
if err != nil {
log.Printf("Failed to delete file %s: %v", file, err)
} else {
log.Printf("Successfully deleted file: %s", file)
}
}
}
}
}
41 changes: 0 additions & 41 deletions main_test.go

This file was deleted.

56 changes: 0 additions & 56 deletions manage.go

This file was deleted.

Loading

0 comments on commit e4c89b5

Please sign in to comment.