-
Notifications
You must be signed in to change notification settings - Fork 23
Manage email clips from the CLI #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0919bf1
Manage email clips from the CLI
robzolkos 45fadb0
Disclose the clip page boundary
robzolkos 776bab7
Validate clip content against its source entry
robzolkos 37de369
Bound clip source validation
robzolkos 71ceddd
Document clip account semantics
robzolkos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,268 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "unicode" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/basecamp/hey-sdk/go/pkg/generated" | ||
|
|
||
| "github.com/basecamp/hey-cli/internal/apierr" | ||
| "github.com/basecamp/hey-cli/internal/htmlutil" | ||
| "github.com/basecamp/hey-cli/internal/output" | ||
| "github.com/basecamp/hey-cli/internal/terminal" | ||
| ) | ||
|
|
||
| type clipsCommand struct { | ||
| cmd *cobra.Command | ||
| } | ||
|
|
||
| func newClipsCommand() *clipsCommand { | ||
| clipsCommand := &clipsCommand{} | ||
| clipsCommand.cmd = &cobra.Command{ | ||
| Use: "clips", | ||
| Short: "List the newest page of passages clipped from email", | ||
| Annotations: map[string]string{ | ||
| "agent_notes": "Returns the newest page of clip IDs, content, source entry IDs, and source thread context. The SDK does not expose the cursor for older pages. Use an ID with hey clip delete.", | ||
| }, | ||
| Example: ` hey clips | ||
| hey clips --json | ||
| hey clips --ids-only`, | ||
| RunE: clipsCommand.run, | ||
| Args: cobra.NoArgs, | ||
| } | ||
| return clipsCommand | ||
| } | ||
|
|
||
| func (c *clipsCommand) run(cmd *cobra.Command, _ []string) error { | ||
| if err := requireAuth(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| clips, err := sdk.Clips().List(cmd.Context()) | ||
| if err != nil { | ||
| return apierr.FromSDK(err) | ||
| } | ||
| notice := clipsPageNotice(clips) | ||
| if stderrNotice := paginationNoticeForStderr(writer.EffectiveFormat(), notice); stderrNotice != "" { | ||
| fmt.Fprintln(cmd.ErrOrStderr(), stderrNotice) | ||
| } | ||
|
|
||
| switch writer.EffectiveFormat() { | ||
| case output.FormatStyled: | ||
| if len(clips) == 0 { | ||
| fmt.Fprintln(cmd.OutOrStdout(), "No clips found") | ||
| return nil | ||
| } | ||
| table := newTable(cmd.OutOrStdout()) | ||
| table.addRow([]string{"ID", "Content", "Entry", "Thread", "Saved"}) | ||
| for _, clip := range clips { | ||
| table.addRow([]string{ | ||
| fmt.Sprintf("%d", clip.Id), | ||
| truncate(terminal.SanitizeLine(clip.Content), 60), | ||
| fmt.Sprintf("%d", clip.EntryId), | ||
| clipTopicLabel(clip.Topic), | ||
| formatDate(clip.CreatedAt), | ||
| }) | ||
| } | ||
| table.print() | ||
| if notice != "" { | ||
| fmt.Fprintln(cmd.ErrOrStderr(), "Notice: "+notice) | ||
| } | ||
| return nil | ||
| case output.FormatMarkdown: | ||
| return writeClipsMarkdown(cmd, clips) | ||
| default: | ||
| return writeOK(clips, | ||
| output.WithSummary(fmt.Sprintf("%d %s", len(clips), clipNoun(len(clips)))), | ||
| output.WithNotice(notice), | ||
| output.WithBreadcrumbs( | ||
| output.Breadcrumb{Action: "create", Command: "hey clip create <entry-id> --content <text>", Description: "Save text from an email entry"}, | ||
| output.Breadcrumb{Action: "delete", Command: "hey clip delete <clip-id>", Description: "Delete a clip"}, | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func clipsPageNotice(clips []generated.Clip) string { | ||
| if len(clips) == 0 { | ||
| return "" | ||
| } | ||
| return "Showing HEY's newest clips page. The SDK does not expose the cursor for older pages." | ||
| } | ||
|
|
||
| func clipTopicLabel(topic generated.ClipTopic) string { | ||
| name := terminal.SanitizeLine(topic.Name) | ||
| if name == "" { | ||
| return fmt.Sprintf("%d", topic.Id) | ||
| } | ||
| return fmt.Sprintf("%s (%d)", name, topic.Id) | ||
| } | ||
|
|
||
| func writeClipsMarkdown(cmd *cobra.Command, clips []generated.Clip) error { | ||
| if len(clips) == 0 { | ||
| _, err := fmt.Fprintln(cmd.OutOrStdout(), "(no results)") | ||
| return err | ||
| } | ||
| var document strings.Builder | ||
| document.WriteString("| id | content | entry_id | topic_id | topic | saved |\n") | ||
| document.WriteString("| --- | --- | --- | --- | --- | --- |\n") | ||
| for _, clip := range clips { | ||
| fmt.Fprintf(&document, "| %d | %s | %d | %d | %s | %s |\n", | ||
| clip.Id, | ||
| markdownSafeText(clip.Content), | ||
| clip.EntryId, | ||
| clip.Topic.Id, | ||
| markdownSafeText(clip.Topic.Name), | ||
| formatDate(clip.CreatedAt), | ||
| ) | ||
| } | ||
| _, err := fmt.Fprint(cmd.OutOrStdout(), document.String()) | ||
| return err | ||
| } | ||
|
|
||
| func clipNoun(count int) string { | ||
| if count == 1 { | ||
| return "clip" | ||
| } | ||
| return "clips" | ||
| } | ||
|
|
||
| type clipCommand struct { | ||
| cmd *cobra.Command | ||
| } | ||
|
|
||
| func newClipCommand() *clipCommand { | ||
| clipCommand := &clipCommand{} | ||
| clipCommand.cmd = &cobra.Command{ | ||
| Use: "clip", | ||
| Short: "Save and manage passages from email", | ||
| Annotations: map[string]string{ | ||
| "agent_notes": "Create a clip from text carried by an email entry, or delete a clip. HEY assigns a created clip to the source entry's account and resolves deletion by identity-owned clip ID across linked accounts; --account selects list presentation. The CLI verifies that the passage is source-backed by the entry's message content before saving it, with a 64 KiB passage limit and a 1 MiB source-validation limit. Find clip IDs with hey clips.", | ||
| }, | ||
| } | ||
| clipCommand.cmd.AddCommand(newClipCreateCommand().cmd) | ||
| clipCommand.cmd.AddCommand(newClipDeleteCommand().cmd) | ||
| return clipCommand | ||
| } | ||
|
|
||
| const ( | ||
| maxClipContentBytes = 64 << 10 | ||
| maxClipSourceBytes = 1 << 20 | ||
| ) | ||
|
|
||
| type clipCreateCommand struct { | ||
| cmd *cobra.Command | ||
| content string | ||
| } | ||
|
|
||
| func newClipCreateCommand() *clipCreateCommand { | ||
| createCommand := &clipCreateCommand{} | ||
| createCommand.cmd = &cobra.Command{ | ||
| Use: "create <entry-id>", | ||
| Aliases: []string{"add"}, | ||
| Short: "Save text from an email entry", | ||
| Long: "Save a passage from an email entry. HEY assigns the clip to the source entry's account. The content must be present in the entry's message text; whitespace differences are accepted. Passages are limited to 64 KiB and source entries to 1 MiB for validation.", | ||
| Example: ` hey clip create 987 --content "The launch moves to Wednesday."`, | ||
| RunE: createCommand.run, | ||
| Args: usageExactOneArg(), | ||
| } | ||
| createCommand.cmd.Flags().StringVar(&createCommand.content, "content", "", "Text selected from the email entry (required)") | ||
| return createCommand | ||
| } | ||
|
|
||
| func (c *clipCreateCommand) run(cmd *cobra.Command, args []string) error { | ||
| if err := requireAuth(); err != nil { | ||
| return err | ||
| } | ||
| entryID, err := parsePositiveID(args[0], "entry") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if strings.TrimSpace(c.content) == "" { | ||
| return apierr.ErrUsage("--content is required") | ||
| } | ||
| if len(c.content) > maxClipContentBytes { | ||
| return apierr.ErrUsage(fmt.Sprintf("--content exceeds the %d KiB clip limit", maxClipContentBytes>>10)) | ||
| } | ||
| message, err := sdk.Messages().Get(cmd.Context(), entryID) | ||
| if err != nil { | ||
| return apierr.FromSDK(err) | ||
| } | ||
| if message == nil { | ||
| return apierr.ErrNotFound("message", fmt.Sprintf("%d", entryID)) | ||
| } | ||
| if len(message.Content) > maxClipSourceBytes { | ||
| return apierr.ErrAPI(0, fmt.Sprintf("entry %d content exceeds the %d MiB clip validation limit", entryID, maxClipSourceBytes>>20)) | ||
| } | ||
| if !clipContentMatches(c.content, message.Content) { | ||
| return apierr.ErrUsageHint( | ||
| fmt.Sprintf("--content does not match text in entry %d", entryID), | ||
| "Copy an exact passage from the entry; whitespace differences are allowed.", | ||
| ) | ||
| } | ||
| if err := sdk.Clips().Create(cmd.Context(), entryID, c.content); err != nil { | ||
| return apierr.FromSDK(err) | ||
| } | ||
| return writeMutation(cmd, fmt.Sprintf("Clip from entry %d created", entryID), map[string]any{"entry_id": entryID}, | ||
| output.WithBreadcrumbs(output.Breadcrumb{Action: "list", Command: "hey clips", Description: "Find the new clip ID"}), | ||
| ) | ||
| } | ||
|
|
||
| func clipContentMatches(content, entryHTML string) bool { | ||
| selected := normalizeClipText(content) | ||
| entry := normalizeClipText(htmlutil.MessageSourceText(entryHTML)) | ||
| return selected != "" && strings.Contains(entry, selected) | ||
| } | ||
|
|
||
| func normalizeClipText(text string) string { | ||
| var normalized strings.Builder | ||
| normalized.Grow(len(text)) | ||
| pendingSpace := false | ||
| for _, r := range text { | ||
| if unicode.IsSpace(r) { | ||
| pendingSpace = normalized.Len() > 0 | ||
| continue | ||
| } | ||
| if pendingSpace { | ||
| normalized.WriteByte(' ') | ||
| pendingSpace = false | ||
| } | ||
| normalized.WriteRune(r) | ||
| } | ||
| return normalized.String() | ||
| } | ||
|
|
||
| type clipDeleteCommand struct { | ||
| cmd *cobra.Command | ||
| } | ||
|
|
||
| func newClipDeleteCommand() *clipDeleteCommand { | ||
| deleteCommand := &clipDeleteCommand{} | ||
| deleteCommand.cmd = &cobra.Command{ | ||
| Use: "delete <clip-id>", | ||
| Aliases: []string{"remove", "rm"}, | ||
| Short: "Delete a saved clip", | ||
| Long: "Delete an identity-owned clip by ID across linked accounts.", | ||
| Example: ` hey clip delete 44`, | ||
| RunE: deleteCommand.run, | ||
| Args: usageExactOneArg(), | ||
| } | ||
| return deleteCommand | ||
| } | ||
|
|
||
| func (c *clipDeleteCommand) run(cmd *cobra.Command, args []string) error { | ||
| if err := requireAuth(); err != nil { | ||
| return err | ||
| } | ||
| clipID, err := parsePositiveID(args[0], "clip") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := sdk.Clips().Delete(cmd.Context(), clipID); err != nil { | ||
| return apierr.FromSDK(err) | ||
| } | ||
| return writeMutation(cmd, fmt.Sprintf("Clip %d deleted", clipID), map[string]any{"id": clipID}) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.