diff --git a/README.md b/README.md index 6982237..acaeb1c 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,9 @@ sudo systemctl start paperless-consume ## Why does this exist? -I didn't any other projects or means to consume a directory but _uploads_ the documents via API. -In my case, I can't configure the scanner to upload to the consume dir as prepared by paperless-already, so I created a tool that also watches a directory, but uploads them to Paperless instead. +I didn't find any other projects or means to consume a directory that _uploads_ the documents via API. +In my case, I can't configure the scanner to directly upload to the consume dir as setup by paperless-already, I have to watch the dir on a different host. +So I created a tool that also watches a directory, but uploads them to Paperless instead. Other projects that I've found: diff --git a/consume_command.go b/consume_command.go index b4a04c2..762bc02 100644 --- a/consume_command.go +++ b/consume_command.go @@ -5,6 +5,7 @@ import ( "io/fs" "os" "path/filepath" + "time" "github.com/ccremer/clustercode/pkg/consumer" "github.com/ccremer/clustercode/pkg/paperless" @@ -20,6 +21,7 @@ type ConsumeCommand struct { PaperlessUser string ConsumeDirName string + ConsumeDelay time.Duration } func newConsumeCommand() *ConsumeCommand { @@ -34,6 +36,7 @@ func newConsumeCommand() *ConsumeCommand { newUsernameFlag(&c.PaperlessUser), newTokenFlag(&c.PaperlessToken), newConsumeDirFlag(&c.ConsumeDirName), + newConsumeDelayFlag(&c.ConsumeDelay), }, } return c @@ -75,7 +78,7 @@ func (c *ConsumeCommand) Action(ctx *cli.Context) error { return fmt.Errorf("cannot walk consumption dir: %w", walkErr) } - watchErr := consumer.StartWatchingDir(ctx.Context, c.ConsumeDirName, func(filePath string) { + watchErr := consumer.StartWatchingDir(ctx.Context, c.ConsumeDirName, c.ConsumeDelay, func(filePath string) { q.Put(filePath) }) if watchErr != nil { diff --git a/flags.go b/flags.go index a7fb5e3..344b485 100644 --- a/flags.go +++ b/flags.go @@ -2,13 +2,14 @@ package main import ( "fmt" + "time" "github.com/urfave/cli/v2" ) func newLogLevelFlag() *cli.IntFlag { return &cli.IntFlag{ - Name: "log-level", Aliases: []string{"v"}, EnvVars: envVars("LOG_LEVEL"), + Name: "log-level", Aliases: []string{"v"}, EnvVars: []string{"LOG_LEVEL"}, Usage: "number of the log level verbosity", Value: 0, } @@ -98,15 +99,34 @@ func newConsumeDirFlag(dest *string) *cli.StringFlag { } } +func newConsumeDelayFlag(dest *time.Duration) *cli.DurationFlag { + return &cli.DurationFlag{ + Name: "consume-delay", EnvVars: []string{"CONSUME_DELAY"}, + Usage: "the delay after detecting the last file write operation before uploading it.", + Value: 1 * time.Second, + Destination: dest, + Action: func(ctx *cli.Context, duration time.Duration) error { + if duration.Milliseconds() < 100 { + return showFlagError(ctx, fmt.Errorf("Duration of flag %q must be at least 100ms", "consume-delay")) + } + return nil + }, + } +} + func checkEmptyString(flagName string) func(*cli.Context, string) error { return func(ctx *cli.Context, s string) error { if s == "" { - subcommands := ctx.Command.Subcommands - ctx.Command.Subcommands = nil // required to print usage of subcommand - _ = cli.ShowCommandHelp(ctx, ctx.Command.Name) - ctx.Command.Subcommands = subcommands - return fmt.Errorf(`Required flag %q not set`, flagName) + return showFlagError(ctx, fmt.Errorf(`Required flag %q not set`, flagName)) } return nil } } + +func showFlagError(ctx *cli.Context, err error) error { + subcommands := ctx.Command.Subcommands + ctx.Command.Subcommands = nil // required to print usage of subcommand + _ = cli.ShowCommandHelp(ctx, ctx.Command.Name) + ctx.Command.Subcommands = subcommands + return err +} diff --git a/go.mod b/go.mod index e523a9e..3628b41 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,10 @@ module github.com/ccremer/clustercode go 1.19 require ( - github.com/ccremer/plogr v0.6.0 + github.com/ccremer/plogr v0.7.0 + github.com/fsnotify/fsnotify v1.6.0 github.com/go-logr/logr v1.2.3 - github.com/pterm/pterm v0.12.50 + github.com/pterm/pterm v0.12.51 github.com/urfave/cli/v2 v2.23.5 ) @@ -14,7 +15,6 @@ require ( atomicgo.dev/keyboard v0.2.8 // indirect github.com/containerd/console v1.0.3 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect - github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/gookit/color v1.5.2 // indirect github.com/lithammer/fuzzysearch v1.1.5 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect diff --git a/go.sum b/go.sum index 25f888d..8538a2c 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,5 @@ atomicgo.dev/assert v0.0.2 h1:FiKeMiZSgRrZsPo9qn/7vmr7mCsh5SZyXY4YGYiYwrg= +atomicgo.dev/assert v0.0.2/go.mod h1:ut4NcI3QDdJtlmAxQULOmA13Gz6e2DWbSAS8RUOmNYQ= atomicgo.dev/cursor v0.1.1 h1:0t9sxQomCTRh5ug+hAMCs59x/UmC9QL6Ci5uosINKD4= atomicgo.dev/cursor v0.1.1/go.mod h1:Lr4ZJB3U7DfPPOkbH7/6TOtJ4vFGHlgj1nc+n900IpU= atomicgo.dev/keyboard v0.2.8 h1:Di09BitwZgdTV1hPyX/b9Cqxi8HVuJQwWivnZUEqlj4= @@ -9,12 +10,13 @@ github.com/MarvinJWendt/testza v0.2.8/go.mod h1:nwIcjmr0Zz+Rcwfh3/4UhBp7ePKVhuBE github.com/MarvinJWendt/testza v0.2.10/go.mod h1:pd+VWsoGUiFtq+hRKSU1Bktnn+DMCSrDrXDpX2bG66k= github.com/MarvinJWendt/testza v0.2.12/go.mod h1:JOIegYyV7rX+7VZ9r77L/eH6CfJHHzXjB69adAhzZkI= github.com/MarvinJWendt/testza v0.3.0/go.mod h1:eFcL4I0idjtIx8P9C6KkAuLgATNKpX4/2oUqKc6bF2c= -github.com/MarvinJWendt/testza v0.3.5/go.mod h1:ExbTpWmA1z2E9HSskvrNcwApoX4F9bID692s10nuHRY= github.com/MarvinJWendt/testza v0.4.2/go.mod h1:mSdhXiKH8sg/gQehJ63bINcCKp7RtYewEjXsvsVUPbE= +github.com/MarvinJWendt/testza v0.4.3/go.mod h1:CpXaOfceNEYnLDtNIyTrPPcCpDJYqzZnu2aiA2Wp33U= github.com/MarvinJWendt/testza v0.5.1 h1:a9Fqx6vQrHQ4CyiaLhktfTTelwGotmFWy8MNhyaohw8= +github.com/MarvinJWendt/testza v0.5.1/go.mod h1:L7csM8IBqCc0HH4TRYZSPCIRg6zJeqzM1pm3FSYZBso= github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk= -github.com/ccremer/plogr v0.6.0 h1:0/2dVxIcbrDkbFjpZcWBUyGYtIHhslgTmBECjMptLzs= -github.com/ccremer/plogr v0.6.0/go.mod h1:0rx2lckCXaVhC8bKV2BIc3Kt2sKEmXpvdsNOQ7hk0g8= +github.com/ccremer/plogr v0.7.0 h1:hASyuM8NYBfYclNHNdugrnHzLiPqFGt+zcMjpc0vdUA= +github.com/ccremer/plogr v0.7.0/go.mod h1:57bEBtEjCiSqybkPwKbUYm7tbN7MmorYIK6DBwmmHIc= github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw= github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= @@ -33,7 +35,9 @@ github.com/gookit/color v1.5.2/go.mod h1:w8h4bGiHeeBpvQVePTutdbERIUf3oJE5lZ8HM0U github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= +github.com/klauspost/cpuid/v2 v2.1.0/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= github.com/klauspost/cpuid/v2 v2.2.0 h1:4ZexSFt8agMNzNisrsilL6RClWDC5YJnLHNIfTy4iuc= +github.com/klauspost/cpuid/v2 v2.2.0/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= @@ -51,9 +55,9 @@ github.com/pterm/pterm v0.12.31/go.mod h1:32ZAWZVXD7ZfG0s8qqHXePte42kdz8ECtRyEej github.com/pterm/pterm v0.12.33/go.mod h1:x+h2uL+n7CP/rel9+bImHD5lF3nM9vJj80k9ybiiTTE= github.com/pterm/pterm v0.12.36/go.mod h1:NjiL09hFhT/vWjQHSj1athJpx6H8cjpHXNAK5bUw8T8= github.com/pterm/pterm v0.12.40/go.mod h1:ffwPLwlbXxP+rxT0GsgDTzS3y3rmpAO1NMjUkGTYf8s= -github.com/pterm/pterm v0.12.41/go.mod h1:LW/G4J2A42XlTaPTAGRPvbBfF4UXvHWhC6SN7ueU4jU= -github.com/pterm/pterm v0.12.50 h1:53nKg5lLI1kXkvLWq2IQI5rgkPkFzEQsuQjxAb39VlE= -github.com/pterm/pterm v0.12.50/go.mod h1:79BLm4vos2z+eOoHnDG7ZWuYtLaSStyaspKjGmSoxc4= +github.com/pterm/pterm v0.12.49/go.mod h1:D4OBoWNqAfXkm5QLTjIgjNiMXPHemLJHnIreGUsWzWg= +github.com/pterm/pterm v0.12.51 h1:iwhNG1FhQMgks+5kVyr/ClRk3WJCuL907nJN7RqmEpw= +github.com/pterm/pterm v0.12.51/go.mod h1:79BLm4vos2z+eOoHnDG7ZWuYtLaSStyaspKjGmSoxc4= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= @@ -62,36 +66,55 @@ github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ= github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/urfave/cli/v2 v2.23.5 h1:xbrU7tAYviSpqeR3X4nEFWUdB/uDZ6DE+HxmRU7Xtyw= github.com/urfave/cli/v2 v2.23.5/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956 h1:XeJjHH1KiLpKGb6lvMiksZ9l0fVUh+AmGcm0nOMEBOY= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/logger.go b/logger.go index 89d397b..5821873 100644 --- a/logger.go +++ b/logger.go @@ -6,7 +6,6 @@ import ( "github.com/ccremer/plogr" "github.com/go-logr/logr" - "github.com/pterm/pterm" "github.com/urfave/cli/v2" ) @@ -35,14 +34,17 @@ func LogMetadata(c *cli.Context) error { } func setupLogging(c *cli.Context) error { - logger = newPlogger() + sink := newSink(c.Int(newLogLevelFlag().Name)) + logger = logr.New(sink) c.Context = logr.NewContext(c.Context, logger) return nil } -func newPlogger() logr.Logger { +func newSink(level int) *plogr.PtermSink { sink := plogr.NewPtermSink() - sink.FallbackPrinter = &pterm.Debug - sink.ErrorPrinter.ShowLineNumber = false - return logr.New(sink) + sink.ErrorPrinter.ShowLineNumber = true + for i := 1; i <= level; i++ { + sink.SetLevelEnabled(i, true) + } + return &sink } diff --git a/main.go b/main.go index c8ad6b4..9b2bdc7 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,7 @@ import ( "os" "time" - "github.com/pterm/pterm" + "github.com/ccremer/plogr" "github.com/urfave/cli/v2" ) @@ -26,7 +26,7 @@ func main() { app := NewApp() err := app.Run(os.Args) if err != nil { - pterm.Error.Println(err.Error()) + plogr.DefaultErrorPrinter.Println(err.Error()) os.Exit(1) } } diff --git a/package/systemd.env b/package/systemd.env index 87556a1..ebdaee2 100644 --- a/package/systemd.env +++ b/package/systemd.env @@ -13,6 +13,9 @@ PAPERLESS_TOKEN= ## (Required) The directory path in which files are consumed (uploaded + deleted). CONSUME_DIR= +## The delay after detecting the last file write operation before uploading it. +# CONSUME_DELAY=1s + ### Misc ## Logging level. Increased numbers are more verbose. diff --git a/pkg/consumer/notify.go b/pkg/consumer/notify.go index c6da23c..9a09007 100644 --- a/pkg/consumer/notify.go +++ b/pkg/consumer/notify.go @@ -11,7 +11,7 @@ import ( "github.com/go-logr/logr" ) -func StartWatchingDir(ctx context.Context, dir string, callback func(filePath string)) error { +func StartWatchingDir(ctx context.Context, dir string, resetDelay time.Duration, callback func(filePath string)) error { log := logr.FromContextOrDiscard(ctx) watcher, err := fsnotify.NewWatcher() if err != nil { @@ -19,7 +19,7 @@ func StartWatchingDir(ctx context.Context, dir string, callback func(filePath st } go func() { - waitFor := 1 * time.Second + waitFor := resetDelay // Keep track of the timers, as [path]timer. timers := sync.Map{} @@ -57,7 +57,8 @@ func StartWatchingDir(ctx context.Context, dir string, callback func(filePath st t.(*time.Timer).Stop() timers.Store(e.Name, t) } - // Reset the timer for this path, so it will start from 100ms again. + // Reset the timer for this path, so it will start the delay again. + log.V(3).Info("Resetting timer", "delay", waitFor) t.(*time.Timer).Reset(waitFor) } } diff --git a/test/consume.env b/test/consume.env new file mode 100644 index 0000000..137da08 --- /dev/null +++ b/test/consume.env @@ -0,0 +1,5 @@ +export PAPERLESS_USERNAME=admin +export PAPERLESS_TOKEN=admin +export PAPERLESS_URL=http://localhost:8008 +export CONSUME_DIR=./.work/consume +export CONSUME_DELAY=3s diff --git a/test/local.mk b/test/local.mk index 66c3095..e9a724b 100644 --- a/test/local.mk +++ b/test/local.mk @@ -4,9 +4,12 @@ compose_project = paperless-cli clean_targets += local-uninstall .PHONY: local-install -local-install: ## Install paperless-ngx in docker-compose +local-install: | $(consume_dir) ## Install paperless-ngx in docker-compose docker-compose -f $(compose_file) -p $(compose_project) up -d .PHONY: local-uninstall local-uninstall: ## Uninstall paperless-ngx in docker-compose docker-compose -f $(compose_file) -p $(compose_project) rm --force --stop -v + +$(consume_dir): + mkdir -p $@