|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/tui-tools/tui-kit/compat" |
| 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 | +// hostDetail explains the one line of the block that would otherwise read as a |
| 16 | +// failed probe. This tool's backend is the machine itself, and a machine has no |
| 17 | +// version: the versions that matter are the engines', and they get their own |
| 18 | +// line right below. |
| 19 | +const hostDetail = "the machine itself; the engine versions are on the engines line" |
| 20 | + |
| 21 | +// runReport prints the block a bug report needs and exits. Everything generic |
| 22 | +// — the kit version, the distribution, the kernel, the terminal, where the |
| 23 | +// binary came from — is collected by the kit, so the whole family answers |
| 24 | +// --report in the same shape. What this function adds is the part only |
| 25 | +// tui-containers knows: which engines this machine has and at which versions, |
| 26 | +// which is what decides nearly every question asked of a report here — an |
| 27 | +// action refused by a Podman below 5.0 and one refused by a bug look the same |
| 28 | +// from the outside. |
| 29 | +// |
| 30 | +// It reads no engine. --check is the flag that does that, and it needs a socket |
| 31 | +// this account may not be able to reach; a report has to work for a user who |
| 32 | +// cannot reach it, because the unreachable socket may be the bug. A machine |
| 33 | +// with neither engine installed still gets a report, with "none" on the engines |
| 34 | +// line: "there is nothing here to drive" is a bug report, not a refusal. |
| 35 | +func runReport(cfg config.Config, opts options, out io.Writer) error { |
| 36 | + palette, _ := theme.ResolvePalette() |
| 37 | + |
| 38 | + // The same probe the header and --check use. There is one version probe in |
| 39 | + // this tool and this is it. It runs `docker --version` and |
| 40 | + // `podman --version`, which read nothing and change nothing. |
| 41 | + results := probeCompat(context.Background(), opts.demo) |
| 42 | + |
| 43 | + var backendName, selectError string |
| 44 | + if backend, err := pickBackend(cfg, opts, results); err != nil { |
| 45 | + selectError = err.Error() |
| 46 | + } else { |
| 47 | + backendName = backend.Name() |
| 48 | + } |
| 49 | + |
| 50 | + info := report.Info{ |
| 51 | + Tool: toolName, |
| 52 | + Version: version, |
| 53 | + Backend: backendName, |
| 54 | + BackendDetail: hostDetail, |
| 55 | + Demo: opts.demo, |
| 56 | + Sudo: cfg.String(config.KeySudo, ""), |
| 57 | + Theme: palette.Name, |
| 58 | + } |
| 59 | + if opts.demo { |
| 60 | + // The fake stands in for the same backend the real one is, and it |
| 61 | + // answers to the same name; naming it on its own line keeps the |
| 62 | + // backend line free to say demo, which is the thing a reader must not |
| 63 | + // miss. |
| 64 | + info.Backend = "demo" |
| 65 | + imitated := backendName |
| 66 | + if imitated == "" { |
| 67 | + imitated = "host" |
| 68 | + } |
| 69 | + info.Extra = append(info.Extra, report.Field{ |
| 70 | + Key: "demo backend", Value: imitated, |
| 71 | + }) |
| 72 | + } |
| 73 | + info.Extra = append(info.Extra, report.Field{ |
| 74 | + Key: "engines", Value: describeEngines(results, opts.demo), |
| 75 | + }) |
| 76 | + if selectError != "" { |
| 77 | + info.Extra = append(info.Extra, report.Field{ |
| 78 | + Key: "backend error", Value: selectError, |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + _, err := io.WriteString(out, report.Render(info)) |
| 83 | + return err |
| 84 | +} |
| 85 | + |
| 86 | +// describeEngines renders every engine this tool declares, with the version it |
| 87 | +// answered or the reason it did not, as one line. A report that named only the |
| 88 | +// engine that answered would leave the reader guessing whether the other was |
| 89 | +// absent or merely unreadable from this account, and that difference is most of |
| 90 | +// the "nothing is listed" reports. |
| 91 | +func describeEngines(results []compat.Result, demo bool) string { |
| 92 | + if demo { |
| 93 | + // --demo drives an in-memory machine, so nothing was probed. "none" |
| 94 | + // here would read as a machine with no engine installed. |
| 95 | + return "not probed (demo reads no machine)" |
| 96 | + } |
| 97 | + parts := make([]string, 0, len(results)) |
| 98 | + for _, result := range results { |
| 99 | + name := strings.TrimSpace(result.Backend) |
| 100 | + if name == "" { |
| 101 | + continue |
| 102 | + } |
| 103 | + switch { |
| 104 | + case strings.TrimSpace(result.Version) != "": |
| 105 | + parts = append(parts, name+" "+strings.TrimSpace(result.Version)) |
| 106 | + case strings.TrimSpace(result.Detail) != "": |
| 107 | + parts = append(parts, name+" (no version: "+result.Detail+")") |
| 108 | + default: |
| 109 | + parts = append(parts, name+" (no version)") |
| 110 | + } |
| 111 | + } |
| 112 | + if len(parts) == 0 { |
| 113 | + return "none" |
| 114 | + } |
| 115 | + return strings.Join(parts, ", ") |
| 116 | +} |
| 117 | + |
| 118 | +// reportUsage is the flag's one-line help, kept here next to what it prints. |
| 119 | +var reportUsage = fmt.Sprintf( |
| 120 | + "print the versions and machine facts a bug report needs, then exit "+ |
| 121 | + "(no UI, no privileges, nothing about you: paste it into a %s issue)", |
| 122 | + toolName) |
0 commit comments