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>
206 lines
5.2 KiB
Go
206 lines
5.2 KiB
Go
// This is the shell command-line entry point to the Miller REPL command line.
|
|
// E.g. at the shell prompt, you type 'mlr repl --json' -- this file will parse
|
|
// that. It will then hand off control to a REPL session which will handle all
|
|
// subsequent REPL command-line statements you type in at a Miller REPL prompt.
|
|
//
|
|
// Example:
|
|
//
|
|
// bash$ mlr repl --json <------------- this file handles this bit
|
|
// [mlr] :open myfile.json
|
|
// [mlr] :read
|
|
// [mlr] :context
|
|
// FILENAME="myfile.json",FILENUM=1,NR=1,FNR=1
|
|
// [mlr] $*
|
|
// {
|
|
// "hostname": "localhost",
|
|
// "pid": 12345
|
|
// }
|
|
// [mlr] :quit
|
|
|
|
package repl
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/johnkerl/miller/v6/pkg/cli"
|
|
)
|
|
|
|
func replUsage(verbName string, o *os.File, exitCode int) {
|
|
exeName := path.Base(os.Args[0])
|
|
fmt.Fprintf(o, "Usage: %s %s [options] {zero or more data-file names}\n", exeName, verbName)
|
|
|
|
// TODO: cli/UsageForReaderOptions
|
|
// TODO: cli/UsageForWriterOptions
|
|
// TODO: cli/UsageForReaderWriterOptions
|
|
|
|
// TODO: maybe -f/-e as in put?
|
|
// TODO: maybe -s as in put?
|
|
// TODO: maybe -x as in put?
|
|
// TODO: maybe -q as in put?
|
|
|
|
fmt.Fprint(o,
|
|
`-v Prints the expressions's AST (abstract syntax tree), which gives
|
|
full transparency on the precedence and associativity rules of
|
|
Miller's grammar, to stdout.
|
|
|
|
-d Like -v but uses a parenthesized-expression format for the AST.
|
|
|
|
-D Like -d but with output all on one line.
|
|
|
|
-w Show warnings about uninitialized variables
|
|
|
|
-q Don't show startup banner
|
|
|
|
-s Don't show prompts
|
|
|
|
--load {DSL script file} Load script file before presenting the prompt.
|
|
If the name following --load is a directory, load all "*.mlr" files
|
|
in that directory.
|
|
|
|
--mload {DSL script files} -- Like --load but works with more than one filename,
|
|
e.g. '--mload *.mlr --'.
|
|
|
|
-h|--help Show this message.
|
|
|
|
Or any --icsv, --ojson, etc. reader/writer options as for the main Miller command line.
|
|
|
|
Any data-file names are opened just as if you had waited and typed :open {filenames}
|
|
at the Miller REPL prompt.
|
|
`)
|
|
|
|
os.Exit(exitCode)
|
|
}
|
|
|
|
// Here the args are the full Miller command line: if the latter was "mlr
|
|
// --some-flag repl foo bar" then the former is "repl foo bar".
|
|
func ReplMain(args []string) int {
|
|
exeName := os.Args[0]
|
|
replName := args[0]
|
|
argc := len(args)
|
|
argi := 1
|
|
|
|
showStartupBanner := true
|
|
showPrompts := true
|
|
astPrintMode := ASTPrintNone
|
|
doWarnings := false
|
|
strictMode := false
|
|
options := cli.DefaultOptions()
|
|
|
|
for argi < argc /* variable increment: 1 or 2 depending on flag */ {
|
|
if !strings.HasPrefix(args[argi], "-") {
|
|
break // No more flag options to process
|
|
}
|
|
|
|
if args[argi] == "-h" || args[argi] == "--help" {
|
|
replUsage(replName, os.Stdout, 0)
|
|
|
|
} else if args[argi] == "-q" {
|
|
showStartupBanner = false
|
|
argi++
|
|
} else if args[argi] == "-s" {
|
|
showPrompts = false
|
|
argi++
|
|
} else if args[argi] == "-v" {
|
|
astPrintMode = ASTPrintIndent
|
|
argi++
|
|
} else if args[argi] == "-d" {
|
|
astPrintMode = ASTPrintParex
|
|
argi++
|
|
} else if args[argi] == "-D" {
|
|
astPrintMode = ASTPrintParexOneLine
|
|
argi++
|
|
} else if args[argi] == "-w" {
|
|
doWarnings = true
|
|
argi++
|
|
|
|
} else if args[argi] == "-z" {
|
|
strictMode = true
|
|
argi++
|
|
|
|
} else if args[argi] == "--load" {
|
|
if argc-argi < 2 {
|
|
replUsage(replName, os.Stderr, 1)
|
|
}
|
|
options.DSLPreloadFileNames = append(options.DSLPreloadFileNames, args[argi+1])
|
|
argi += 2
|
|
|
|
} else if args[argi] == "--mload" {
|
|
if argc-argi < 2 {
|
|
replUsage(replName, os.Stderr, 1)
|
|
}
|
|
argi += 1
|
|
for argi < argc && args[argi] != "--" {
|
|
options.DSLPreloadFileNames = append(options.DSLPreloadFileNames, args[argi])
|
|
argi += 1
|
|
}
|
|
if args[argi] == "--" {
|
|
argi += 1
|
|
}
|
|
|
|
} else if cli.FLAG_TABLE.Parse(args, argc, &argi, options) {
|
|
|
|
} else {
|
|
replUsage(replName, os.Stderr, 1)
|
|
}
|
|
}
|
|
|
|
if err := cli.FinalizeReaderOptions(&options.ReaderOptions); err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr %s: %v\n", replName, err)
|
|
return 1
|
|
}
|
|
if err := cli.FinalizeWriterOptions(&options.WriterOptions); err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr %s: %v\n", replName, err)
|
|
return 1
|
|
}
|
|
|
|
// --auto-flatten is on by default. But if input and output formats are both JSON,
|
|
// then we don't need to actually do anything. See also mlrcli_parse.go.
|
|
options.WriterOptions.AutoFlatten = cli.DecideFinalFlatten(&options.WriterOptions)
|
|
options.WriterOptions.AutoUnflatten = cli.DecideFinalUnflatten(options, [][]string{})
|
|
|
|
recordOutputFileName := "(stdout)"
|
|
recordOutputStream := os.Stdout
|
|
|
|
repl, err := NewRepl(
|
|
exeName,
|
|
replName,
|
|
showStartupBanner,
|
|
showPrompts,
|
|
astPrintMode,
|
|
doWarnings,
|
|
strictMode,
|
|
options,
|
|
recordOutputFileName,
|
|
recordOutputStream,
|
|
)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
filenames := args[argi:]
|
|
if len(filenames) > 0 {
|
|
repl.openFiles(filenames)
|
|
}
|
|
|
|
err = repl.handleSession(os.Stdin)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr %s: %v\n", repl.replName, err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
err = repl.bufferedRecordOutputStream.Flush()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr %s: %v\n", repl.replName, err)
|
|
os.Exit(1)
|
|
}
|
|
err = repl.closeBufferedOutputStream()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "mlr %s: %v\n", repl.replName, err)
|
|
os.Exit(1)
|
|
}
|
|
return 0
|
|
}
|