Parallelize

This commit is contained in:
2025-10-01 13:57:12 +02:00
parent 9fbd9207e3
commit e4139dab87
3 changed files with 21 additions and 10 deletions

8
go.mod
View File

@@ -1,14 +1,18 @@
module datesorter
go 1.23.3
go 1.23.6
require git.site.quack-lab.dev/dave/cylogger v1.4.0
require (
git.site.quack-lab.dev/dave/cylogger v1.4.0
git.site.quack-lab.dev/dave/cyutils v1.4.0
)
require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/hexops/valast v1.5.0 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/time v0.12.0 // indirect
golang.org/x/tools v0.4.0 // indirect
mvdan.cc/gofumpt v0.4.0 // indirect
)

4
go.sum
View File

@@ -1,5 +1,7 @@
git.site.quack-lab.dev/dave/cylogger v1.4.0 h1:3Ca7V5JWvruARJd5S8xDFwW9LnZ9QInqkYLRdrEFvuY=
git.site.quack-lab.dev/dave/cylogger v1.4.0/go.mod h1:wctgZplMvroA4X6p8f4B/LaCKtiBcT1Pp+L14kcS8jk=
git.site.quack-lab.dev/dave/cyutils v1.4.0 h1:/Xo3QfLIFNab+axHneWmUK4MyfuObl+qq8whF9vTQpk=
git.site.quack-lab.dev/dave/cyutils v1.4.0/go.mod h1:fBjALu2Cp2u2bDr+E4zbGVMBeIgFzROg+4TCcTNAiQU=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
@@ -22,6 +24,8 @@ golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
golang.org/x/tools v0.4.0 h1:7mTAgkunk3fr4GAloyyCasadO6h9zSsQZbwvcaIciV4=
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
mvdan.cc/gofumpt v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM=

19
main.go
View File

@@ -9,6 +9,7 @@ import (
"time"
logger "git.site.quack-lab.dev/dave/cylogger"
"git.site.quack-lab.dev/dave/cyutils"
)
type DateParser interface {
@@ -55,13 +56,15 @@ func main() {
files := flag.Args()
for _, file := range files {
filelog := logger.Default.WithPrefix(fmt.Sprintf("file=%q", file))
cyutils.WithWorkers(10, files, func(worker int, _ int, file string) {
workerlog := logger.Default.WithPrefix(fmt.Sprintf("worker=%d", worker))
filelog := workerlog.WithPrefix(fmt.Sprintf("file=%q", file))
filelog.Info("processing file")
date, err := parseDate(file, parsers)
if err != nil {
filelog.Error("error parsing date: %s", err)
continue
return
}
filelog.Info("date: %s", date)
@@ -71,26 +74,26 @@ func main() {
err = os.MkdirAll(datedir, 0755)
if err != nil {
filelog.Error("error creating directory: %s", err)
continue
return
}
// Make sure file doesn't exist
destfile, err := os.Stat(dest)
if err == nil {
filelog.Info("dest file already exists: %s", destfile.Name())
continue
return
} else {
if !os.IsNotExist(err) {
filelog.Error("error checking dest file: %s", err)
continue
return
}
}
err = os.Rename(file, dest)
if err != nil {
filelog.Error("error renaming file: %s", err)
continue
return
}
filelog.Info("moved to %q", dest)
}
})
}
func parseDate(file string, parsers []DateParser) (time.Time, error) {