diff --git a/config/default.go b/config/default.go index 04aacf2..461f5c3 100644 --- a/config/default.go +++ b/config/default.go @@ -17,6 +17,8 @@ path = "%s" ext = "txt" # Whether to look in sub-folders recurse = true +# Whether to use a dot as a folder separator in log names +dotFolder = true # config for output types [output] diff --git a/config/types.go b/config/types.go index b218509..a3a3681 100644 --- a/config/types.go +++ b/config/types.go @@ -6,9 +6,10 @@ type Config struct { } type Input struct { - Path string `env:"LOG_PATH"` - Recurse bool `env:"LOG_RECURSE"` - Ext string `env:"LOG_EXT"` + Path string `env:"LOG_PATH"` + Recurse bool `env:"LOG_RECURSE"` + Ext string `env:"LOG_EXT"` + DotFolder bool `env:"LOG_DOT_FOLDER"` } type Outputs map[string]Output diff --git a/files/append.go b/files/append.go index 4600f4d..bd9f4ee 100644 --- a/files/append.go +++ b/files/append.go @@ -16,7 +16,14 @@ func Append(l models.Log) error { return err } - filename := fmt.Sprintf("%s.%s", strings.ReplaceAll(l.Name, ".", string(os.PathSeparator)), conf.Input.Ext) + filename := l.Name + if conf.Input.DotFolder { + filename = strings.ReplaceAll(filename, ".", string(os.PathSeparator)) + } + + if conf.Input.Ext != "" { + filename = fmt.Sprintf("%s.%s", filename, conf.Input.Ext) + } path := fp.Join(conf.Input.Path, filename) dir := fp.Dir(path) err = os.MkdirAll(dir, 0750) diff --git a/files/append_test.go b/files/append_test.go index da6a5a6..487acfd 100644 --- a/files/append_test.go +++ b/files/append_test.go @@ -57,6 +57,66 @@ func (s *AppendTestSuite) TestSuccess() { s.Assert().Contains(st, "\n@bar true") } +func (s *AppendTestSuite) TestDotFolder() { + config.Overrides["input.dotFolder"] = "true" + e := models.Entry{ + Title: "something", + Date: time.Now(), + } + l := models.Log{ + Name: "sub.test", + Entries: []models.Entry{e}, + } + err := Append(l) + s.Require().NoError(err) + s.Require().FileExists(s.dir + "/sub/test.log") + by, err := os.ReadFile(s.dir + "/sub/test.log") + st := string(by) + s.Require().NoError(err) + s.Assert().Contains(st, "something @end") +} + +func (s *AppendTestSuite) TestDotFolderNo() { + config.Overrides["input.dotFolder"] = "false" + e := models.Entry{ + Title: "another", + Date: time.Now(), + } + l := models.Log{ + Name: "sub.test", + Entries: []models.Entry{e}, + } + err := Append(l) + s.Require().NoError(err) + s.Require().FileExists(s.dir + "/sub.test.log") + by, err := os.ReadFile(s.dir + "/sub.test.log") + st := string(by) + s.Require().NoError(err) + s.Assert().Contains(st, "another @end") +} + +func (s *AppendTestSuite) TestNoExt() { + config.Overrides["input.ext"] = "" + defer func() { + config.Overrides["input.ext"] = "log" + }() + e := models.Entry{ + Title: "baz", + Date: time.Now(), + } + l := models.Log{ + Name: "foobar", + Entries: []models.Entry{e}, + } + err := Append(l) + s.Require().NoError(err) + s.Require().FileExists(s.dir + "/foobar") + by, err := os.ReadFile(s.dir + "/foobar") + st := string(by) + s.Require().NoError(err) + s.Assert().Contains(st, "baz @end") +} + func (s *AppendTestSuite) TestConfLoadErr() { currConf := config.ConfigPath tmp, _ := os.CreateTemp("", "app-conf-*.toml")