From 19a84b77fbb4e8c3c4475a2892b5cdd18f9450dc Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 3 May 2023 13:14:27 +0200 Subject: [PATCH] Adding app/log/config.go --- app/log/config.go | 27 ++++++++++++++ app/log/config_test.go | 84 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 app/log/config.go create mode 100644 app/log/config_test.go diff --git a/app/log/config.go b/app/log/config.go new file mode 100644 index 0000000..54c3b55 --- /dev/null +++ b/app/log/config.go @@ -0,0 +1,27 @@ +package log + +import ( + "path" + "time" + + "github.com/eosswedenorg/thalos/app/types" +) + +type Config struct { + Filename string `yaml:"filename"` + Directory string `yaml:"directory"` + MaxFileSize types.Size `yaml:"maxfilesize"` + MaxTime time.Duration `yaml:"maxtime"` +} + +func (c Config) GetFilename() string { + return path.Base(c.Filename) +} + +func (c Config) GetDirectory() string { + return path.Clean(c.Directory) +} + +func (c Config) GetFilePath() string { + return path.Join(c.GetDirectory(), c.GetFilename()) +} diff --git a/app/log/config_test.go b/app/log/config_test.go new file mode 100644 index 0000000..e3ad7ac --- /dev/null +++ b/app/log/config_test.go @@ -0,0 +1,84 @@ +package log + +import ( + "testing" +) + +func TestConfig_GetDirectory(t *testing.T) { + tests := []struct { + name string + directory string + want string + }{ + {"empty", "", "."}, + {"root", "/", "/"}, + {"one", "dir", "dir"}, + {"path", "/path/to/some/directory", "/path/to/some/directory"}, + {"relative", "relative/directory", "relative/directory"}, + {"backtrace", "/path/./to/some/../directory", "/path/to/directory"}, + {"multislash", "//path/to///directory//", "/path/to/directory"}, + {"everything", "path/to/..//./from/directory//", "path/from/directory"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := Config{ + Directory: tt.directory, + } + if got := c.GetDirectory(); got != tt.want { + t.Errorf("Config.GetDirectory() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestConfig_GetFilename(t *testing.T) { + tests := []struct { + name string + filename string + want string + }{ + {"empty", "", "."}, + {"name", "some_file.txt", "some_file.txt"}, + {"path", "/path/to/my.log", "my.log"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := Config{ + Filename: tt.filename, + } + if got := c.GetFilename(); got != tt.want { + t.Errorf("Config.GetFilename() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestConfig_GetFilePath(t *testing.T) { + tests := []struct { + name string + filename string + directory string + want string + }{ + {"empty", "", "", "."}, + {"directory", "", "dir", "dir"}, + {"filename", "filename", "", "filename"}, + {"both", "filename", "dir", "dir/filename"}, + {"root", "filename", "/", "/filename"}, + {"abs", "filename", "/path/to/logs", "/path/to/logs/filename"}, + {"relative", "filename", "/srv/../log", "/log/filename"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := Config{ + Filename: tt.filename, + Directory: tt.directory, + } + if got := c.GetFilePath(); got != tt.want { + t.Errorf("Config.GetFilePath() = %v, want %v", got, tt.want) + } + }) + } +}