|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/tui-tools/tui-firewall/internal/backends" |
| 10 | + "github.com/tui-tools/tui-kit/config" |
| 11 | + "github.com/tui-tools/tui-kit/report" |
| 12 | + "github.com/tui-tools/tui-kit/theme" |
| 13 | +) |
| 14 | + |
| 15 | +// runReport prints the block a bug report needs and exits. Everything generic |
| 16 | +// — the kit version, the distribution, the kernel, the terminal, where the |
| 17 | +// binary came from — is collected by the kit, so the whole family answers |
| 18 | +// --report in the same shape. What this function adds is the part only |
| 19 | +// tui-firewall knows: which backend was selected, what the compat probe read |
| 20 | +// off it, and what the detector saw of the backend it did not pick. |
| 21 | +// |
| 22 | +// It never reads the firewall. --check is the flag that does that, and it needs |
| 23 | +// privileges; a report has to work for a user who cannot get them, because the |
| 24 | +// missing privilege may be the bug. For the same reason a machine with no |
| 25 | +// firewall at all still gets a report, with the selection error as one of its |
| 26 | +// lines: "there is nothing here to drive" is a bug report, not a refusal. |
| 27 | +func runReport(cfg config.Config, opts options, out io.Writer) error { |
| 28 | + palette, _ := theme.ResolvePalette() |
| 29 | + |
| 30 | + var backendName, selectError string |
| 31 | + if backend, err := pickBackend(cfg, opts); err != nil { |
| 32 | + selectError = err.Error() |
| 33 | + } else { |
| 34 | + backendName = backend.Name() |
| 35 | + } |
| 36 | + |
| 37 | + // The same probe --check and the header use. There is one version probe in |
| 38 | + // this tool and this is it. |
| 39 | + backendCompat := probeCompat(context.Background(), backendName, opts.demo.on) |
| 40 | + |
| 41 | + info := report.Info{ |
| 42 | + Tool: toolName, |
| 43 | + Version: version, |
| 44 | + Backend: backendName, |
| 45 | + BackendVersion: backendCompat.Version, |
| 46 | + BackendDetail: backendCompat.Detail, |
| 47 | + Demo: opts.demo.on, |
| 48 | + Sudo: cfg.String(config.KeySudo, ""), |
| 49 | + Theme: palette.Name, |
| 50 | + } |
| 51 | + if opts.demo.on { |
| 52 | + // The fake imitates one of the real backends, and which one decides |
| 53 | + // which command builders and which parser the session exercised. It is |
| 54 | + // taken from the flag rather than from the fake's own name, because a |
| 55 | + // fake is free to call itself "demo". |
| 56 | + info.Backend = "demo" |
| 57 | + info.Extra = append(info.Extra, report.Field{ |
| 58 | + Key: "demo backend", Value: opts.demo.backend, |
| 59 | + }) |
| 60 | + } |
| 61 | + info.Extra = append(info.Extra, report.Field{ |
| 62 | + Key: "backends", Value: describeBackends(backends.Inspect(backendName)), |
| 63 | + }) |
| 64 | + if selectError != "" { |
| 65 | + info.Extra = append(info.Extra, report.Field{ |
| 66 | + Key: "backend error", Value: selectError, |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + _, err := io.WriteString(out, report.Render(info)) |
| 71 | + return err |
| 72 | +} |
| 73 | + |
| 74 | +// describeBackends renders the detector's view of every backend the tool knows |
| 75 | +// as one line. A report that says only "firewalld" leaves the reader guessing |
| 76 | +// whether ufw was absent or merely stopped, and that difference is most of the |
| 77 | +// backend selection bugs. |
| 78 | +func describeBackends(states []backends.State) string { |
| 79 | + parts := make([]string, 0, len(states)) |
| 80 | + for _, s := range states { |
| 81 | + switch { |
| 82 | + case !s.Installed: |
| 83 | + parts = append(parts, s.Name+" absent") |
| 84 | + case s.Active: |
| 85 | + parts = append(parts, s.Name+" active") |
| 86 | + default: |
| 87 | + parts = append(parts, s.Name+" installed") |
| 88 | + } |
| 89 | + } |
| 90 | + if len(parts) == 0 { |
| 91 | + return "none" |
| 92 | + } |
| 93 | + return strings.Join(parts, ", ") |
| 94 | +} |
| 95 | + |
| 96 | +// reportUsage is the flag's one-line help, kept here next to what it prints. |
| 97 | +var reportUsage = fmt.Sprintf( |
| 98 | + "print the versions and machine facts a bug report needs, then exit "+ |
| 99 | + "(no UI, no privileges, nothing about you: paste it into a %s issue)", |
| 100 | + toolName) |
0 commit comments