| 210 | } |
| 211 | |
| 212 | func writeDocsWriteOrphanResult(ctx context.Context, docID, tabID string, orphans []docsWriteOrphanComment) error { |
| 213 | if len(orphans) == 0 { |
| 214 | return nil |
| 215 | } |
| 216 | result := docsWriteOrphanResult{ |
| 217 | DocumentID: docID, |
| 218 | TabID: tabID, |
| 219 | WouldOrphan: orphans, |
| 220 | } |
| 221 | if outfmt.IsJSON(ctx) { |
| 222 | if err := outfmt.WriteJSON(ctx, stdoutWriter(ctx), result); err != nil { |
| 223 | return err |
| 224 | } |
| 225 | return &ExitError{Code: exitCodeOrphaned, Err: nil} |
| 226 | } |
| 227 | |
| 228 | u := ui.FromContext(ctx) |
| 229 | u.Err().Linef("docs write blocked: %d open comment(s) would become orphaned", len(orphans)) |
| 230 | lastAuthor := "" |
| 231 | for _, orphan := range orphans { |
| 232 | author := strings.TrimSpace(orphan.Author) |
| 233 | if author == "" { |
| 234 | author = strings.TrimSpace(orphan.AuthorEmail) |
| 235 | } |
| 236 | if author == "" { |
| 237 | author = "(unknown author)" |
| 238 | } |
| 239 | if author != lastAuthor { |
| 240 | u.Err().Linef("%s", author) |
| 241 | lastAuthor = author |
| 242 | } |
| 243 | u.Err().Linef(" %s\t%s\t%s", |
| 244 | orphan.CommentID, |
| 245 | truncateString(oneLineTSV(orphan.Content), 80), |
| 246 | truncateString(oneLineTSV(orphan.Quote), 60), |
| 247 | ) |
| 248 | } |
| 249 | return &ExitError{Code: exitCodeOrphaned, Err: nil} |
| 250 | } |