mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-17 16:38:54 +00:00
* refine the plan
* Fix all staticcheck lint findings (uncapped)
golangci-lint's default max-same-issues=3 was hiding most of the backlog:
the true pre-fix count was 69 staticcheck findings, not 34. This fixes all
of them, driving staticcheck to zero:
- ST1023/QF1011 (37): omit explicit types inferred from the RHS
- S1009/S1031 (15): drop redundant nil checks before len()/range
- SA9003 (9): remove comment-only empty branches, keeping the comments
- QF1007 (3): merge conditional assignment into declaration
- QF1006 (3): lift break conditions into loop conditions
- QF1001 (3): apply De Morgan's law / name the negated predicate
Also updates plans/lintfixes.md with the cap discovery and the corrected
errcheck picture (1202 uncapped, ~949 of them fmt.Fprint*).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* Drive errcheck to zero: config for bulk categories, propagate real errors
Adds .golangci.yml with errcheck exclude-functions for fmt.Fprint* (usage
printers), (*bufio.Writer).Write/WriteString (sticky errors, surfaced at the
now-checked final Flush), and (*strings.Builder).WriteString; pins
max-issues-per-linter/max-same-issues to 0 so CI reports true counts.
Real error paths now propagate instead of being dropped:
- Finalize{Reader,Writer}Options in join/put/filter/split/tee and the
repl/script entry points: 'mlr join -i badformat' now errors instead of
silently using wrong separators
- final output-stream Flush in pkg/stream: write failure no longer exits 0
- DSL emit/print/dump redirect writes, matching their sibling branches
- CSV writer WriteCSVRecordMaybeColorized, close-time Flush in file output
handlers, ENV[...] Setenv, REPL record-write and redirect-close errors
- termcvt write-side Close before rename (had "TODO: check return status")
The rest are deliberate ignores, marked with _ = and a comment where the
reason isn't obvious: unset-of-missing-path no-ops, read-side closes,
mid-stream FlushOnEveryRecord, init-time strftime registrations, in-memory
usage-capture pipes, and regtest-harness env/temp-file teardown.
golangci-lint now reports 0 issues on ./cmd/mlr ./pkg/... with all caps off.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
163 lines
4.4 KiB
Go
163 lines
4.4 KiB
Go
// Entry point for mlr script: executes a Miller DSL script with next()-driven
|
|
// record iteration. No prompt, no REPL. Script from -f or -e, data from
|
|
// filenames or stdin.
|
|
//
|
|
// Example:
|
|
//
|
|
// mlr script -f sum.mlr data.csv
|
|
// mlr script -e '@sum=0; while(next()){@sum+=$x}; print @sum' --csv data.csv
|
|
|
|
package script
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/johnkerl/miller/v6/pkg/cli"
|
|
"github.com/johnkerl/miller/v6/pkg/lib"
|
|
)
|
|
|
|
func scriptUsage(verbName string, o *os.File, exitCode int) {
|
|
exeName := path.Base(os.Args[0])
|
|
fmt.Fprintf(o, "Usage: %s %s [options] -f {script file} | -e {expression} [zero or more data-file names]\n", exeName, verbName)
|
|
fmt.Fprint(o,
|
|
`-f {file} Load DSL script from file. If file is a directory, load all *.mlr files.
|
|
-e {expression} DSL script from command line. May be combined with -f.
|
|
|
|
--load {file} Preload DSL before script (e.g. function library).
|
|
--mload {files} Like --load but multiple files. Use -- to terminate list.
|
|
|
|
-w Show warnings about uninitialized variables.
|
|
-z Strict mode.
|
|
|
|
-h|--help Show this message.
|
|
|
|
Or any --icsv, --ojson, etc. reader/writer options.
|
|
|
|
Data-file names (or stdin if none) are the record input stream.
|
|
`)
|
|
os.Exit(exitCode)
|
|
}
|
|
|
|
func ScriptMain(args []string) int {
|
|
scriptName := args[0]
|
|
argc := len(args)
|
|
argi := 1
|
|
|
|
doWarnings := false
|
|
strictMode := false
|
|
options := cli.DefaultOptions()
|
|
var dslStrings []string
|
|
haveDSLStrings := false
|
|
|
|
for argi < argc {
|
|
if !strings.HasPrefix(args[argi], "-") {
|
|
break
|
|
}
|
|
|
|
if args[argi] == "-h" || args[argi] == "--help" {
|
|
scriptUsage(scriptName, os.Stdout, 0)
|
|
} else if args[argi] == "-w" {
|
|
doWarnings = true
|
|
argi++
|
|
} else if args[argi] == "-z" {
|
|
strictMode = true
|
|
argi++
|
|
} else if args[argi] == "-f" {
|
|
if argc-argi < 2 {
|
|
scriptUsage(scriptName, os.Stderr, 1)
|
|
}
|
|
argi++
|
|
filename, err := cli.VerbGetStringArg("script", "-f", args, &argi, argc)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
theseStrings, err := lib.LoadStringsFromFileOrDir(filename, ".mlr")
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr script: cannot load script from \"%s\": %v\n", filename, err)
|
|
os.Exit(1)
|
|
}
|
|
dslStrings = append(dslStrings, theseStrings...)
|
|
haveDSLStrings = true
|
|
} else if args[argi] == "-e" {
|
|
if argc-argi < 2 {
|
|
scriptUsage(scriptName, os.Stderr, 1)
|
|
}
|
|
argi++
|
|
expr, err := cli.VerbGetStringArg("script", "-e", args, &argi, argc)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
dslStrings = append(dslStrings, expr)
|
|
haveDSLStrings = true
|
|
} else if args[argi] == "--load" {
|
|
if argc-argi < 2 {
|
|
scriptUsage(scriptName, os.Stderr, 1)
|
|
}
|
|
options.DSLPreloadFileNames = append(options.DSLPreloadFileNames, args[argi+1])
|
|
argi += 2
|
|
} else if args[argi] == "--mload" {
|
|
if argc-argi < 2 {
|
|
scriptUsage(scriptName, os.Stderr, 1)
|
|
}
|
|
argi++
|
|
for argi < argc && args[argi] != "--" {
|
|
options.DSLPreloadFileNames = append(options.DSLPreloadFileNames, args[argi])
|
|
argi++
|
|
}
|
|
if argi < argc && args[argi] == "--" {
|
|
argi++
|
|
}
|
|
} else if cli.FLAG_TABLE.Parse(args, argc, &argi, options) {
|
|
} else {
|
|
scriptUsage(scriptName, os.Stderr, 1)
|
|
}
|
|
}
|
|
|
|
if !haveDSLStrings {
|
|
fmt.Fprintf(os.Stderr, "mlr script: -f or -e is required\n")
|
|
scriptUsage(scriptName, os.Stderr, 1)
|
|
}
|
|
|
|
if err := cli.FinalizeReaderOptions(&options.ReaderOptions); err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr %s: %v\n", scriptName, err)
|
|
return 1
|
|
}
|
|
if err := cli.FinalizeWriterOptions(&options.WriterOptions); err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr %s: %v\n", scriptName, err)
|
|
return 1
|
|
}
|
|
options.WriterOptions.AutoFlatten = cli.DecideFinalFlatten(&options.WriterOptions)
|
|
options.WriterOptions.AutoUnflatten = cli.DecideFinalUnflatten(options, [][]string{})
|
|
|
|
filenames := args[argi:]
|
|
|
|
scr, err := NewScript(options, doWarnings, strictMode, dslStrings)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr script: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
scr.openFiles(filenames)
|
|
err = scr.run()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr script: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
err = scr.bufferedRecordOutputStream.Flush()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr script: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
err = scr.closeBufferedOutputStream()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr script: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
return 0
|
|
}
|