Skip to content

Commit

Permalink
chore: satisfy linter
Browse files Browse the repository at this point in the history
  • Loading branch information
dmke committed Oct 30, 2024
1 parent 353c2d5 commit dcf764c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
31 changes: 20 additions & 11 deletions cmd/texd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand All @@ -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

Check warning on line 82 in cmd/texd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/texd/main.go#L82

Added line #L82 was not covered by tests
sz, err := units.FromHumanSize(retPolSize)
if err != nil {
return nil, err
Expand Down Expand Up @@ -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)

Check warning on line 141 in cmd/texd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/texd/main.go#L141

Added line #L141 was not covered by tests
}

return fs.Args()
}

func main() {
func main() { //nolint:funlen

Check warning on line 147 in cmd/texd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/texd/main.go#L147

Added line #L147 was not covered by tests
texd.PrintBanner(os.Stdout)
images := parseFlags() //nolint:ifshort // func has sideeffects
log, err := setupLogger()
Expand All @@ -158,12 +167,12 @@ func main() {
xlog.String("flag", "--tex-engine"),
xlog.Error(err))

Check warning on line 168 in cmd/texd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/texd/main.go#L167-L168

Added lines #L167 - L168 were not covered by tests
}
if max, err := units.FromHumanSize(maxJobSize); err != nil {
if maxsz, err := units.FromHumanSize(maxJobSize); err != nil {

Check warning on line 170 in cmd/texd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/texd/main.go#L170

Added line #L170 was not covered by tests
log.Fatal("error parsing maximum job size",
xlog.String("flag", "--max-job-size"),
xlog.Error(err))

Check warning on line 173 in cmd/texd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/texd/main.go#L172-L173

Added lines #L172 - L173 were not covered by tests
} else {
opts.MaxJobSize = max
opts.MaxJobSize = maxsz

Check warning on line 175 in cmd/texd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/texd/main.go#L175

Added line #L175 was not covered by tests
}
if storageDSN != "" {
rp, err := retentionPolicy()
Expand Down Expand Up @@ -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

Check warning on line 224 in cmd/texd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/texd/main.go#L223-L224

Added lines #L223 - L224 were not covered by tests
signal.Notify(exitCh, syscall.SIGINT, syscall.SIGTERM)
sig := <-exitCh

Expand Down
30 changes: 20 additions & 10 deletions tex/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -271,33 +281,33 @@ 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

Check warning on line 289 in tex/document.go

View check run for this annotation

Codecov / codecov/patch

tex/document.go#L289

Added line #L289 was not covered by tests
} else {
doc.files[name] = file
}

var wd string
wd, err = doc.WorkingDirectory()
if err != nil {
return // err is already an errWithCategory
return wc, err // err is already an errWithCategory

Check warning on line 297 in tex/document.go

View check run for this annotation

Codecov / codecov/patch

tex/document.go#L297

Added line #L297 was not covered by tests
}

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

Check warning on line 303 in tex/document.go

View check run for this annotation

Codecov / codecov/patch

tex/document.go#L303

Added line #L303 was not covered by tests
}
}

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

Check warning on line 310 in tex/document.go

View check run for this annotation

Codecov / codecov/patch

tex/document.go#L310

Added line #L310 was not covered by tests
}

log := doc.log.With(xlog.String("filename", file.name))
Expand All @@ -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
}

Expand Down

0 comments on commit dcf764c

Please sign in to comment.