This commit is contained in:
iratusmachina 2024-06-23 12:33:34 -04:00
parent 1b15eeefe2
commit a976a26fec
6 changed files with 128 additions and 14 deletions

View File

@ -51,4 +51,4 @@ coverage-full: test
.PHONY: coverage-html .PHONY: coverage-html
coverage-html: coverage-full coverage-html: coverage-full
go tool cover -html=./coverage/coverage.out -o ./coverage/coverage.html go tool cover -html=./coverage/coverage.out -o ./coverage/coverage.html
open ./coverage/coverage.html # open ./coverage/coverage.html

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
) )
type parsedConf struct { type parsedConf struct {
@ -19,6 +20,7 @@ type parsedConf struct {
type Config struct { type Config struct {
MappingFilePath string MappingFilePath string
MappingRules ImportRulesMappings MappingRules ImportRulesMappings
sync.Mutex
} }
type ImportRulesMappings struct { type ImportRulesMappings struct {
@ -144,6 +146,8 @@ func checkIfSizeIsConfigured(fsize string) (bool, error) {
func (c *Config) LoadMainConfigFile(fp string) (parsedConf, error) { func (c *Config) LoadMainConfigFile(fp string) (parsedConf, error) {
var conf parsedConf var conf parsedConf
var err error var err error
c.Lock()
defer c.Unlock()
ok := isFile(fp) ok := isFile(fp)
if !ok { if !ok {
// generate config file // generate config file

View File

@ -2,6 +2,8 @@ package main
import ( import (
"fmt" "fmt"
"os"
"strconv"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -124,3 +126,77 @@ func TestGenerateConfigFile(t *testing.T) {
assert.Equal(t, ok, true) assert.Equal(t, ok, true)
} }
func TestLoadMainConfigFile(t *testing.T) {
t.Run("load the main config file correctly - default load", func(t *testing.T) {
tmpDir := t.TempDir()
tempConfigDir := fmt.Sprintf("%s/tmp/config", tmpDir)
mkDirForTest(t, tempConfigDir)
hmDir, _ := os.UserConfigDir()
oldConfigFile := fmt.Sprintf("%s/gocustomurls/config.json", hmDir)
t.Log("oldConfigFile: ", oldConfigFile)
newConfigFile := fmt.Sprintf("%s/config.json", tempConfigDir)
ok := doesFileExist(oldConfigFile)
if ok {
t.Log("cp old -> new")
cpFileForTest(t, oldConfigFile, newConfigFile)
}
t.Cleanup(func() {
found := doesFileExist(newConfigFile)
if found {
t.Log("cp new -> old")
cpFileForTest(t, newConfigFile, oldConfigFile)
} else {
// Add a loop to wait until you find the old ConfigFile
ok := doesFileExist(oldConfigFile)
if ok {
t.Log("remove old")
removeFileForTest(t, oldConfigFile)
}
}
})
cfg := &Config{}
assert.Equal(t, cfg.MappingFilePath, "")
conf, err := cfg.LoadMainConfigFile("")
// time.Sleep(4 * time.Second)
assert.Equal(t, err, nil)
assert.NotEmpty(t, conf)
assert.NotEmpty(t, cfg.MappingFilePath)
})
t.Run("load the main config file correctly - load from a location", func(t *testing.T) {
t.SkipNow()
tmpDir := t.TempDir()
tempConfigDir := fmt.Sprintf("%s/tmp", tmpDir)
tempLogsDir := fmt.Sprintf("%s/tmp/logs", tmpDir)
mkDirForTest(t, tempConfigDir)
defaultConfigJson := map[string]string{
"rulesFp": fmt.Sprintf("%s/rules.json", tempConfigDir),
"logFp": fmt.Sprintf("%s/app.log", tempLogsDir),
"Port": "9005",
"Compression": strconv.FormatBool(false),
"SizeToRotate": "50MB",
}
writeJsonForTest(t, defaultConfigJson, fmt.Sprintf("%s/confg.json", tempConfigDir))
cfg := &Config{}
assert.Equal(t, cfg.MappingFilePath, "")
conf, err := cfg.LoadMainConfigFile(fmt.Sprintf("%s/confg.json", tempConfigDir))
assert.Equal(t, err, nil)
assert.NotEmpty(t, conf)
assert.NotEmpty(t, cfg.MappingFilePath)
})
}

View File

@ -139,11 +139,12 @@ func compressOldFile(fname string) error {
return nil return nil
} }
func (lf *LogFile) rotate() error { func (lf *LogFile) rotate(canCompress bool) error {
lf.fileLock.Lock() lf.fileLock.Lock()
defer lf.fileLock.Unlock() defer lf.fileLock.Unlock()
prefix := fmt.Sprintf("%s.%s", lf.handle.Name(), time.Now().Format("2006-01-02")) // new file
newFilePrefix := fmt.Sprintf("%s.%s", lf.handle.Name(), time.Now().Format("2006-01-02"))
// close file to allow for read-only access // close file to allow for read-only access
err := lf.handle.Close() err := lf.handle.Close()
@ -152,15 +153,17 @@ func (lf *LogFile) rotate() error {
} }
// make a copy of the old log file // make a copy of the old log file
err = lf.makeCopyTo(prefix) err = lf.makeCopyTo(newFilePrefix)
if err != nil { if err != nil {
return err return err
} }
// compress the new log file // compress the new log file
err = compressOldFile(prefix) if canCompress {
if err != nil { err = compressOldFile(newFilePrefix)
return err if err != nil {
return err
}
} }
// truncate the old log file // truncate the old log file
@ -178,7 +181,7 @@ func (lf *LogFile) rotate() error {
return nil return nil
} }
func (lf *LogFile) open(maxSize string) error { func (lf *LogFile) open(maxSize string, canCompress bool) error {
f, err := os.OpenFile(lf.path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) f, err := os.OpenFile(lf.path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil { if err != nil {
return err return err
@ -188,8 +191,8 @@ func (lf *LogFile) open(maxSize string) error {
return err return err
} }
curSize := prettyByteSize(finfo.Size()) curSize := prettyByteSize(finfo.Size())
if curSize > maxSize { if len(strings.TrimSpace(maxSize)) != 0 && curSize > maxSize {
err = lf.rotate() err = lf.rotate(canCompress)
if err != nil { if err != nil {
return err return err
} }
@ -199,7 +202,7 @@ func (lf *LogFile) open(maxSize string) error {
return nil return nil
} }
func newFileLogger(path string, maxSize string) (*LogFile, error) { func newFileLogger(path string, maxSize string, canCompress bool) (*LogFile, error) {
requestedFile := filepath.Clean(filepath.Join("/", path)) requestedFile := filepath.Clean(filepath.Join("/", path))
parentDir := filepath.Dir(requestedFile) parentDir := filepath.Dir(requestedFile)
err := os.MkdirAll(parentDir, 0755) err := os.MkdirAll(parentDir, 0755)
@ -209,7 +212,7 @@ func newFileLogger(path string, maxSize string) (*LogFile, error) {
lf := &LogFile{ lf := &LogFile{
path: path, path: path,
} }
err = lf.open(maxSize) err = lf.open(maxSize, canCompress)
return lf, err return lf, err
// f, err := os.OpenFile(requestedFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) // f, err := os.OpenFile(requestedFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
// if err != nil { // if err != nil {

View File

@ -120,7 +120,7 @@ func main() {
errorLog.Println(err) errorLog.Println(err)
os.Exit(1) os.Exit(1)
} }
l, err := newFileLogger(pConf.LogFp, pConf.SizeToRotate) l, err := newFileLogger(pConf.LogFp, pConf.SizeToRotate, pConf.Compression)
if err != nil { if err != nil {
errorLog.Println(err) errorLog.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -1,7 +1,10 @@
package main package main
import ( import (
"encoding/json"
"errors"
"io" "io"
"io/fs"
"os" "os"
"testing" "testing"
) )
@ -20,8 +23,16 @@ func cpFileForTest(t *testing.T, src string, dst string) {
var srcinfo os.FileInfo var srcinfo os.FileInfo
srcfd, err = os.Open(src) srcfd, err = os.Open(src)
if err != nil { if err != nil {
t.Fatal(err) if errors.Is(err, fs.ErrNotExist) {
return
} else {
t.Fatal(err)
}
} }
// if err != nil {
// t.Fatal(err)
// }
defer srcfd.Close() defer srcfd.Close()
dstfd, err = os.Create(dst) dstfd, err = os.Create(dst)
if err != nil { if err != nil {
@ -63,3 +74,23 @@ func IsDirEmpty(t *testing.T, name string) bool {
// and if the file is EOF... well, the dir is empty. // and if the file is EOF... well, the dir is empty.
return err == io.EOF return err == io.EOF
} }
func doesFileExist(name string) bool {
_, err := os.Stat(name)
return !errors.Is(err, fs.ErrNotExist)
}
func removeFileForTest(t *testing.T, name string) {
err := os.Remove(name)
if err != nil {
t.Fatal(err)
}
}
func writeJsonForTest(t *testing.T, data map[string]string, fp string) {
jsonString, _ := json.Marshal(data)
err := os.WriteFile(fp, jsonString, os.ModePerm)
if err != nil {
t.Fatal(err)
}
}