Use dot as folder separator

Also don't add dot if no extension
This commit is contained in:
Dan Jones 2024-02-26 19:30:32 -06:00
commit a61af1b4b3
4 changed files with 74 additions and 4 deletions

View file

@ -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]

View file

@ -9,6 +9,7 @@ type Input struct {
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

View file

@ -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)

View file

@ -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")