| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "os" |
| 10 | "strings" |
| 11 | "time" |
| 12 | |
| 13 | "reasonix/internal/config" |
| 14 | "reasonix/internal/crashreport" |
| 15 | "reasonix/internal/i18n" |
| 16 | ) |
| 17 | |
| 18 | var sendCLIReport = crashreport.Send |
| 19 | |
| 20 | func reportCommand(args []string) int { |
| 21 | return reportCommandWithIO(args, cliIsInteractive(), os.Stdin, os.Stdout, os.Stderr) |
| 22 | } |
| 23 | |
| 24 | func reportCommandWithIO(args []string, interactive bool, in io.Reader, out, errOut io.Writer) int { |
| 25 | home := config.ReasonixHomeDir() |
| 26 | action := "" |
| 27 | id := "" |
| 28 | if len(args) > 0 { |
| 29 | action = strings.ToLower(strings.TrimSpace(args[0])) |
| 30 | } |
| 31 | if len(args) > 1 { |
| 32 | id = strings.TrimSpace(args[1]) |
| 33 | } |
| 34 | if len(args) > 2 { |
| 35 | reportUsage(errOut) |
| 36 | return 2 |
| 37 | } |
| 38 | |
| 39 | switch action { |
| 40 | case "list": |
| 41 | if id != "" { |
| 42 | reportUsage(errOut) |
| 43 | return 2 |
| 44 | } |
| 45 | reports, err := crashreport.List(home) |
| 46 | if err != nil { |
| 47 | fmt.Fprintln(errOut, i18n.M.ErrorPrefix, err) |
| 48 | return 1 |
| 49 | } |
| 50 | if len(reports) == 0 { |
| 51 | fmt.Fprintln(out, i18n.M.ReportNoPending) |
| 52 | return 0 |
| 53 | } |
| 54 | for _, pending := range reports { |
| 55 | fmt.Fprintf(out, "%s %s %s/%s %s\n", pending.ID, pending.CapturedAt().Format(time.RFC3339), pending.Report.OS, pending.Report.Arch, pending.Report.Version) |
| 56 | } |
| 57 | return 0 |
| 58 | case "show": |
| 59 | return showCLIReport(home, id, out, errOut) |
| 60 | case "send": |
| 61 | pending, err := loadCLIReport(home, id, errOut) |
| 62 | if err != nil { |
| 63 | return reportLoadResult(err, out) |
| 64 | } |
| 65 | return sendPendingCLIReport(home, pending, out, errOut) |
| 66 | case "delete": |
| 67 | pending, err := loadCLIReport(home, id, errOut) |
| 68 | if err != nil { |
| 69 | return reportLoadResult(err, out) |
| 70 | } |
| 71 | if err := crashreport.Remove(home, pending.ID); err != nil { |
| 72 | fmt.Fprintln(errOut, i18n.M.ErrorPrefix, err) |
| 73 | return 1 |
| 74 | } |
| 75 | fmt.Fprintf(out, i18n.M.ReportDeletedFmt+"\n", pending.ID) |
| 76 | return 0 |
| 77 | case "": |
| 78 | pending, err := loadCLIReport(home, "", errOut) |
| 79 | if err != nil { |
| 80 | return reportLoadResult(err, out) |
| 81 | } |
| 82 | if rc := printCLIReport(pending, out, errOut); rc != 0 { |
| 83 | return rc |
| 84 | } |
| 85 | if !interactive { |
| 86 | fmt.Fprintf(out, "\n"+i18n.M.ReportPreviewOnlyFmt+"\n", pending.ID) |
| 87 | return 0 |
| 88 | } |
| 89 | answer := ask(bufio.NewScanner(in), out, "\n"+i18n.M.ReportSendPrompt, "y/N") |
| 90 | if !strings.EqualFold(strings.TrimSpace(answer), "y") && !strings.EqualFold(strings.TrimSpace(answer), "yes") { |
| 91 | fmt.Fprintln(out, i18n.M.ReportKept) |
| 92 | return 0 |
| 93 | } |
| 94 | return sendPendingCLIReport(home, pending, out, errOut) |
| 95 | default: |
| 96 | reportUsage(errOut) |
| 97 | return 2 |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func loadCLIReport(home, id string, errOut io.Writer) (crashreport.Pending, error) { |
| 102 | pending, err := crashreport.Load(home, id) |
| 103 | if err != nil { |
| 104 | if !errors.Is(err, crashreport.ErrNoReports) { |
| 105 | fmt.Fprintln(errOut, i18n.M.ErrorPrefix, err) |
| 106 | } |
| 107 | return crashreport.Pending{}, err |
| 108 | } |
| 109 | return pending, nil |
| 110 | } |
| 111 | |
| 112 | func showCLIReport(home, id string, out, errOut io.Writer) int { |
| 113 | pending, err := loadCLIReport(home, id, errOut) |
| 114 | if err != nil { |
| 115 | return reportLoadResult(err, out) |
| 116 | } |
| 117 | return printCLIReport(pending, out, errOut) |
| 118 | } |
| 119 | |
| 120 | func reportLoadResult(err error, out io.Writer) int { |
| 121 | if errors.Is(err, crashreport.ErrNoReports) { |
| 122 | fmt.Fprintln(out, i18n.M.ReportNoPending) |
| 123 | return 0 |
| 124 | } |
| 125 | return 1 |
| 126 | } |
| 127 | |
| 128 | func printCLIReport(pending crashreport.Pending, out, errOut io.Writer) int { |
| 129 | preview, err := crashreport.Preview(pending.Report) |
| 130 | if err != nil { |
| 131 | fmt.Fprintln(errOut, i18n.M.ErrorPrefix, err) |
| 132 | return 1 |
| 133 | } |
| 134 | fmt.Fprintf(out, i18n.M.ReportHeaderFmt+"\n"+i18n.M.ReportCapturedFmt+"\n\n%s\n", pending.ID, pending.CapturedAt().Format(time.RFC3339), preview) |
| 135 | return 0 |
| 136 | } |
| 137 | |
| 138 | func sendPendingCLIReport(home string, pending crashreport.Pending, out, errOut io.Writer) int { |
| 139 | cfg, err := config.Load() |
| 140 | if err != nil { |
| 141 | fmt.Fprintln(errOut, i18n.M.ErrorPrefix, fmt.Sprintf(i18n.M.ReportConfigFailedFmt, err)) |
| 142 | return 1 |
| 143 | } |
| 144 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 145 | defer cancel() |
| 146 | if err := sendCLIReport(ctx, pending.Report, cfg.NetworkProxySpec()); err != nil { |
| 147 | fmt.Fprintln(errOut, i18n.M.ErrorPrefix, fmt.Sprintf(i18n.M.ReportUploadFailedFmt, err)) |
| 148 | return 1 |
| 149 | } |
| 150 | if err := crashreport.Remove(home, pending.ID); err != nil { |
| 151 | fmt.Fprintln(errOut, i18n.M.ErrorPrefix, fmt.Sprintf(i18n.M.ReportSentDeleteFailedFmt, err)) |
| 152 | return 1 |
| 153 | } |
| 154 | fmt.Fprintf(out, i18n.M.ReportSentFmt+"\n", pending.ID) |
| 155 | return 0 |
| 156 | } |
| 157 | |
| 158 | func reportUsage(w io.Writer) { |
| 159 | fmt.Fprintln(w, i18n.M.ReportUsageBody) |
| 160 | } |
| 161 |