|
| 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 | + "github.com/tui-tools/tui-ssh/internal/openssh" |
| 14 | +) |
| 15 | + |
| 16 | +// runReport prints the block a bug report needs and exits. Everything generic |
| 17 | +// — the kit version, the distribution, the kernel, the terminal, where the |
| 18 | +// binary came from — is collected by the kit, so the whole family answers |
| 19 | +// --report in the same shape. What this function adds is the part only tui-ssh |
| 20 | +// knows: the backend it selected, the OpenSSH version the same probe --check |
| 21 | +// uses read off `ssh -V`, and which of the manifest's features that version |
| 22 | +// has — because the keyword this tool would write is a version question, and a |
| 23 | +// bug about the wrong spelling is answered on that line. |
| 24 | +// |
| 25 | +// It reads no configuration and starts no sshd. --check is the flag that does |
| 26 | +// that, and it escalates; a report has to work for a user who cannot, because |
| 27 | +// the missing privilege may be the bug. For the same reason a machine with no |
| 28 | +// OpenSSH at all still gets a report, with the selection error as one of its |
| 29 | +// lines: "there is nothing here to drive" is a bug report, not a refusal. |
| 30 | +func runReport(cfg config.Config, opts options, out io.Writer) error { |
| 31 | + palette, _ := theme.ResolvePalette() |
| 32 | + |
| 33 | + // The same probe --check and the header use. There is one version probe in |
| 34 | + // this tool and this is it, and the backend is built from its capability |
| 35 | + // set, so it comes first here as it does in run. |
| 36 | + backendCompat := probeCompat(context.Background(), opts.demo) |
| 37 | + |
| 38 | + var selected, selectError string |
| 39 | + if backend, err := pickBackend(cfg, opts, backendCompat); err != nil { |
| 40 | + selectError = err.Error() |
| 41 | + } else { |
| 42 | + selected = backend.Name() |
| 43 | + } |
| 44 | + |
| 45 | + info := report.Info{ |
| 46 | + Tool: toolName, |
| 47 | + Version: version, |
| 48 | + Backend: selected, |
| 49 | + BackendVersion: backendCompat.Version, |
| 50 | + BackendDetail: backendCompat.Detail, |
| 51 | + Demo: opts.demo, |
| 52 | + Sudo: cfg.String(config.KeySudo, ""), |
| 53 | + Theme: palette.Name, |
| 54 | + } |
| 55 | + if opts.demo { |
| 56 | + // The fake answers to the same name as the real backend, so the block |
| 57 | + // would otherwise read as a live server. What it imitates is OpenSSH, |
| 58 | + // which is the parsers and the command builders the session exercised. |
| 59 | + info.Backend = "demo" |
| 60 | + info.Extra = append(info.Extra, report.Field{ |
| 61 | + Key: "demo backend", Value: backendName, |
| 62 | + }) |
| 63 | + } |
| 64 | + info.Extra = append(info.Extra, report.Field{ |
| 65 | + Key: "features", Value: describeFeatures(backendCompat.Caps(), opts.demo), |
| 66 | + }) |
| 67 | + if selectError != "" { |
| 68 | + info.Extra = append(info.Extra, report.Field{ |
| 69 | + Key: "backend error", Value: selectError, |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + _, err := io.WriteString(out, report.Render(info)) |
| 74 | + return err |
| 75 | +} |
| 76 | + |
| 77 | +// reportedFeatures are the manifest features whose answer changes what this |
| 78 | +// tool writes, in the order the block names them. |
| 79 | +var reportedFeatures = []string{ |
| 80 | + openssh.FeatureIncludeDropins, |
| 81 | + openssh.FeatureKbdInteractive, |
| 82 | +} |
| 83 | + |
| 84 | +// describeFeatures renders the probe's verdict on each of them as one line. A |
| 85 | +// report that says only "openssh 8.4" leaves the reader working out for |
| 86 | +// themselves which keyword spelling that release accepts, and that difference |
| 87 | +// is most of the bugs about a change that did not take. |
| 88 | +func describeFeatures(caps compat.Caps, demo bool) string { |
| 89 | + // Nothing was probed in demo mode, and an unprobed capability set answers |
| 90 | + // yes to everything by design — which would read here as a verdict about |
| 91 | + // the machine rather than as the absence of one. |
| 92 | + if demo { |
| 93 | + return "not probed (the fake accepts every spelling)" |
| 94 | + } |
| 95 | + parts := make([]string, 0, len(reportedFeatures)) |
| 96 | + for _, name := range reportedFeatures { |
| 97 | + parts = append(parts, name+" "+yesNo(caps.Has(name))) |
| 98 | + } |
| 99 | + if len(parts) == 0 { |
| 100 | + return "none" |
| 101 | + } |
| 102 | + return strings.Join(parts, ", ") |
| 103 | +} |
| 104 | + |
| 105 | +// yesNo renders a capability the way the block reads it. |
| 106 | +func yesNo(v bool) string { |
| 107 | + if v { |
| 108 | + return "yes" |
| 109 | + } |
| 110 | + return "no" |
| 111 | +} |
| 112 | + |
| 113 | +// reportUsage is the flag's one-line help, kept here next to what it prints. |
| 114 | +var reportUsage = fmt.Sprintf( |
| 115 | + "print the versions and machine facts a bug report needs, then exit "+ |
| 116 | + "(no UI, no privileges, nothing about you or your server: paste it "+ |
| 117 | + "into a %s issue)", |
| 118 | + toolName) |
0 commit comments