diff --git a/DESIGN.md b/DESIGN.md index bd98fe1..e85564d 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -183,8 +183,9 @@ The Flux reader produces the same target as the Argo one. The field-by-field rul ## 7. Outputs -- **One analysis, several writers.** Every format is written from the same result. `--output-file FORMAT=FILE` adds a report without replacing stdout, so CI gets Markdown for the pull request, SARIF for code scanning and the table in the log from one run. -- **`--output-file` exists because of PowerShell.** `>` decodes the output of other programs with the console code page and corrupts UTF-8 (`—` becomes `ÔÇö`). deadvalues writes the file itself, always UTF-8 and uncolored. +- **One analysis, several writers.** Every format is written from the same result. `-o table,markdown,sarif --report` keeps the table on screen and writes the others to files, so CI gets Markdown for the pull request, SARIF for code scanning and the table in the log from one run. +- **`-o` is the format, `--report` the files.** `-o` stays one format on stdout without `--report`, as in Helm and kubectl, so `-o json | jq` keeps working; `--report-path` takes a directory, a base name or one file, as `nmap -oA` and Dependency-Check's `--out` do. Files are named `-`: the time is inside the report, and a second run on the same day replaces the first instead of piling up. `--output-file FORMAT=FILE`, the first form, is still accepted but hidden: the GitHub Action passes it, so the action also works with a release before `--report`. +- **Files because of PowerShell.** `>` decodes the output of other programs with the console code page and corrupts UTF-8 (`—` becomes `ÔÇö`). deadvalues writes the file itself, always UTF-8 and uncolored. - **Exact lines.** The values are parsed twice: into a `map` for Helm and as `yaml.v3` nodes for positions. `github` annotations and SARIF point to the line of the key. - **Markdown without emoji.** Statuses are plain text and the ones that fail by default (`DEAD`, `BREAKING`) are bold. Color belongs to whatever shows the report; the GitHub Action adds a GitHub alert on top. - **Relative paths.** Every output, JSON included, uses paths relative to the current directory. diff --git a/README.md b/README.md index 9f174ff..a336a9c 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ Only apps whose chart version changed between the two sides are diffed. The outp deadvalues scan apps # Every finding, as a Markdown report -deadvalues scan apps --details -o markdown --output-file report.md +deadvalues scan apps --details -o markdown --report-path report.md # Only the apps whose files changed in this branch deadvalues scan apps --changed-since origin/main @@ -221,7 +221,7 @@ No script is needed around it: the action installs the release matching its ref, ```yaml - uses: guarnz/deadvalues/setup@v0 -- run: deadvalues scan apps -o sarif --output-file results.sarif --fail-on none +- run: deadvalues scan apps --report-path results.sarif --fail-on none ``` `args` runs your own command with the same report; `setup` only installs the binary. Inputs, outputs and the report format are in [docs/github-action.md](https://github.com/guarnz/deadvalues/blob/main/docs/github-action.md). diff --git a/docs/cli/deadvalues.md b/docs/cli/deadvalues.md index ffa7816..6201c31 100644 --- a/docs/cli/deadvalues.md +++ b/docs/cli/deadvalues.md @@ -17,19 +17,20 @@ deadvalues [flags] ### Options ``` - --api-versions strings extra API versions for .Capabilities.APIVersions.Has - --cache-dir string chart cache (default "/deadvalues") - --color string auto | always | never (default "auto") - -c, --config string path to .deadvalues.yaml (default: repo root) - -h, --help help for deadvalues - --kube-version string Kubernetes version for .Capabilities (default: Helm SDK default) - --no-cache always download charts - --no-color same as --color=never - -o, --output string table | json | markdown | sarif | github (default "table") - --output-file stringArray write the report to a file in UTF-8 instead of stdout; FORMAT=FILE writes that format and keeps stdout (repeatable) - -v, --verbose log every render and probe - --version print version, commit and Helm SDK version - -w, --workers int concurrent renders (default number of CPUs) + --api-versions strings extra API versions for .Capabilities.APIVersions.Has + --cache-dir string chart cache (default "/deadvalues") + --color string auto | always | never (default "auto") + -c, --config string path to .deadvalues.yaml (default: repo root) + -h, --help help for deadvalues + --kube-version string Kubernetes version for .Capabilities (default: Helm SDK default) + --no-cache always download charts + --no-color same as --color=never + -o, --output strings table | json | markdown | sarif | github; several, comma-separated, with --report (default [table]) + --report write json, markdown and sarif, or those in -o, to -. in the current directory + --report-path string write the report files here instead: a directory, a base name, or a file name when there is one file format (implies --report) + -v, --verbose log every render and probe + --version print version, commit and Helm SDK version + -w, --workers int concurrent renders (default number of CPUs) ``` ### SEE ALSO diff --git a/docs/cli/deadvalues_check.md b/docs/cli/deadvalues_check.md index 381b8a7..e4b7195 100644 --- a/docs/cli/deadvalues_check.md +++ b/docs/cli/deadvalues_check.md @@ -43,17 +43,18 @@ deadvalues check [flags] ### Options inherited from parent commands ``` - --api-versions strings extra API versions for .Capabilities.APIVersions.Has - --cache-dir string chart cache (default "/deadvalues") - --color string auto | always | never (default "auto") - -c, --config string path to .deadvalues.yaml (default: repo root) - --kube-version string Kubernetes version for .Capabilities (default: Helm SDK default) - --no-cache always download charts - --no-color same as --color=never - -o, --output string table | json | markdown | sarif | github (default "table") - --output-file stringArray write the report to a file in UTF-8 instead of stdout; FORMAT=FILE writes that format and keeps stdout (repeatable) - -v, --verbose log every render and probe - -w, --workers int concurrent renders (default number of CPUs) + --api-versions strings extra API versions for .Capabilities.APIVersions.Has + --cache-dir string chart cache (default "/deadvalues") + --color string auto | always | never (default "auto") + -c, --config string path to .deadvalues.yaml (default: repo root) + --kube-version string Kubernetes version for .Capabilities (default: Helm SDK default) + --no-cache always download charts + --no-color same as --color=never + -o, --output strings table | json | markdown | sarif | github; several, comma-separated, with --report (default [table]) + --report write json, markdown and sarif, or those in -o, to -. in the current directory + --report-path string write the report files here instead: a directory, a base name, or a file name when there is one file format (implies --report) + -v, --verbose log every render and probe + -w, --workers int concurrent renders (default number of CPUs) ``` ### SEE ALSO diff --git a/docs/cli/deadvalues_diff.md b/docs/cli/deadvalues_diff.md index 4929663..d2c4c9d 100644 --- a/docs/cli/deadvalues_diff.md +++ b/docs/cli/deadvalues_diff.md @@ -55,17 +55,18 @@ deadvalues diff [dir] [flags] ### Options inherited from parent commands ``` - --api-versions strings extra API versions for .Capabilities.APIVersions.Has - --cache-dir string chart cache (default "/deadvalues") - --color string auto | always | never (default "auto") - -c, --config string path to .deadvalues.yaml (default: repo root) - --kube-version string Kubernetes version for .Capabilities (default: Helm SDK default) - --no-cache always download charts - --no-color same as --color=never - -o, --output string table | json | markdown | sarif | github (default "table") - --output-file stringArray write the report to a file in UTF-8 instead of stdout; FORMAT=FILE writes that format and keeps stdout (repeatable) - -v, --verbose log every render and probe - -w, --workers int concurrent renders (default number of CPUs) + --api-versions strings extra API versions for .Capabilities.APIVersions.Has + --cache-dir string chart cache (default "/deadvalues") + --color string auto | always | never (default "auto") + -c, --config string path to .deadvalues.yaml (default: repo root) + --kube-version string Kubernetes version for .Capabilities (default: Helm SDK default) + --no-cache always download charts + --no-color same as --color=never + -o, --output strings table | json | markdown | sarif | github; several, comma-separated, with --report (default [table]) + --report write json, markdown and sarif, or those in -o, to -. in the current directory + --report-path string write the report files here instead: a directory, a base name, or a file name when there is one file format (implies --report) + -v, --verbose log every render and probe + -w, --workers int concurrent renders (default number of CPUs) ``` ### SEE ALSO diff --git a/docs/cli/deadvalues_explain.md b/docs/cli/deadvalues_explain.md index 30c2ff2..7a93f6c 100644 --- a/docs/cli/deadvalues_explain.md +++ b/docs/cli/deadvalues_explain.md @@ -38,17 +38,18 @@ deadvalues explain [flags] ### Options inherited from parent commands ``` - --api-versions strings extra API versions for .Capabilities.APIVersions.Has - --cache-dir string chart cache (default "/deadvalues") - --color string auto | always | never (default "auto") - -c, --config string path to .deadvalues.yaml (default: repo root) - --kube-version string Kubernetes version for .Capabilities (default: Helm SDK default) - --no-cache always download charts - --no-color same as --color=never - -o, --output string table | json | markdown | sarif | github (default "table") - --output-file stringArray write the report to a file in UTF-8 instead of stdout; FORMAT=FILE writes that format and keeps stdout (repeatable) - -v, --verbose log every render and probe - -w, --workers int concurrent renders (default number of CPUs) + --api-versions strings extra API versions for .Capabilities.APIVersions.Has + --cache-dir string chart cache (default "/deadvalues") + --color string auto | always | never (default "auto") + -c, --config string path to .deadvalues.yaml (default: repo root) + --kube-version string Kubernetes version for .Capabilities (default: Helm SDK default) + --no-cache always download charts + --no-color same as --color=never + -o, --output strings table | json | markdown | sarif | github; several, comma-separated, with --report (default [table]) + --report write json, markdown and sarif, or those in -o, to -. in the current directory + --report-path string write the report files here instead: a directory, a base name, or a file name when there is one file format (implies --report) + -v, --verbose log every render and probe + -w, --workers int concurrent renders (default number of CPUs) ``` ### SEE ALSO diff --git a/docs/cli/deadvalues_prune.md b/docs/cli/deadvalues_prune.md index a5496cc..ff637b8 100644 --- a/docs/cli/deadvalues_prune.md +++ b/docs/cli/deadvalues_prune.md @@ -44,17 +44,18 @@ deadvalues prune [flags] ### Options inherited from parent commands ``` - --api-versions strings extra API versions for .Capabilities.APIVersions.Has - --cache-dir string chart cache (default "/deadvalues") - --color string auto | always | never (default "auto") - -c, --config string path to .deadvalues.yaml (default: repo root) - --kube-version string Kubernetes version for .Capabilities (default: Helm SDK default) - --no-cache always download charts - --no-color same as --color=never - -o, --output string table | json | markdown | sarif | github (default "table") - --output-file stringArray write the report to a file in UTF-8 instead of stdout; FORMAT=FILE writes that format and keeps stdout (repeatable) - -v, --verbose log every render and probe - -w, --workers int concurrent renders (default number of CPUs) + --api-versions strings extra API versions for .Capabilities.APIVersions.Has + --cache-dir string chart cache (default "/deadvalues") + --color string auto | always | never (default "auto") + -c, --config string path to .deadvalues.yaml (default: repo root) + --kube-version string Kubernetes version for .Capabilities (default: Helm SDK default) + --no-cache always download charts + --no-color same as --color=never + -o, --output strings table | json | markdown | sarif | github; several, comma-separated, with --report (default [table]) + --report write json, markdown and sarif, or those in -o, to -. in the current directory + --report-path string write the report files here instead: a directory, a base name, or a file name when there is one file format (implies --report) + -v, --verbose log every render and probe + -w, --workers int concurrent renders (default number of CPUs) ``` ### SEE ALSO diff --git a/docs/cli/deadvalues_scan.md b/docs/cli/deadvalues_scan.md index dfdae0c..4d3e858 100644 --- a/docs/cli/deadvalues_scan.md +++ b/docs/cli/deadvalues_scan.md @@ -39,17 +39,18 @@ deadvalues scan [flags] ### Options inherited from parent commands ``` - --api-versions strings extra API versions for .Capabilities.APIVersions.Has - --cache-dir string chart cache (default "/deadvalues") - --color string auto | always | never (default "auto") - -c, --config string path to .deadvalues.yaml (default: repo root) - --kube-version string Kubernetes version for .Capabilities (default: Helm SDK default) - --no-cache always download charts - --no-color same as --color=never - -o, --output string table | json | markdown | sarif | github (default "table") - --output-file stringArray write the report to a file in UTF-8 instead of stdout; FORMAT=FILE writes that format and keeps stdout (repeatable) - -v, --verbose log every render and probe - -w, --workers int concurrent renders (default number of CPUs) + --api-versions strings extra API versions for .Capabilities.APIVersions.Has + --cache-dir string chart cache (default "/deadvalues") + --color string auto | always | never (default "auto") + -c, --config string path to .deadvalues.yaml (default: repo root) + --kube-version string Kubernetes version for .Capabilities (default: Helm SDK default) + --no-cache always download charts + --no-color same as --color=never + -o, --output strings table | json | markdown | sarif | github; several, comma-separated, with --report (default [table]) + --report write json, markdown and sarif, or those in -o, to -. in the current directory + --report-path string write the report files here instead: a directory, a base name, or a file name when there is one file format (implies --report) + -v, --verbose log every render and probe + -w, --workers int concurrent renders (default number of CPUs) ``` ### SEE ALSO diff --git a/docs/cli/deadvalues_version.md b/docs/cli/deadvalues_version.md index 544fdd8..c3168ca 100644 --- a/docs/cli/deadvalues_version.md +++ b/docs/cli/deadvalues_version.md @@ -15,17 +15,18 @@ deadvalues version [flags] ### Options inherited from parent commands ``` - --api-versions strings extra API versions for .Capabilities.APIVersions.Has - --cache-dir string chart cache (default "/deadvalues") - --color string auto | always | never (default "auto") - -c, --config string path to .deadvalues.yaml (default: repo root) - --kube-version string Kubernetes version for .Capabilities (default: Helm SDK default) - --no-cache always download charts - --no-color same as --color=never - -o, --output string table | json | markdown | sarif | github (default "table") - --output-file stringArray write the report to a file in UTF-8 instead of stdout; FORMAT=FILE writes that format and keeps stdout (repeatable) - -v, --verbose log every render and probe - -w, --workers int concurrent renders (default number of CPUs) + --api-versions strings extra API versions for .Capabilities.APIVersions.Has + --cache-dir string chart cache (default "/deadvalues") + --color string auto | always | never (default "auto") + -c, --config string path to .deadvalues.yaml (default: repo root) + --kube-version string Kubernetes version for .Capabilities (default: Helm SDK default) + --no-cache always download charts + --no-color same as --color=never + -o, --output strings table | json | markdown | sarif | github; several, comma-separated, with --report (default [table]) + --report write json, markdown and sarif, or those in -o, to -. in the current directory + --report-path string write the report files here instead: a directory, a base name, or a file name when there is one file format (implies --report) + -v, --verbose log every render and probe + -w, --workers int concurrent renders (default number of CPUs) ``` ### SEE ALSO diff --git a/docs/github-action.md b/docs/github-action.md index e2cd4d1..ea71ede 100644 --- a/docs/github-action.md +++ b/docs/github-action.md @@ -19,7 +19,7 @@ steps: with: fetch-depth: 0 - uses: guarnz/deadvalues/setup@v0 - - run: deadvalues scan apps -o sarif --output-file results.sarif --fail-on none + - run: deadvalues scan apps --report-path results.sarif --fail-on none ``` | Input | Default | Description | diff --git a/docs/json.md b/docs/json.md index 55878fd..d3a6095 100644 --- a/docs/json.md +++ b/docs/json.md @@ -1,6 +1,6 @@ # JSON output -`-o json`, and `--output-file json=FILE`, print one object for every command that produces a report: +`-o json`, and the json file of `--report`, hold one object for every command that produces a report: ```json { diff --git a/docs/output.md b/docs/output.md index 12cb262..a3f10dd 100644 --- a/docs/output.md +++ b/docs/output.md @@ -14,14 +14,17 @@ ## Writing to files -`--output-file` writes the report to a file in UTF-8, whatever the shell (PowerShell's `>` re-encodes the output of other programs). `FORMAT=FILE` writes that format and keeps the normal output on stdout, so one run can produce several reports: +`--report` writes the report to files in UTF-8, whatever the shell (PowerShell's `>` re-encodes the output of other programs), and keeps the table on screen. The files are named `-.` in the current directory: `scan-2026-11-10.json`, `.md` and `.sarif`. Without a file format in `-o` it writes all three (`explain`: json and Markdown); with `-o`, only those listed, and `-o` takes several formats, comma-separated: ```bash -deadvalues check -f values.yaml -o markdown --output-file report.md -deadvalues scan apps --output-file markdown=report.md --output-file sarif=results.sarif +deadvalues scan apps --report # table on screen, scan-.json, .md and .sarif +deadvalues scan apps -o table,sarif --report # table on screen, scan-.sarif +deadvalues scan apps -o markdown,sarif --report-path reports/ # reports/scan-.md and .sarif +deadvalues scan apps --report-path reports/homelab # reports/homelab.json, .md and .sarif +deadvalues scan apps --report-path results.sarif # that file only ``` -The file is overwritten; a missing directory is an error. `prune` and `version` do not produce a report and reject the flag. +`--report-path` implies `--report` and says where: a directory (an existing one, or a path ending in `/`) gets the default names; a file name with the extension of a format (`.json`, `.md`, `.sarif`) is that one file; anything else is a base name, to which each format adds its extension. Missing directories are created and the files are overwritten, so a second run on the same day replaces the first; the time is in the report (`generatedAt` in json). Each file written is printed on stderr. `table` or `github`, when listed in `-o`, stays on screen. Without `--report`, `-o` takes one format and prints it on stdout, so `-o json | jq` works as before. `prune` and `version` do not produce a report and reject these flags. ## Color diff --git a/internal/cli/output.go b/internal/cli/output.go index 7afa799..0c697a3 100644 --- a/internal/cli/output.go +++ b/internal/cli/output.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "os" + "path/filepath" "slices" "strings" "time" @@ -17,6 +18,7 @@ type outputFile struct { format string path string prefixed bool + mkdir bool } func parseOutputFiles(values []string, defaultFormat string) []outputFile { @@ -31,6 +33,57 @@ func parseOutputFiles(values []string, defaultFormat string) []outputFile { return out } +var fileExt = map[string]string{"json": ".json", "markdown": ".md", "sarif": ".sarif"} + +// reportFiles places the -o formats that are files, or every file format the command writes when +// -o names none, at path: a directory, a base name, or one file. An empty path is the current one. +func reportFiles(command, path string, formats []string, now time.Time) ([]outputFile, error) { + var files []string + for _, f := range formats { + if fileExt[f] != "" && !slices.Contains(files, f) { + files = append(files, f) + } + } + byExt := "" + for f, ext := range fileExt { + if strings.EqualFold(filepath.Ext(path), ext) { + byExt = f + } + } + base := command + "-" + now.Format("2006-01-02") + switch { + case path == "": + case strings.HasSuffix(path, "/") || strings.HasSuffix(path, string(filepath.Separator)) || isDir(path): + base = filepath.Join(path, base) + case byExt != "": + if len(files) == 0 { + files = []string{byExt} + } + if len(files) > 1 || files[0] != byExt { + return nil, usageErr("--report-path %s is one %s file, but -o asks for %s", path, byExt, strings.Join(files, ", ")) + } + return []outputFile{{format: byExt, path: path, prefixed: true, mkdir: true}}, nil + default: + base = path + } + if len(files) == 0 { + files = []string{"json", "markdown", "sarif"} + if command == "explain" { + files = files[:2] + } + } + var out []outputFile + for _, f := range files { + out = append(out, outputFile{format: f, path: base + fileExt[f], prefixed: true, mkdir: true}) + } + return out, nil +} + +func isDir(path string) bool { + fi, err := os.Stat(path) + return err == nil && fi.IsDir() +} + // emit writes a report to stdout and every --output-file (UTF-8, never colored): a // plain PATH replaces stdout, and FORMAT=FILE entries are written in addition to it. func (g *globals) emit(write func(*report.Writer) error) error { @@ -55,6 +108,11 @@ func (g *globals) emit(write func(*report.Writer) error) error { if err := write(w); err != nil { return err } + if dir := filepath.Dir(o.path); o.mkdir && dir != "." { + if err := os.MkdirAll(dir, 0o755); err != nil { + return &exitError{code: 2, err: err} + } + } if err := os.WriteFile(o.path, buf.Bytes(), 0o644); err != nil { return &exitError{code: 2, err: err} } @@ -63,10 +121,48 @@ func (g *globals) emit(write func(*report.Writer) error) error { return nil } -// noOutputFiles rejects --output-file on commands that produce no report. +// noOutputFiles rejects report files on commands that produce no report. func (g *globals) noOutputFiles(command string) error { if len(g.outputs) > 0 { - return usageErr("%s does not produce a report; --output-file is not supported", command) + return usageErr("%s does not produce a report; --report, --report-path and --output-file are not supported", command) + } + return nil +} + +// outputFormats sets the format printed on stdout and the report files from -o, --report and +// --report-path; several formats in -o need --report, which writes those that are files. +func (g *globals) outputFormats(command string, now time.Time) error { + var screen []string + for _, f := range g.formats { + if !slices.Contains(report.Formats, f) { + return usageErr("--output must be one of %s", strings.Join(report.Formats, ", ")) + } + if fileExt[f] == "" && !slices.Contains(screen, f) { + screen = append(screen, f) + } + } + if len(g.formats) == 0 { + return usageErr("--output must be one of %s", strings.Join(report.Formats, ", ")) + } + if !g.report && g.reportPath == "" { + if len(g.formats) > 1 { + return usageErr("-o takes several formats only with --report or --report-path, which write json, markdown and sarif to files") + } + g.output = g.formats[0] + g.outputs = parseOutputFiles(g.outputFiles, g.output) + return nil + } + if len(screen) > 1 { + return usageErr("-o can print only one of %s on screen", strings.Join(screen, " or ")) + } + g.output = "table" + if len(screen) == 1 { + g.output = screen[0] + } + files, err := reportFiles(command, g.reportPath, g.formats, now) + if err != nil { + return err } + g.outputs = append(parseOutputFiles(g.outputFiles, g.output), files...) return nil } diff --git a/internal/cli/output_test.go b/internal/cli/output_test.go index 4efa821..a202ada 100644 --- a/internal/cli/output_test.go +++ b/internal/cli/output_test.go @@ -58,6 +58,63 @@ func TestOutputFileSeveralFormatsInOneRun(t *testing.T) { } } +func TestReportFiles(t *testing.T) { + chart, values := absPath(t, casesChart), absPath(t, casesValues) + check := []string{"check", "--chart", chart, "-f", values} + day := time.Now().Format("2006-01-02") + dir := t.TempDir() + t.Chdir(dir) + + out, code := runCLI(t, append(check, "--report")...) + if code != 1 || !strings.Contains(out, "service.prot") { + t.Errorf("--report keeps the table on stdout and the exit code: %d\n%s", code, out) + } + for _, name := range []string{"check-" + day + ".json", "check-" + day + ".md", "check-" + day + ".sarif"} { + if _, err := os.Stat(filepath.Join(dir, name)); err != nil { + t.Errorf("--report without a file format in -o writes all of them: %v", err) + } + } + + runCLI(t, append(check, "-o", "table,json", "--report-path", "reports/")...) + if entries, _ := os.ReadDir(filepath.Join(dir, "reports")); len(entries) != 1 || entries[0].Name() != "check-"+day+".json" { + t.Errorf("a directory gets the -o file formats with the default name, got %v", entries) + } + + runCLI(t, append(check, "-o", "json,sarif", "--report-path", "out/home")...) + for _, name := range []string{"home.json", "home.sarif"} { + if _, err := os.Stat(filepath.Join(dir, "out", name)); err != nil { + t.Errorf("a base name gets the extension of each format: %v", err) + } + } + + runCLI(t, append(check, "--report-path", "results.sarif")...) + if data, _ := os.ReadFile(filepath.Join(dir, "results.sarif")); !strings.Contains(string(data), `"version": "2.1.0"`) { + t.Errorf("a file name with a format's extension is that file:\n%s", data) + } + + for _, args := range [][]string{ + {"-o", "json,sarif"}, + {"-o", "json,sarif", "--report-path", "x.json"}, + {"-o", "table,github", "--report"}, + } { + if out, code := runCLI(t, append(check, args...)...); code != 2 { + t.Errorf("%v: exit %d, want 2\n%s", args, code, out) + } + } + if _, code := runCLI(t, "prune", "-f", values, "--chart", chart, "--report"); code != 2 { + t.Errorf("prune writes no report: exit %d, want 2", code) + } +} + +func absPath(t *testing.T, p string) string { + t.Helper() + abs, err := filepath.Abs(p) + if err != nil { + t.Fatal(err) + } + return abs +} + func TestJSONReportEnvelope(t *testing.T) { type envelope struct { SchemaVersion int `json:"schemaVersion"` diff --git a/internal/cli/root.go b/internal/cli/root.go index 3dd838b..212e7f5 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -11,6 +11,7 @@ import ( "runtime" "slices" "strings" + "time" "github.com/spf13/cobra" "golang.org/x/term" @@ -86,6 +87,9 @@ type globals struct { color string noColor bool verbose bool + formats []string + report bool + reportPath string outputFiles []string outputs []outputFile @@ -122,7 +126,9 @@ output changes. No template parsing, no regex.`, f := root.PersistentFlags() f.StringVarP(&g.configPath, "config", "c", "", "path to .deadvalues.yaml (default: repo root)") - f.StringVarP(&g.output, "output", "o", "table", "table | json | markdown | sarif | github") + f.StringSliceVarP(&g.formats, "output", "o", []string{"table"}, "table | json | markdown | sarif | github; several, comma-separated, with --report") + f.BoolVar(&g.report, "report", false, "write json, markdown and sarif, or those in -o, to -. in the current directory") + f.StringVar(&g.reportPath, "report-path", "", "write the report files here instead: a directory, a base name, or a file name when there is one file format (implies --report)") f.StringVar(&g.kubeVersion, "kube-version", "", "Kubernetes version for .Capabilities (default: Helm SDK default)") f.StringSliceVar(&g.apiVersions, "api-versions", nil, "extra API versions for .Capabilities.APIVersions.Has") f.StringVar(&g.cacheDir, "cache-dir", "", `chart cache (default "/deadvalues")`) @@ -132,6 +138,7 @@ output changes. No template parsing, no regex.`, f.BoolVar(&g.noColor, "no-color", false, "same as --color=never") f.BoolVarP(&g.verbose, "verbose", "v", false, "log every render and probe") f.StringArrayVar(&g.outputFiles, "output-file", nil, "write the report to a file in UTF-8 instead of stdout; FORMAT=FILE writes that format and keeps stdout (repeatable)") + _ = f.MarkHidden("output-file") var showVersion bool root.Flags().BoolVar(&showVersion, "version", false, "print version, commit and Helm SDK version") @@ -174,8 +181,8 @@ output changes. No template parsing, no regex.`, } func (g *globals) setup(cmd *cobra.Command) error { - if !slices.Contains(report.Formats, g.output) { - return usageErr("--output must be one of %s", strings.Join(report.Formats, ", ")) + if err := g.outputFormats(cmd.Name(), time.Now()); err != nil { + return err } if !slices.Contains([]string{"auto", "always", "never"}, g.color) { return usageErr("--color must be auto, always or never") @@ -183,8 +190,6 @@ func (g *globals) setup(cmd *cobra.Command) error { if g.workers < 1 { return usageErr("--workers must be at least 1") } - g.outputs = parseOutputFiles(g.outputFiles, g.output) - if g.verbose { log.SetOutput(g.stderr) } else {