From 9d10789a12378ac5d06e6b08622c3927652c4389 Mon Sep 17 00:00:00 2001 From: Dominik Menke Date: Wed, 30 Oct 2024 19:53:01 +0100 Subject: [PATCH] chore: satisfy linter --- cmd/texd/main.go | 31 ++++++++++++++++++++----------- tex/document.go | 30 ++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/cmd/texd/main.go b/cmd/texd/main.go index 3f8f442..059218a 100644 --- a/cmd/texd/main.go +++ b/cmd/texd/main.go @@ -28,12 +28,21 @@ import ( "github.com/thediveo/enumflag" ) +const ( + defaultQueueTimeout = 10 * time.Second + defaultMaxJobSize = 50 * units.MiB + defaultCompileTimeout = time.Minute + defaultRetentionPoolSize = 100 * units.MiB + + exitFlagErr = 2 +) + var opts = service.Options{ Addr: ":2201", QueueLength: runtime.GOMAXPROCS(0), - QueueTimeout: 10 * time.Second, - MaxJobSize: 50 * units.MiB, - CompileTimeout: time.Minute, + QueueTimeout: defaultQueueTimeout, + MaxJobSize: defaultMaxJobSize, + CompileTimeout: defaultCompileTimeout, Mode: "local", Executor: exec.LocalExec, KeepJobs: service.KeepJobsNever, @@ -60,8 +69,8 @@ var ( 1: {"purge", "purge-on-start"}, 2: {"access"}, } - retPolItems = 1000 // number of items in refstore - retPolSize = units.BytesSize(float64(100 * units.MiB)) // max total file size + retPolItems = 1000 // number of items in refstore + retPolSize = units.BytesSize(float64(defaultRetentionPoolSize)) // max total file size ) func retentionPolicy() (refstore.RetentionPolicy, error) { @@ -70,7 +79,7 @@ func retentionPolicy() (refstore.RetentionPolicy, error) { return &refstore.KeepForever{}, nil case 1: return &refstore.PurgeOnStart{}, nil - case 2: + case 2: //nolint:mnd sz, err := units.FromHumanSize(retPolSize) if err != nil { return nil, err @@ -129,13 +138,13 @@ func parseFlags() []string { os.Exit(0) case err != nil: fmt.Fprintf(os.Stderr, "Error parsing flags:\n\t%v\n", err) - os.Exit(2) + os.Exit(exitFlagErr) } return fs.Args() } -func main() { +func main() { //nolint:funlen texd.PrintBanner(os.Stdout) images := parseFlags() //nolint:ifshort // func has sideeffects log, err := setupLogger() @@ -158,12 +167,12 @@ func main() { xlog.String("flag", "--tex-engine"), xlog.Error(err)) } - if max, err := units.FromHumanSize(maxJobSize); err != nil { + if maxsz, err := units.FromHumanSize(maxJobSize); err != nil { log.Fatal("error parsing maximum job size", xlog.String("flag", "--max-job-size"), xlog.Error(err)) } else { - opts.MaxJobSize = max + opts.MaxJobSize = maxsz } if storageDSN != "" { rp, err := retentionPolicy() @@ -212,7 +221,7 @@ const exitTimeout = 10 * time.Second type stopFun func(context.Context) error func onExit(log xlog.Logger, stopper ...stopFun) { - exitCh := make(chan os.Signal, 2) + exitCh := make(chan os.Signal, 2) //nolint:mnd // idiomatic signal.Notify(exitCh, syscall.SIGINT, syscall.SIGTERM) sig := <-exitCh diff --git a/tex/document.go b/tex/document.go index 9c2fba4..8d41b97 100644 --- a/tex/document.go +++ b/tex/document.go @@ -18,6 +18,16 @@ import ( // should specify main files explicitly. const Mark = "%!texd" +// When no main input file is given, the first KiB of each TeX file is +// searched for a `\documentclass` macro. This is another last resort +// measurement. +const guessLimit = 1024 // first KiB + +const ( + o_rwx = 0o700 // read, write, execute permissions for owner + o_rw = 0o600 // read and write permissions for owner +) + // texFs can be overridden in tests. var texFs = afero.NewOsFs() @@ -57,8 +67,8 @@ type fileWriter struct { func (w *fileWriter) Write(p []byte) (int, error) { if w.file.isCandidate() { // fill buf, if buf has capacity - if max := len(w.buf); w.off < max { - if n := max - w.off; n > len(p) { + if pos := len(w.buf); w.off < pos { + if n := pos - w.off; n > len(p) { // p fits completely into buf's rest capacity copy(w.buf[w.off:], p) w.off += len(p) @@ -271,12 +281,12 @@ func (doc *document) NewWriter(name string) (wc io.WriteCloser, err error) { var ok bool if file.name, ok = cleanpath(name); !ok { err = InputError("invalid file name", nil, nil) - return + return wc, err } if _, exists := doc.files[name]; exists { err = InputError("duplicate file name", nil, nil) - return + return wc, err } else { doc.files[name] = file } @@ -284,20 +294,20 @@ func (doc *document) NewWriter(name string) (wc io.WriteCloser, err error) { var wd string wd, err = doc.WorkingDirectory() if err != nil { - return // err is already an errWithCategory + return wc, err // err is already an errWithCategory } if dir := path.Dir(name); dir != "" { - if osErr := doc.fs.MkdirAll(path.Join(wd, dir), 0o700); osErr != nil { + if osErr := doc.fs.MkdirAll(path.Join(wd, dir), o_rwx); osErr != nil { err = InputError("cannot create directory", osErr, nil) - return + return wc, err } } - f, osErr := doc.fs.OpenFile(path.Join(wd, name), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600) + f, osErr := doc.fs.OpenFile(path.Join(wd, name), os.O_CREATE|os.O_APPEND|os.O_WRONLY, o_rw) if osErr != nil { err = InputError("cannot create file", osErr, nil) - return + return wc, err } log := doc.log.With(xlog.String("filename", file.name)) @@ -310,7 +320,7 @@ func (doc *document) NewWriter(name string) (wc io.WriteCloser, err error) { log: log, file: file, wc: f, - buf: make([]byte, 1024), + buf: make([]byte, guessLimit), }, nil }